gpt4 book ai didi

c# - 串行端口通信丢失一个字节

转载 作者:太空狗 更新时间:2023-10-29 23:46:20 25 4
gpt4 key购买 nike

我负责创建一个 HMI,它将使用 USB 端口与设备通信。

我是 C# 和 WPF 的新手,所以我开始进行一些研究,并在此站点上发现了几个问题和主题,它们帮助我实现了我想要的起点:我能够读取和写入 SerialPort 类。

为了测试设备,我有一个 UART,可以回显它收到的任何消息。我有一个带有文本框、按钮和标签的简单表单。单击按钮后,它会将框中键入的文本发送到设备(如果没有输入文本,我有一个预定义的字节数组来测试)。只要端口接收到字节,标签就会更新。

第一条消息的代码运行正常(哪条消息无关紧要),但之后发送的任何消息几乎总是返回丢失的字节。我不知道为什么会这样,我每次都尝试丢弃缓冲区,但无济于事。

这是我的代码:

using System.IO.Ports;

namespace LearningSteps
{
/// <summary>
/// Interaction logic for Comm.xaml
/// </summary>
///
public partial class Comm : Window
{
SerialPort port;
BackgroundWorker backgroundWorker1 = new BackgroundWorker();

public delegate void AtualizaCallBack(string message);

public Comm()
{
InitializeComponent();
//InitializeBackgroundWorker();
port = new SerialPort("COM4",115200,Parity.None,8,StopBits.One);
port.RtsEnable = true;
port.DataReceived +=
new SerialDataReceivedEventHandler(Recebido);
port.Open();

}

private void Recebido(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
String indata = sp.ReadExisting();

sp.DiscardOutBuffer();
sp.DiscardInBuffer();

my_label.Dispatcher.Invoke(new AtualizaCallBack(this.atualiza),new object[]{indata});

}

private void bt_Click(object sender, RoutedEventArgs e)
{
if (txt1.Text.Length == 0)
{
byte[] vetor = new byte[] { 0x40, 0x45, 0x2B, 0x5C, 0x10 };
port.DiscardOutBuffer();
port.Write(vetor, 0, 5);
}
else
{
port.DiscardOutBuffer();
port.Write(txt1.Text);
}
}

private void atualiza(string s)
{
my_label.Content = s;
}

}
}

这是 XAML:

<Window x:Class="LearningSteps.Comm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Comm" Height="346" Width="404">
<Grid Background="#FF9E9E9E">
<Label x:Name="my_label" HorizontalAlignment="Left" Height="40" Margin="80,200,0,0" VerticalAlignment="Top" Width="240" Background="#FFD1D18D" FontSize="14" FontWeight="Bold" Foreground="#FF1D83BD" HorizontalContentAlignment="Center" Content="Lalala"/>
<TextBox x:Name="txt1" HorizontalAlignment="Left" Height="40" Margin="80,80,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="240" TextAlignment="Center" FontSize="14" FontWeight="Bold"/>
<Button x:Name="bt" Content="Enviar" HorizontalAlignment="Left" Height="40" Margin="140,140,0,0" VerticalAlignment="Top" Width="120" FontWeight="Bold" Foreground="#FF4084BD" Click="bt_Click">
<Button.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF3F3F3" Offset="0"/>
<GradientStop Color="#FFE6C041"/>
<GradientStop Color="#FFE8C382" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>
</Grid>
</Window>

对这里可能存在的问题有什么想法吗?

最佳答案

        String indata = sp.ReadExisting();
sp.DiscardOutBuffer();
sp.DiscardInBuffer();

这是非常不明智的,端口将在您调用 sp.ReadExisting() 时继续接收数据。在您使用的高波特率下,当 ReadExisting() 返回时接收到另一个字节的可能性非零。您的 DiscardInBuffer() 调用会破坏它。

删除对 DiscardOutBuffer 和 DiscardInBuffer 的所有 调用。它们不会刷新,它们只会导致随机数据丢失。它们应该只用于协议(protocol)重置以清除驱动程序缓冲区,您没有协议(protocol)。

关于c# - 串行端口通信丢失一个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18494043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com