JeroenV
JeroenV

Reputation: 157

jssc writebytes stops working after a while on linux

I'm programming in Java to talk with a device connected to a com-port (connected to the PC through USB, but by an RS232 to USB cable in between). I've written a program that talks with jssc and that functioned correctly under Windows, it kept working for a longer time even when nothing happens, like it should. Under Linux the program stops responding after a minute of 2 or 3 and I wonder why.

The run statement is as follows

public void run() {
    while (stayConnected) {
        try {
            serialPort.writeBytes(pollBuf);
            readResponse(false);
            Thread.sleep(400);
            serialPort.writeBytes(readEvents);
            readResponse(true);
        } catch (InterruptedException ie) {
            logger.error("Interrupted exception: " + ie.getMessage());
        } catch (SerialPortException spe) {
            logger.error("SerialPortException: " + spe.getMessage());
        }
    }
}

To know where the program hangs I've added loglines and I found out that the last command to function correctly is the last call to readResponse(true) and the first to stop returning is serialPort.writeBytes(pollBuf). Hoping that it would solve the issue I split the 400 ms sleep in two and placed the other sleep before serialPort.writeBytes(pollBuf). That doesn't help. Somehow the serialPort.writeBytes function just never returns and doesn't throw an exception either.

Does anyone have a guess of what the failure might be? It's not the stayConnected boolean as I never call the function yet that sets it so false;

edit: I've just added a counter and the program gets into the loop 283 and 285 times when I run it twice, that's pretty close and both around 2 minutes ...

Upvotes: 1

Views: 875

Answers (2)

Fabio Frumento
Fabio Frumento

Reputation: 330

I had exactly the same issue, the cause was another thread closing the port while the main one still reading, i see your code snippet is about a Runnable so carefully check you multithread management.

Upvotes: 0

Sean
Sean

Reputation: 145

I'm having a very similar problem under Windows 7. I've deployed my software to a clients PC and after about 5 to 6 hours the serial port can be opened but not able to be written to.

As per the example my code is similar:

String readPort(String command, int byteToRead) {

    String Input = null;
    if (opened == true) {
        try {
            serialPort.writeString(command);

            Input = serialPort.readString(byteToRead);

        } catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }

    return Input;
}

The line of code that does not return is

serialPort.writeString(command);

Upvotes: 1

Related Questions