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
This will get written to the serial port driver transmit buffer without any extra help. Flush is not required. If it doesn't make it to the device then you've probably got a handshake problem. A classic mistake is to leave it set to None and not setting the DtrEnable and RtsEnable properties to true.
It can also be a wiring problem, shake that out by using another program like HyperTerminal or Putty. Last but not least: it is difficult to keep a device happy that uses timeouts when you're debugging your code.
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
.
I have a very similar situation. I try to write on a serial port from c# to an Arduino board. I can write into the serial port but data are finally send only after close and open the port. I try several settings, may be that should help you. DtrEnable is certainly the most important one.
port.Handshake = Handshake.None;
port.Open();
port.DiscardInBuffer();
port.DiscardOutBuffer();
port.DtrEnable = true;
精彩评论