简体   繁体   English

一段时间后,jssc writebytes在Linux上停止工作

[英]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). 我正在用Java编程,以与连接到COM端口的设备进行通讯(通过USB连接到PC,但通过两者之间的RS232转USB电缆连接)。 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. 我编写了一个与jssc对话的程序,并且该程序在Windows下可以正常运行,即使什么也没发生,它仍可以工作更长的时间,就像应该的那样。 Under Linux the program stops responding after a minute of 2 or 3 and I wonder why. 在Linux下,程序在2或3分钟后停止响应,我想知道为什么。

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). 为了知道程序的挂起位置,我添加了日志行,并且发现正常运行的最后一条命令是对readResponse(true)的最后一次调用,而第一个停止返回的命令是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). 希望可以解决该问题,我将400 ms睡眠分为两部分,并将另一部分睡眠置于serialPort.writeBytes(pollBuf)之前。 That doesn't help. 那没有帮助。 Somehow the serialPort.writeBytes function just never returns and doesn't throw an exception either. 不知何故,serialPort.writeBytes函数永远不会返回,也不会引发异常。

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; 这不是stayConnected布尔值,因为我从未调用过该函数,但将其设置为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 ... 编辑:我刚刚添加了一个计数器,当我运行两次时,程序进入循环283和285次,非常接近,并且都在2分钟左右...

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. 在Windows 7下,我遇到了一个非常类似的问题。我已经将软件部署到客户端PC,大约5到6个小时后,串行端口可以打开但无法写入。

As per the example my code is similar: 根据示例,我的代码是相似的:

String readPort(String command, int byteToRead) { 字符串readPort(字符串命令,整数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); serialPort.writeString(命令);

我有完全相同的问题,原因是另一个线程关闭了端口,而主要线程仍在读取,我看到您的代码段是关于Runnable的,因此请仔细检查您的多线程管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM