本文介绍了无法从串口获取正确的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发用于读取串行端口数据的窗口应用程序.

这是我读取串口的代码:

I am develop window application for reading serial port data.

Here is my code for reading serial port:

void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    try
    {
        if (comPort.IsOpen == true)//comPort is serial port which i use in my project
        {
            bytes = comPort.BytesToRead;
            byte[] comBuffer = new byte[bytes];
            comPort.Read(comBuffer, 0, bytes);
        }
    }
    catch (Exception ex)
    {
    }
}


我的问题是当我连接两台计算机并将数据从一台计算机发送到我的软件时,我没有收到完整的数据.但是当我放置断点和此事件并检查然后它显示完整数据并且还显示完整数据但没有断点时没有显示完整的数据意味着丢失了许多数据.
请解决此问题.

[edit]添加了代码块,将我的内容作为纯文本..."选项已禁用-OriginalGriff [/edit]


My problem is when i connect two computer and send data from one computer to my software i am not receive complete data.But when i put break point and this event and check then it show complete data and also display complete data but without break point it not show complete data means lost many data.
Please solve this problem.

[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]

推荐答案


报价:

基于编码,读取SerialPort对象的流和输入缓冲区中所有立即可用的字节.

Reads all immediately available bytes, based on the encoding, in both the stream and the input buffer of the SerialPort object.



在MSDN上阅读以下内容:
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readexisting.aspx [ ^ ]



Read this at MSDN:
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readexisting.aspx[^]


这篇关于无法从串口获取正确的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 01:25