QSerialPort是导致程序停止(无限循环?)如果打开设备 [英] QSerialPort is causing a program stop (endless loop?) if opening device

查看:1994
本文介绍了QSerialPort是导致程序停止(无限循环?)如果打开设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在串行设备上书写。不幸的是,我有感觉QSerialPort没有正确实现在linux下。与其他方法(python)相比,我得到!有时!当尝试调用时,暂停程序:

I want to write on a serial device. Unfortunately I have the feeling the QSerialPort is not properly implemented under linux. In contrast to other methods (python) I get !sometimes! a hang of the program when I try to call:

serial.open(QIODevice::ReadWrite)

我使用的示例从 http://qt-project.org/wiki/QtSerialPort (见下文)。 QSerialPortInfo工作正常,以便我可以在我打开设备之前搜索我的设备。问题出现在所有Qt 5. *系列。我目前使用5.3 beta从OpenSuse存储库。其他工具或方法证明设备正在工作(Windows或Python)。

I am using the example from http://qt-project.org/wiki/QtSerialPort (see below). The QSerialPortInfo is working properly so that I can search for my device before I open it. The problem appeared in all Qt 5.* series. I am currently using 5.3 beta from the OpenSuse repository. Other tools or methods proof, that the device is working (Windows or Python).

// Example use QSerialPortInfo
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
    qDebug() << "Name        : " << info.portName();
    qDebug() << "Description : " << info.description();
    qDebug() << "Manufacturer: " << info.manufacturer();

    // Example use QSerialPort
    QSerialPort serial;
    serial.setPort(info);
    if (serial.open(QIODevice::ReadWrite)) // Hang of the program
        serial.close();
}

运行python脚本:

Working python script:

com_port = 4
baud_rate = '9600'
pySerial = serial.Serial(com_port, baud_rate)

编辑:
我用调试器测试。似乎是一个问题Qt 5至少与linux。它看起来像与设备锁连接。

I tested with debugger. Seems to be a problem of Qt 5 at least with linux. It looks like connected with a device lock.

推荐答案

我不知道Papp先生抱怨什么;

I don't know what Mr. Papp is complaining about; I was able to reproduce your problem without any more information.

在Linux上,如果一个打开的QSerialPort实例的Qt进程异常终止,或者QSerialPort实例没有被销毁进程退出,然后锁文件挂起,并可能导致问题。一个陈旧的锁文件不应该导致这个问题;锁文件包含崩溃的应用程序的PID,并且新的应用程序实例应该识别没有具有该PID的进程存在,并且在创建新的PID之前立即删除该锁。 strace会以增加的退避时间反复显示有问题的锁定文件,因为新进程反复检查以查看失效的锁定文件是否已被删除等。所以,删除文件(例如,/var/lock/LCK..ttyS0);如果你运行的应用程序崩溃,你将拥有锁定文件。

On Linux, if the Qt process with an open QSerialPort instance terminates abnormally or the QSerialPort instance is otherwise not destroyed upon process exit, then the lock file hangs around and can cause a problem. A stale lock file shouldn't cause this problem; the lock file contains the crashed application's PID, and the new application instance should recognize that no process with that PID exists and delete the lock immediately before creating a new one. strace will show you the lock file in question repeatedly with increasing backoff time as the new process repeatedly checks to see if the stale lock file has been deleted or whatever. So, delete the file (for example, /var/lock/LCK..ttyS0); you will own the lock file if you ran the application that crashed.

一个随机的注释:如果你在Python中使用QSerialPort通过PyQt5(它的工作,方式!),请确保您已在Python解释器退出之前显式删除了QSerialPort实例。如果你正在IPython中操作端口,然后在退出之前执行%xdel portobject。

A random side note: if you are using QSerialPort in Python through PyQt5 (which does work, by the way!), be sure you have explicitly deleted the QSerialPort instance before the Python interpreter exits. If you're manipulating the port in IPython, then do "%xdel portobject" before exiting.

这是一个愚蠢的解决方法,但如果你使用一些其他机制,你没有你的程序的两个实例运行和使用相同的端口 - 或者如果你只是不在乎 - 你可以有一行代码删除该文件,然后打开端口。

It's a dumb workaround, but provided you are using some other mechanism to ensure that you don't have two instances of your program running and using the same port - or if you simply don't care - you could have a line of code delete that lock file before opening the port.

IMHO,Qt不应该首先模仿完全没有价值的Windows风格的保姆状态保护。我可以sudo rm -rf /和它发生!我甚至可以串口。鉴于这种无拘无束的,原始的,神的力量,我应该能够打开一个串口,不管我什么时候...

IMHO, Qt shouldn't be emulating completely worthless Windows style nanny state protections in the first place. I can sudo rm -rf / and it happens! I can even rm the serial port. Given such unfettered, primal, god like power, I should be able to open a serial port whenever and however I please...

这篇关于QSerialPort是导致程序停止(无限循环?)如果打开设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆