1
votes

I'm trying to read bytes from a scanner hooked up to a COM port into a byte array. The Serial Port library in C# already has a Read function, this is the function I use to attempt a read. 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. 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. Otherwise I get my expected output in small "byte sized samples." Any help is appreciated!

3
Your code is not valid code, there is no function of SerialPort that only accepts a byte array. The only two Read function there are both have three parametersScott Chamberlain
Why not use the serial ports datareceived event to get the data? No need to use additional threads as the datareceived handler is in its own thread.user2019047
Read is overloaded to accept a byte arraydan-0

3 Answers

3
votes

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.

0
votes

I recently posted a similar question found here: SerialPort.DataReceived Returning Strange Results

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#

My implementation is as follows in the DataReceived event:

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;
0
votes

The best approach to put in while loop and also give 10 millisecond of sleep time in between of byte read. This will give 10 milliseconds to scanner to write data on COM port. 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.