6
votes

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);
2
... how are you closing the connection? Generally serial connections don't have an "open" and "closed" state. There's no way to "detect" that someone has gone away (other than timeouts perhaps).Chris Eberle

2 Answers

3
votes

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.

0
votes

You always specify the baud rate when you connect to a device. Even if you don't, the software / API you use does that for you. Chances are your API connects to this device with the default baud rate of 300. Baud rate is defined for the serial connection not for the device. Once you are not connected, there is no baud rate.