Is it a problem to turn off power to a running QSerialPort?

瘦欲@ 提交于 2019-12-11 17:42:05

问题


I'm developing an application with several serial ports. Each of these ports is handled by a different thread and has its own QSerialPort object. From a hardware point of view, they are connected hierarchicaly, meaning that there is one main device connected to the PC with a usb cable (1 COM port), to this main device there are several other devices connected, each of them having its own COM port. The main device can turn on/off the power supply to these child ports.

In the application, the ports are handled asynchronously. Each device object is running in its own loop. If its port is opened, it reads the incoming data. If the port is closed, it tries to open it in every loop until it succeeds. Each QSerialPort object handles errors on the errorOccurred signal. If it receives DeviceNotFoundError, PermissionError, ResourceError error, the port is closed (if it was opened) and the looping continues as described above.

The problem is that this serial communication part of the application is crashing (segmentation fault). I spent days finding the issue but with no results so far. To better understand what is going on, I wanted to ask here. Could it be a problem for QSerialPort if the main device turns off the power supply for the child ports while they're opened and are working? Or if the power supply is turned off while the child ports are being opened/closed or any other operation is being executed on them? (I don't want to include the specific executable code as it's a part of a bigger application and would be hard to make and executable from it. I'd like to discuss just the concepts described above if possible.)

Thanks for any help or ideas!

UPDATE

Creating of QSerialPort and putting it into a different thread:

QThread *t = new QThread(this)
SomeObject *o = new SomeObject(this);

o->moveToThread(t);
t->start();

Later in the SomeObject:

QSerialPort *port = new QSerialPort();

回答1:


Try to destroy port object before power off, and recreate it after power on

QSerialPort *port = new QSerialPort();
//init and use
//....
delete port;
port = nullptr;
//turn the power off

//turn the power on
port = new QSerialPort();


来源:https://stackoverflow.com/questions/52457145/is-it-a-problem-to-turn-off-power-to-a-running-qserialport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!