S
S
Slavka2016-04-19 12:43:30
C++ / C#
Slavka, 2016-04-19 12:43:30

Serial port communication method?

In general, you need to exchange information with a remote device using the MODBUS protocol, this protocol involves sending commands and receiving a response. I will send a lot of commands. To work with the port I use QSerialPort. This class has a slot that accepts data. All that came to my mind and I implemented it before it was announced

enum{
  some_comand, //нормальные имена отражающие суть
  some_comand1,
 // other comand
};

made a global variable command and assigned its constant to it when sending the command, and in the receive handler, separated the data reception by this constant using switch(command) or if.. else if.
But is there a smarter option?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Cheremisin, 2016-04-19
@leahch

Look towards ready-made libraries:
libmodbus.org
www.freemodbus.org

A
Alexander Ananiev, 2016-04-19
@SaNNy32

If you write in C, then it's better to do something like in libmodbus.
If you write in C ++, you can build a class hierarchy for sending / receiving data.

A
abukirev, 2016-04-19
@abukirev

If you are already using Qt / C ++, then perhaps it is better to make a certain hierarchy of package classes. The data can be wrapped in a QByteArray and convenient functions for working with the package can be implemented - its creation / formation and its reading / parsing. For quick debugging and expansion, it's worth it.
You can also look at the Qt Modbus module, if the version of Qt you are using allows.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question