K
K
kuzubov2013-03-16 12:52:45
Python
kuzubov, 2013-03-16 12:52:45

Python + QT + virtual COM port

I made myself a lamp that is controlled by a computer via a radio channel. It's time to write software.
I chose a bunch of Python2.7 + QT for this. The control unit is plugged into USB and a virtual COM port appears in the system. I installed the pySerial library and pretty quickly wrote an application that simply sends messages to the lamp when a button is pressed.

The lamp itself also has manual control, so you need to listen to the COM port, and if a signal came from the lamp that the knob was turned there, then in the window on the computer change the state to a new one for the controls ...

And here the problem arose - a piece of the program that listens to COM -port is an endless cycle, and therefore the program hangs tightly in this cycle, the windows are not processed and nothing is spinning anywhere ...

In the controller itself, I have actions on interrupts, and during my work in DOS (in the distant 90s), I also wrote software actively using interrupts. But now is not the time and it seems to me that this is being solved in a different way ...

I have the Terminal program lying around for tests and it works as it should - that is, it listens to the port and displays what came from it, and all the buttons in the window work and you can send it back ... Here is something this is what I need to get ...

QUESTION:
How are such applications written when you need to listen to what came from the port, for example, and at the same time so that it does not hang up the program until it comes at all? And you also need to write to the port at a time when nothing is read from there - in order to send commands back and work out the window ...

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Desiderata, 2013-03-16
@Desiderata

You can listen to the COM port in a separate thread, and send a signal to the program interface when the lamp state changes.

V
Vladimir, 2013-03-16
@noonv

can be in a separate thread, or you can use non-blocking access.

A
agmt, 2013-03-25
@agmt

You can use QSerialPort. Well, or just QSocketNotifier, which will emit a signal when data is ready for reading.

S
smartass111, 2013-03-25
@smartass111

I did it based on terminal.py (an example from the serial library) - a class with a reader method,
which while (1) hangs on the computer port and parses bytes from there .
The method is launched in a separate thread
. Working with some objects from inside the thread is mandatory with lock.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question