Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change baud rate without closing the connection?

I want to connect to a device that connected to serial port (COM4) with initial baud rate 300, the device can change its baud rate to 9600 with a command, my Java code send this command to device and baud rate of the device changed, but i don't know how to change baud rate in my program without closing the connection. When connection has been closed, device comeback to the initial baud rate.

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

After I send the "change baud rate" command to device, when below code executed device lost the connection. I think this method is just for initializing, not for changing baud rate in the middle of communication.

port.setSerialPortParams(
    9600,
    SerialPort.DATABITS_8,
    SerialPort.STOPBITS_1,
    SerialPort.PARITY_NONE);
like image 292
Mehdi Avatar asked Jul 09 '11 15:07

Mehdi


1 Answers

Most likely the device senses either DTR or RTS. Windows will toggle them when port is open, and restore them when port is closed. Thus, you have 3 options. I am not sure which one will work -- you might have to try them all, I do not have windows box with serial port.

  1. Use a different Java library, like "gnu.io.RXTXPort" of librxtx, which can change baudrate without closing the connection.

  2. Try to use windows "mode" command: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/mode.mspx . Try both RTS and DSR to "on" and "off", and see if any setting helps. Note that if this succeeds, the device will never get reset, even if you exit your java program. You will have to call "mode" again to reset the device.

  3. Get a special serial cable, which does not pass DTR signals. These are called "3-wire" (RX and TX only) or "5-wire" (RX/TX + RTS/CTS) RS232 cables, and there are easy to make.

like image 175
theamk Avatar answered Oct 03 '22 01:10

theamk