Good QT Serial Port Library
Kye
Posts: 2,200
Does anyone know of a good QT serial port library that does not toggle the DTR or the RTS lines whenever the serial port settings are changed? What serial library is used for the command line prop-load tool?
I've been working on a GUI in QT for my CMUcam4 and it's surprisingly difficult to implement basic serial connectivity that I was able to do with an Arduino/Prop.
I need to be able to change the baud rate while the serial port is open. Right now I'm using the QtSerialPort library. However, the library toggles the DTR and RTS liens whenever settings are changed...
Thank for any help,
I've been working on a GUI in QT for my CMUcam4 and it's surprisingly difficult to implement basic serial connectivity that I was able to do with an Arduino/Prop.
I need to be able to change the baud rate while the serial port is open. Right now I'm using the QtSerialPort library. However, the library toggles the DTR and RTS liens whenever settings are changed...
Thank for any help,
Comments
I have been hacking on the propgcc loader recently for use on the Raspberry Pi. You can browse through the code here:http://code.google.com/p/propgcc/source/browse/#hg%2Floader%2Fsrc or just check out the entire propgcc project and have at it.
The loader does not use a cross platform serial library but rather has different serial handling code for each OS. osint_linux.c osint_cygwin.c etc etc.
It does not support changing baud of an open port but you might be able to hack it around to do that.
Why do that anyway? It's nothing to close the port and reopen at a different baud.
Do the libraries you were using David allow me to do this effectively?
Thanks,
In windows there are some settings to disable RTS/DTR flipping (links below). Linux and Mac are much more difficult.
The QExtSerialPort library has been reasonable except for needing the receive to be polled.
Windows communications reference: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363195(v=vs.85).aspx
DTR/RTS control from the DCB struct: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx
Cross-platform serial port operations is a bear. In Linux/Mac, you may be able to set the NOHUP attribute while making the change (not persistent in Mac). Another trick to try if that doesn't work is having a second process open a port in shared mode while you do your change in the main program.
--Steve
Do you know if QTExtSerialPort will work for what I need to do? I need to be able to change the baud rate on the port without the DTR or RTS lines changing.
Thanks,
It works in the SimpleIDE 0-8-5 terminal. There is a baud-rate box in the terminal that can be changed at any time.
I tested changing baud-rates just now with Windows7 and Linux. My Mac box is turned off to save energy - maybe someone else can check that. I don't remember ever having such trouble with the Mac, but I could be wrong.
Thanks,
I spawn the child thread in this section...
And then the child preforms I/O with this code.
Anything come to mind that is wrong? (Please ignore the forum software removing and adding spaces randomly).
Specifically, the child thread calls read and never returns from the function... The problem is, the child thread never calls read until it knows that bytes are available by first calling bytes available. The writing code appears to have no problems.
What's weird is if I make the serial port event driven the code immediately crashes hard and has to close. I'm not sure why that happens...
Thanks,
---
Why is working with serial ports so hard on a PC... I've spent 3 days trying to do what was trivial on an Arduino or Propeller.
To draw on a widget, you need to set up a signal/slot function and emit the signal.
Yes, like I said polling is the only reliable way with QExtSerialPort. I've never seen serial port event handling work on windows with QExtSerialPort.
Look at the PortListener files below. You'll note that PortListener has a run() function that polls for data in the receiver and "emits" a signal that is setup to be handled by the console.cpp code below.
It is common in a multi-tasking OS application to have these "producer/consumer" relationships when there is lots of overhead that just can't happen at "interrupt" time. In this case the producer (PortListener::run() ... emit ...) is a tiny thread that lives outside the main context of Qt and emits the signal updateEvent which becomes the consumer slot PortListener::updateReady(). The consumer calls Console::updateReady().
http://code.google.com/p/propside/source/browse/propside/PortListener.cpp?name=spinside
http://code.google.com/p/propside/source/browse/propside/PortListener.h?name=spinside
All the drawing work is done here:
http://code.google.com/p/propside/source/browse/propside/console.cpp?name=spinside
In this case serial port handling is easier on Propeller and other devices because there is no GUI.
An alternative to Qt is to stick with .net or delphi on Windows ... that's essentially what Parallax has done until now. I know that Chip has wrestled with this stuff too, and limiting to one platform reduces the complexity. Chip's desire for self-contained computing environment for example would simplify this greatly. I've called this idea the ApocalyChips, but it sounds practical in light of these serial-port struggles.
Getting buttons and message prompts for the GUI was so much easier however. It just worked.
Thank you,
Not trying to be condescending or anything. Empathy was my goal - guess that didn't come through.
It was hard to tell if the approach you were using was correct, so I generalized. Hope the example helps.
I can do two things to solve this problem...
I can create a program you like have where the child thread is always alive and working or I just make the master thread do everything and sprinkle processEvents calls in the lowest level code for the CMUcam4 interface.
My goal with the GUI I am doing right now is for it to be simple... so, I'm going to try option 2 first.
Thanks,
Fundamentally, I guess we are not using these signal lines for their intended purpose. Not that the propeller is the only one to do this (picaxe have similar issues).
When writing code I've come to the reluctant conclusion that it is best to use just Tx and Rx and not use the handshaking lines at all. Poll for data regularly (either on the prop or on the PC), and have wakeup bytes at the beginning of packets. On the picaxe forum there were many discussions around radio and using 10101010 wakeup bytes (lots of ascii U's) to bias the volts prior to sending data. And of course with radio you need another channel to send handshaking signals so it is easiest not to. Things like a "reset" can still be sent as a packet of data rather than by toggling a line.
My PC serial drivers tend to be a little unorthodox as a result - instead of waiting for a serial "event" I set up a clock and check for bytes on a regular basis, and if there are bytes, find out how many and retrieve that number of bytes. Wiring cables are simpler too if there are only 3 wires.
But I guess you already have written code based on handshaking lines?
The GUI is coming out really nicely. QT is amazing. I wish, however, that they didn't move their website and kill all of their links.
That's good news. Did you try http://qt-project.org/ ?
Yeah. Not their fault really. Qt was started by TrollTech which was bought by Nokia. Nokia is now gutted by Microsoft. Who of course do not like cross-platform tools. So Qt is now farmed out to Digia.
I do hope Qt can survive all this turmoil.
Regarding the "need to poll"... I manage happily with this code for many things. I connect the readRead() signal to my handling function, and away I go. BUT, this fails when I need better response time than about 1.3 seconds. (embedded Linux on 600MHz BeagleBone)
Waiting for the signal, as tidy as it is, was inadequate for a particular situation when connecting to my microcontroller. For faster response time (I needed < 85ms) I had to resort to polling as shown here. Best do this in another QThread, too.
So, that is how it works for me. Hope it helps you, or others.
I still haven't fix the threading issue yet. I think it has to do with the way I created the serial port.
Thanks,
ErNa
2 years ago... wow, time flies...