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!
SerialPort
that only accepts a byte array. The only twoRead
function there are both have three parameters – Scott Chamberlain