Not plotting serial data

57 views
Skip to first unread message

Phillip Gavino

unread,
Aug 26, 2022, 11:11:10 AM8/26/22
to pyqtgraph
Hello,

I am currently trying to plot manipulated joystick serial data from an Arduino. I am running into the problem that I can create the graph canvas, but there is no real-time plotting occurring.
aiming.ino
aim.py

Patrick

unread,
Aug 28, 2022, 8:56:52 PM8/28/22
to pyqtgraph
Hi,

Taking a quick look, you are calling your _update() method once during initialisation of the plot window, and then that's it. If everything else is working, it will only collect the first data point.
You need to be calling your _update() method in a background thread or using a QTimer. Take a look at the examples (eg. "updating plot" in Basic Plotting) which uses a QTimer to continuously call the update method. Specifically, the lines are:

timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(50)

If you go the timer route you may also want to change your pass inside the while loop to a return so that the Qt thread doesn't get locked up when serial data isn't present.

Another tip would be to think about "blocking the pipes" with your flow of serial data. That is, if your arduino is trying to continuously pump data at some rate, you should try to handle it at that rate on the computer side. Either call your QTimer more often than the arduino is sending data, and/or in your _update() method, run a loop to completely drain all waiting serial data before returning. Sometimes this isn't a problem, but I find Windows in particular has some terrible serial buffering/performance issues that can cause strange behaviour if your thread stalls for a bit for whatever reason.

You may eventually also want to add some try/except blocks to handle failures reading from the serial device, or corruption etc that stops you from decoding the two floats from the received string.

Patrick
Reply all
Reply to author
Forward
0 new messages