简体   繁体   English

不完整的串口读C#

[英]Incomplete Serial Port Read C#

I'm trying to read bytes from a scanner hooked up to a COM port into a byte array. 我正在尝试将连接到COM端口的扫描程序中的字节读入字节数组。 The Serial Port library in C# already has a Read function, this is the function I use to attempt a read. C#中的串行端口库已经具有读取功能,这是我用来尝试读取的功能。 I have it setup so that the bytes read in are output to the console. 我设置它,以便读入的字节输出到控制台。 I'm working with a protocol that is very predictable so I know what kind of byte array I am expecting when I pass that line in the code. 我正在使用一个非常可预测的协议,因此我知道当我在代码中传递该行时,我期待什么样的字节数组。 However, if I run the program, I only get a single byte read in. If I re-run that same instance of the program (by sending the same read command) I get the rest of the expected bytes. 但是,如果我运行程序,我只读取一个字节。如果我重新运行该程序的同一个实例(通过发送相同的读取命令),我得到剩余的预期字节。 Only after I run this a third time do I get all of the bytes I'm expecting. 只有在我第三次运行之后才能获得我期望的所有字节。 This problem is completely avoided though if I simply insert a breakpoint over the read line and step over that line. 如果我只是在读取行上插入一个断点并跨过该行,则完全避免了这个问题。 If I do this, I get a complete read every time. 如果我这样做,我每次都会得到完整的阅读。 My question is, how can I get a complete read every time without inserting a breakpoint? 我的问题是,如何在不插入断点的情况下每次都能获得完整的读取? I've tried using the System Pause approach to halt the execution and let the COM port scan fast enough, which did not work. 我已经尝试使用系统暂停方法来暂停执行并让COM端口扫描足够快,这不起作用。 I've also tried using a thread (see code below). 我也试过使用一个线程(见下面的代码)。 This also did not work. 这也行不通。 Any suggestions? 有什么建议么?

    t = new Thread(() => device.Read(buffer));
    t.Start();
    t.Join();

Again, my expected output only comes in a full-packet after re-sending the Read command a few times or by stepping over the above commands with a breakpoint. 同样,我的预期输出仅在重新发送Read命令几次之后或通过使用断点逐步执行上述命令而出现在完整数据包中。 Otherwise I get my expected output in small "byte sized samples." 否则,我会在小的“字节大小的样本”中得到我期望的输出。 Any help is appreciated! 任何帮助表示赞赏!

This is expected behaviour with byte streams. 这是字节流的预期行为。 Loop round the read and pump however many bytes received one-by-one into your ProtocolUnit class instance, (or whatever), until it is complete and verified. 然后循环读取并泵送许多字节逐个接收到您的ProtocolUnit类实例(或其他),直到它完成并验证。

I recently posted a similar question found here: SerialPort.DataReceived Returning Strange Results 我最近发布了一个类似的问题: SerialPort.DataReceived返回奇怪的结果

The problem is, as you know, the program tries to read from the buffer without any guarantee that the device is finished issuing whatever command it's running. 问题是,如你所知,程序试图从缓冲区中读取,而不保证设备完成发出它正在运行的任何命令。 My advice is to implement the first answer in this post: Reading from the serial port in C# 我的建议是在这篇文章中实现第一个答案: 从C#中的串口读取

My implementation is as follows in the DataReceived event: 我在DataReceived事件中的实现如下:

void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e)
    {
        byte[] data = new byte[serialPort.BytesToRead];
        serialPort.Read(data, 0, data.Length);
        Console.Write(System.Text.Encoding.UTF8.GetString(data));
    }

And just to wire it up in the code: 只是在代码中连接它:

serialPort.DataReceived += serialPort_DataReceived;

The best approach to put in while loop and also give 10 millisecond of sleep time in between of byte read. 放入while循环的最佳方法是在字节读取之间提供10毫秒的休眠时间。 This will give 10 milliseconds to scanner to write data on COM port. 这将给扫描器提供10毫秒的时间来在COM端口上写入数据。 Actually, Problem happens when you connect scanner in network port. 实际上,当您在网络端口连接扫描仪时会出现问题。

You can also write delegate or event which will notify when data is completely read from scanner. 您还可以编写代理或事件,以便在从扫描仪完全读取数据时通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM