[Interest] Is there a way to simulate serial port data?

Ch'Gans chgans at gna.org
Tue Mar 28 03:47:05 CEST 2017


On 28 March 2017 at 12:58, Murphy, Sean <smurphy at walbro.com> wrote:
>> What about writing a “virtual serial port” ?
>>
>> QSerialPort is a QIODevice, so one thing you can do is to replace it with a
>> custom QIODevice where you can write what you from e.g. a “console
>> widget” to evaluate what you want from your application.
>
> So you're suggesting when I want to debug my class I change this:
>   class mySerialPort : public QSerialPort {};
> to
>   class mySerialPort : public virtualSerialPort {};
> and recompile (where virtualSerialPort inherits from a QIODevice that I customize to do what I need to do)?
>
> Basically the features I need are:
> 1. I need to be able to inject a QByteArray into the class and have that emit the readyRead() signal
> 2. The readyRead() signal is connected to my parse() slot
> 3. in parse(), I need myClass::readAll() to come back with that same QByteArray that I injected in step #1
>
> It looks like maybe QBuffer (which I didn’t know existed) might be close to what I need... Ideally I was hoping to do it in a way that I didn't have to change any of the code of my serial port class, just to make sure I'm not introducing an error during this process...

First of all your parsing class should only do the parsing, so it
shouldn't derive from QSerialPort.
instead it should take a QSerialPort pointer as a parameter, eg:
QSerialPort port(...);
port.open(...)
Parser parser(&serialPort);
Result result = parser.parse();
or
Parser parser;
parser.setSerialPort(&port);

This way, you could then derive QSerialPort into a VirtualSerialPort,
and use it this way:
VirtualSerialPort port(...);
port.setData(byteArray);
port.SetSomeConfigurationParameter(...);
parser parser(&port);
Result result = parser.parse();

A parser is not a serial port (do not use derivation), a parser uses a
serial port (use data member).

Look for example at QXmlStreamReader, it can be used with any
QIODevice, including  QBuffer:
http://doc.qt.io/qt-5/qxmlstreamreader.html#setDevice


My 2 cents;

>
> Sean
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest



More information about the Interest mailing list