C#Serial.DataReceiveved数据是分段的 [英] C# Serial.DataRecieved data is in pieces

查看:73
本文介绍了C#Serial.DataReceiveved数据是分段的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序正在所有可用的COM端口中搜索文本"ConnectAlready".多亏了堆栈溢出人员的帮助,我才能够完成大部分任务.我打印在控制台上接收到的数据,但没有得到一个字符串"ConnectAlready",而是得到了类似的内容:

I'm making a program that is searching all available COM ports for a text "ConnectAlready." Thanks to the help of people on stack overflow I was able to do a majority of it. I print the data i receive on the console, but instead of getting one string "ConnectAlready", i get something like:

Conne
ct
alre
ady

其中每个都是不同的字符串,因此我无法检查串行端口是否正在读取"ConnectAlready".

Each one of those is a different string, and therefore i cannot check if the serial port is reading "ConnectAlready."

private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort serialPort1 = sender as SerialPort;
        byte[] data = new byte[serialPort1.BytesToRead];
        Stream portStream = serialPort1.BaseStream;
        portStream.Read(data, 0, data.Length); 
        string dataString = Encoding.UTF8.GetString(data);
        Console.WriteLine(dataString);
        bool hasData = dataString.Contains("ConnectAlready");
        if (hasData)
        {
            established = true;
            estport = serialPort1.PortName;
            MessageBox.Show(estport);
        }
    }

有什么建议吗?谢谢您的帮助!

Any advice? thanks for your help!

推荐答案

好的,您的问题如下:如果在端口上接收到数据,则触发该事件.然后,您读取已经存在的字节.但是您的计算机比传输的速度快->并不是整个消息都已经保存在缓冲区中.

Okay, your problem is the following: If you receive data on your Port, the event fires. Then you read the bytes that are already there. But your computer is faster than your transmission --> Not the whole message is already in your buffer.

以下是两种可能的解决方案:

Here are two possible solutions:

1:如果您的邮件末尾包含换行符,则可以这样简单地完成操作:

//Add to your init:
YourSerialPort.Timeout = 100; //Maximum duration of your message in milliseconds
YourSerialPort.NewLine = "\r\n"; //You can also change the newline-character (you can also omit)

private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    string dataString = ((SerialPort)sender).ReadLine();
    if (dataString.Contains("ConnectAlready"))
    {
        //Your code
    }
}

如果使用Arduino Serial.println()函数,这将是您的解决方案.

If you use the Arduino Serial.println()-function, this would be your solution.

2:稍等片刻,然后阅读您的消息:

private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    Thread.sleep(50);
    SerialPort serialPort1 = sender as SerialPort;
    byte[] data = new byte[serialPort1.BytesToRead];
    Stream portStream = serialPort1.BaseStream;
    portStream.Read(data, 0, data.Length); 
    string dataString = Encoding.UTF8.GetString(data);
    Console.WriteLine(dataString);
    bool hasData = dataString.Contains("ConnectAlready");
    if (hasData)
    {
        //Your code
    }
}

祝你好运!

这篇关于C#Serial.DataReceiveved数据是分段的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆