How to change the baud rate without closing the connection?

I want to connect to a device connected to a serial port (COM4) with an initial speed of 300 baud, the device can change the baud rate to 9600 using a command, my Java code will send this command to the device and the baud rate of the device has changed, but I don’t know how to change the baud rate in my program without closing the connection. When the connection has been closed, the device returns to the initial transmission rate.

Is there a way to change the baud rate in Java while the connection is open?

After sending the “change baud rate” command to the device when the device below has lost the connection. I think that this method is intended only for initialization, and not for changing the data transfer rate in the middle of communication.

port.setSerialPortParams( 9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 
+6
source share
2 answers

Most likely, the device perceives DTR or RTS. Windows will switch them when the port is open, and restore them when the port is closed. So you have 3 options. I'm not sure which one will work - you may have to try all of them, I don’t have a window with a serial port.

  • Use another Java library, such as "gnu.io.RXTXPort" librxtx, which can change the transfer rate without closing the connection.

  • Try using the Windows "mode" command: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/mode.mspx . Try both RTS and DSR before “on” and “off,” and see if any setup helps. Please note: if this happens, the device will never receive a reset, even if you exit your java program. You will need to call "mode" on the reset device again.

  • Receive a special serial cable that does not transmit DTR signals. They are called “3-wire only” (RX and TX only) or “5-wire” (RX / TX + RTS / CTS) RS232 cables and are easy to make.

+3
source

You always indicate the baud rate when connecting to the device. Even if you do not, the software / API you use will do it for you. Most likely, your API connects to this device with a default speed of 300 times. The baud rate is determined for serial connection not for the device. When you are not connected, the baud rate is not set.

0
source

Source: https://habr.com/ru/post/892393/


All Articles