March 1, 2012

luggi luggi
Lab Rat
59 posts

[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:

  1. switch(i)
  2.     {
  3.     case 0:
  4.         port->setParity(SerialPort::EvenParity);
  5.         break;
  6.     case 1:
  7.         port->setParity(SerialPort::OddParity);
  8.         break;
  9.     case 2:
  10.         port->setParity(SerialPort::NoParity);
  11.         break;
  12.     case 3:
  13.         port->setParity(SerialPort::MarkParity);
  14.         break;
  15.     case 4:
  16.         port->setParity(SerialPort::SpaceParity);
  17.         break;
  18.     }

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:

  1. QList<SerialPort> databits << SerialPort::Data5 << SerialPort::Data6 << SerialPort::Data7 << SerialPort::Data8;

And then:

  1. port->setDataBits(databits[i]);

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

4 replies

March 1, 2012

Tobias Hunger Tobias Hunger
Mad Scientist
3125 posts

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)?

March 1, 2012

Tobias Hunger Tobias Hunger
Mad Scientist
3125 posts

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.

March 5, 2012

luggi luggi
Lab Rat
59 posts

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

  1. QList<SerialPort::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:

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

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

March 6, 2012

kuzulis kuzulis
Hobby Entomologist
125 posts

2 luggi,

see examples in /tests/guiapp/optionsdialog.cpp

 
  ‹‹ How to use QuaZIP for zipping a file      Using signals and slots in a derived class from AbstractSerial (QSerialDevice) ››

You must log in to post a reply. Not a member yet? Register here!