Change SerialPort BaudRate when connection is open

I use the SerialPort class to communicate with an external device. I start communication at 300 bauds per second, but after the initial “handshake” I need to switch to the baud rate indicated by the device (usually 9600 bps).

I was not successful in my attempts to continue communication after I increased the Baud rate, but I'm not sure why. This leads me to my question: What is the correct way to change the baud rate? Can this be done while the connection is open, or do I need to close the connection first?

I could not find information about this in the documentation ...

I tried both and was unsuccessful in both directions, which made me think that I should miss something obvious.

+5
source share
2 answers

Turns out you can change SerialPort.BaudRate while it's open. But the SerialPort.DiscardInBuffer mentioned by @Groo in the answer is good advice!

+4
source

From my experience, the safest bet so far is to close the connection first, change the speed and then open it again. Your device will not mind this, as it is probably waiting for you to send some control characters to make sure that the speed has been changed.

, , ( ), , ( SerialPort.DiscardInBuffer).

+9

All Articles