Search code examples
c#serial-portflush

c# serial port flushing for small data chunks


I'm writing a small byte array of two bytes using something like the following:

int bytes_to_write = 2;
port.Write(byte_buffer, buffer_offset, bytes_to_write);

It appears as though the underlying libraries are waiting for more data to come through before writing it to the port. Is this the correct behaviour and can I force it to write the small chunk immediately? Some of the forums say using:

port.BaseStream.Flush(); 

will cause the correct behaviour but others say this doesn't work. I've run a couple of tests and it doesn't seem to be working for me.

Cheers, Richard


Solution

  • Are you sure the data isn't getting written to the port? I've dealt with the SerialPort module extensively and have written single bytes out just fine. There should be no need to call Flush().

    Set a breakpoint with the debugger after the port.Write call and inspect the port object. Is the BytesToWrite property > 0?

    Edit: Also, make sure your buffer_offset argument is 0.