[SOLVED] Lighter setup for serial port parameters (qSerialDevice)



  • Hey there

    If I want to save the last serial port parameters to initialize this port at the next program start, I used always big switch statements like this one:

    @switch(i)
    {
    case 0:
    port->setParity(SerialPort::EvenParity);
    break;
    case 1:
    port->setParity(SerialPort::OddParity);
    break;
    case 2:
    port->setParity(SerialPort::NoParity);
    break;
    case 3:
    port->setParity(SerialPort::MarkParity);
    break;
    case 4:
    port->setParity(SerialPort::SpaceParity);
    break;
    }@

    Is it possible to save these different serial port parameters into a QList or something similar, so I would be able to reduce all of this into one line? Something like this:

    @QList<SerialPort> databits << SerialPort::Data5 << SerialPort::Data6 << SerialPort::Data7 << SerialPort::Data8;@

    And then:

    @port->setDataBits(databits[i]);@

    Unfortunately it does not work with a QList, or I haven't found the right method to do this.


  • Moderators

    Assuming that SerialPort::EvenParity, etc. are actually enums: Why don't you just cast those to an int, save that into "serialPort/parityIndex" (which should be surrounded by QLatin1String(...) by the way) and then call port->setParity(SerialPort::WhatEverEnum(value)?


  • Moderators

    PS: Since the enum values are not of type SerialPort the QList<SerialPort> databits thing can not work. You need to use the type of the enum, not the class containing it there.



  • Thx for the advice. As you said my fault was the type of the QList. I just needed to set the type to:

    @QListSerialPort::DataBits databits@

    with the DataBits enum for example.

    It is also possible to use the integer value. But in my application I use QComboBox to adjust the serialPort. Finally I save the index value of the QComboBox when the settings are applied. If I want to use these integers to set the serial port parameteres it gets a bit confusing because the enums have different values. For example:

    @ enum Parity {
    NoParity = 0,
    EvenParity = 2,
    OddParity = 3,
    SpaceParity = 4,
    MarkParity = 5,
    UnknownParity = -1
    };@

    Therefore it's anyway useful to fill a QList in the order like you want it, isn't it?



  • 2 luggi,

    see examples in /tests/guiapp/optionsdialog.cpp


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.