qt-quickqtserialport

Automatic Serial Connection


I'm making a Qt Quick project which involves creating a serial connection with qserialport, i was wondering if it's possible to make the serial settings(baud rate, data bits, parity) automatically adapt to the serial device. Could someone help me with this?


Solution

  • That will be a tall order. It may be possible, but not recommended by the Qt people. Here is what the QSerialPort documentation for the "FramingError" status says:

    Framing error detected by the hardware while reading data.
    This value is obsolete. We strongly advise against using 
    it in new code.
    

    All of the serial error statuses have that same warning. I have no clue why they would be obsolete, and what would replace those status bits, but if you ignore the warning, there may be a inelegant solution.

    I have implemented auto-baud on a number of devices, but each time I had better access to the hardware than QML allows. The easiest approach is when a timer can be programed onto the pin (a common capability of a MCU) to measure the rate directly, but that is probably not available in your hardware. The fall-back approach is to monitor serial-port errors and respond to them intelligently. "Framing error" is the most useful, along with "Parity error".

    My algorithm starts by setting the baud rate to the highest expected value. That way, the errors come fast, and you can respond to them quicker. If I get errors at that rate, then I try the next slower baud-rate. I count errors over a period of time (about ten character periods), and use a threshold to determine whether to change the rate, so that an occasional error doesn't cause an unwanted change in rate.

    But I only ever changed the baud-rate, and have always used eight data-bits, one stop-bit and odd-parity. Trying to add all of those additional variables to the algorithm would be pretty daunting. If you could ignore parity errors until you had the correct rate, and then cycle through the parity settings, then maybe you could accommodate parity as well. But the number of data-bits would be very difficult to determine.

    I know that this doesn't help much, but I hope that it could be a start. Good luck.