Some problems encountered by Java using RXTX

Make your own development and use some packages of rxtx in java to call the c library to communicate with the device. A little experience:

For operations such as library files that need to be imported, a lot of information can be found on the network:


  • Adopt rxtxcomm package:

Problems encountered:

Not much to say, go straight to the code:

public static SerialPort openSerialPort(SerialPortParameter parameter, int timeout)
        throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException {
    //Get port by port name
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(parameter.getSerialPortName());
    //Open port, (custom name, open timeout)
    CommPort commPort =, timeout);
    //Determine whether it is a serial port
    if (commPort instanceof SerialPort) {
        SerialPort serialPort = (SerialPort) commPort;
        //Set serial port parameters (baud rate, data bit 8, stop bit 1, check bit none)
        serialPort.setSerialPortParams(parameter.getBaudRate(), parameter.getDataBits(), parameter.getStopBits(), parameter.getParity());
        return serialPort;
    } else {
        //Are other types of ports
        throw new NoSuchPortException();

When I call commportidentifier Getportidentifier (parameter.getSerialPortName()) such as my parameter The value of getserialportname() is COM1. When I use the tool AccessPort to monitor another serial port, I will find that it will open and close other serial ports once, which is easy to cause

The jvm crashed and the program could not be used normally

Reasons for the problem (different opinions on the Internet): the first one: the jvm may collapse because you are blocking now

The second is the overlapping operation of win10

(Reference) RXTX cannot be used on Win10. An error will be reported as soon as write is called. RXTX has not been updated for more than 10 years. I think it's the pot of Win10, because the overlapping operation of Win10 will report an error. Open the serial port by calling the API in C. when the overlapping operation is used, all kinds of abnormalities and random errors can only be opened in a synchronous way. RXTX may be based on overlapping operations, so it won't work in Win10.
How to re-establish a serial port read-write interface.
Open the serial port asynchronously, which will cause problems.
Then, the timeout parameter is set to 0 except that the shortest one is - 1. This allows non blocking operations like InputStream. Incidentally, these parameters are related. If they are not set properly, they may get stuck there. Therefore, it is easiest to set them as non blocking and no delay.
Flow control is off by default. It is impossible to use flow control equipment, so you don't need to manage it. Just open the input buffer, output buffer, baud rate, byte length, check and stop bit.
Create a background thread in java to read the serial port, and then use a blocking queue as the buffer of the application layer.
With a buffer, data splicing can be done. There are three elements of data splicing: 1. Timeout. The poll method of blocking queue can be used; 2. Maximum length: check the maximum length in the background thread; 3, check condition, check condition is a Lambda object, each time after receiving the data packet, call the Lambda to determine whether to meet the verification requirements. If the requirements are met, then quit directly, no longer wait for the timeout, and do not wait for the data to reach the upper limit of the number.

Solution: I replaced the jar package of purejavacomm


Based on the previous rxtx, the code does not need to be changed, and there is no previous problem

Friends also recommended some other methods, which have not been used yet: jSerialComm



Keywords: Java

Added by adrianl on Mon, 07 Mar 2022 18:16:04 +0200