More Related Content
Similar to [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt (20)
[Webinar] QtSerialBus: Using Modbus and CAN bus with Qt
- 1. © Integrated Computer Solutions, Inc. All Rights Reserved
QtSerialBus: Using Modbus
and CAN bus with Qt
Jeff Tranter <jtranter@ics.com>
Integrated Computer Solutions, Inc.
- 2. © Integrated Computer Solutions, Inc. All Rights Reserved
Agenda
• What is CAN bus?
• What is Modbus?
• The QtSerialBus Module
• What Hardware and Platforms are Supported?
• Qt APIs
• Code Examples
• Demonstration
• Areas of Possible Future Work
• Summary
• References
- 3. © Integrated Computer Solutions, Inc. All Rights Reserved
What is CAN bus?
• Controller Area Network bus.
• Bus standard that allows microcontrollers and devices to communicate
with each other in applications without a host computer.
• Multi-master serial bus where all nodes are connected to each other
through a two wire bus.
• Message-based protocol.
• Originally designed for multiplexed electrical wiring within automobiles,
but also used in many other contexts.
- 8. © Integrated Computer Solutions, Inc. All Rights Reserved
What is Modbus?
• Serial communications protocol commonly used
for connecting industrial electronic devices.
• Allows communication among multiple devices
connected to the same network, often to connect
a supervisory computer with a remote terminal
unit in Supervisory Control and Data Acquisition
(SCADA) systems.
• Originally developed by Modicon in 1979 for use
with its programmable logic controllers (PLCs).
- 12. © Integrated Computer Solutions, Inc. All Rights Reserved
The QtSerialBus Module
• New module introduced as technical preview in Qt 5.6.0.
• Supports CAN bus and Modbus.
• May support other serial protocols in the future.
• Licensed like most Qt modules (LGPLv3, GPLv2, GPLv3 or commercial).
• git repo: http://code.qt.io/cgit/qt/qtserialbus.git
• Main developers and maintainers are Alex Blasche (CAN bus) and Karsten
Heimrich (Modbus) of The Qt Company.
- 13. © Integrated Computer Solutions, Inc. All Rights Reserved
What Hardware and Platforms are Supported?
For CAN bus, currently supports the following back ends:
• SocketCAN, which uses Linux sockets and open source drivers.
• Peak CAN, which supports PCAN adaptors from PEAK-System Technik
GmbH.
• TinyCAN, with support for Tiny-CAN adapters from MHS Elektronik.
• VectorCAN, supporting Vector Informatik CAN adapters.
- 14. © Integrated Computer Solutions, Inc. All Rights Reserved
What Hardware and Platforms are Supported?
For Modbus:
• One implementation (not a plugin) which doesn't depend on any external
libraries.
• Uses Qt's QtSerialPort and networking APIs.
• Supports RTU (serial) and TCP (Ethernet) communications.
- 15. © Integrated Computer Solutions, Inc. All Rights Reserved
Qt APIs
• C++ only, no QML
• To add to qmake projects: QT += serialbus
• Module include file: #include <QtSerialBus>
• logging categories:
• qt.modbus (standard)
• qt.modbus.lowlevel (low-level packets)
- 16. © Integrated Computer Solutions, Inc. All Rights Reserved
Qt APIs - CAN bus
Six classes:
QcanBus - Handles registration and creation of bus backends
QcanBusDevice::Filter - Defines a filter for CAN bus messages
QcanBusDevice - The interface class for CAN bus
QcanBusFactory - Factory class used as the plugin interface
QcanBusFrame - Container class representing a single CAN frame
QcanBusFrame::TimeStamp - Timestamp information with µsec precision
- 17. © Integrated Computer Solutions, Inc. All Rights Reserved
Qt APIs - Modbus
14 classes:
QModbusClient - The interface to send Modbus requests
QmodbusDataUnit - Container class representing entries in Modbus
register
QmodbusDevice - base class for QModbusServer and QModbusClient
QmodbusDeviceIdentification - Container class representing the physical
and functional description of a Modbus server
QmodbusExceptionResponse - Container class containing the function and
error code inside a Modbus ADU
QmodbusPdu - Abstract container class containing the function code and
payload that is stored inside a Modbus ADU
- 18. © Integrated Computer Solutions, Inc. All Rights Reserved
Qt APIs – Modbus (cont'd)
QmodbusRequest - Container class containing the function code and
payload that is stored inside a Modbus ADU
QmodbusResponse - Container class containing the function code and
payload that is stored inside a Modbus ADU
QmodbusReply - Contains the data for a request sent with a
QModbusClient derived class
QmodbusRtuSerialMaster - Represents a Modbus client that uses a serial
bus for its communication with the Modbus server
QmodbusRtuSerialSlave - Represents a Modbus server that uses a serial
port for its communication with the Modbus client
- 19. © Integrated Computer Solutions, Inc. All Rights Reserved
Qt APIs – Modbus (cont'd)
QmodbusServer - The interface to receive and process Modbus requests
QmodbusTcpClient - The interface class for Modbus TCP client device
QmodbusTcpServer - Represents a Modbus server that uses a TCP server
for its communication with the Modbus client
- 20. © Integrated Computer Solutions, Inc. All Rights Reserved
Code Examples - CAN bus
Basic Steps:
1. Create a device, specifying plugin and device name.
2. Connect.
3. Create data frames.
4. Send data frames.
5. Disconnect when done.
QCanBusDevice emits signals: errorOccurred, framesReceived, framesWritten,
stateChanged. To receive frames, connect to signal framesReceived.
- 21. © Integrated Computer Solutions, Inc. All Rights Reserved
Code Examples - CAN bus
// Create device.
QCanBusDevice *device = QCanBus::instance()->createDevice("socketcan", "vcan0");
if (device != nullptr) {
qDebug() << "Created device, state is:" << device->state();
} else {
qFatal("Unable to create CAN device.");
}
// Connect.
if (device->connectDevice()) {
qDebug() << "Connected, state is:" << device->state();
} else {
qDebug() << "Connect failed, error is:" << device->errorString();
}
- 22. © Integrated Computer Solutions, Inc. All Rights Reserved
Code Examples - CAN bus (cont'd)
// Create a data frame.
QCanBusFrame frame(QCanBusFrame::DataFrame, "12345");
// Send it.
if (device->writeFrame(frame)) {
qDebug() << "Wrote frame, state is:" << device->state();
} else {
qDebug() << "Write failed, error is:" << device->errorString();
}
// Disconnect.
device->disconnectDevice();
qDebug() << "Disconnected, state is:" << device->state();
- 23. © Integrated Computer Solutions, Inc. All Rights Reserved
Code Examples - CAN bus
On Linux there is a virtual CAN driver for testing purposes which can be
loaded and created as below:
sudo modprobe can
sudo modprobe can_raw
sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
ip link show vcan0
3: vcan0: <NOARP,UP,LOWER_UP> mtu 16 qdisc noqueue state UNKNOWN
link/can
- 26. © Integrated Computer Solutions, Inc. All Rights Reserved
Code Examples - Modbus
• Unlike CAN bus which is peer to peer, Modbus is client/server.
• Client sends request and gets response from server.
• Master/Slave arrangement where the Master is a Client and the Slave is a
Server.
• Confusing terminology: there is a single Modbus client (master) and
multiple Modbus servers (slaves).
• Support for serial devices and TCP network devices.
- 27. © Integrated Computer Solutions, Inc. All Rights Reserved
Code Examples - Modbus
QObject
QModbusDevice
QModbusClient QModbusServer
QModbusSerialMaster QModbusTcpClient QModbusRtuSerialSlave QModbusTcpServer
- 28. © Integrated Computer Solutions, Inc. All Rights Reserved
Code Examples - Modbus
Basic steps for a TCP client (others are similar):
1. Create a QModbusTcpClient()
2. Set connection parameters with setConnectionParameter()
3. Connect using connectDevice()
4. Call as needed:
sendRawRequest()
sendReadRequest()
sendReadWriteRequest()
sendWriteRequest()
4. When done, call disconnectDevice()
- 29. © Integrated Computer Solutions, Inc. All Rights Reserved
Code Examples - Modbus
// Create device.
QModbusTcpClient *device = new QModbusTcpClient();
if (device != nullptr) {
qDebug() << "Created device, state is:" << device->state();
} else {
qFatal("Unable to create Modbus TCP client device.");
}
// Set connection parameters. Defaults to local host port 502.
// Instead use TCP port 1502 as it is non-privileged.
device->setConnectionParameter(QModbusDevice::NetworkPortParameter, 1502);
// Connect.
if (device->connectDevice()) {
qDebug() << "Connected, state is:" << device->state();
} else {
qDebug() << "Connect failed, error is:" << device->errorString();
}
- 30. © Integrated Computer Solutions, Inc. All Rights Reserved
Code Examples – Modbus (cont'd)
// Create ADU.
QVector<quint16> data(4);
QModbusDataUnit adu(QModbusDataUnit::Coils, 1, data);
// Send read request to a server at address 1.
QModbusReply *reply = device->sendReadRequest(adu, 1);
if (reply != nullptr) {
qDebug() << "Sent read request, state is:" << device->state();
qDebug() << reply;
} else {
qDebug() << "Send of read request failed, error is:" << device->errorString();
}
// Disconnect.
device->disconnectDevice();
qDebug() << "Disconnected, state is:" << device->state();
- 35. © Integrated Computer Solutions, Inc. All Rights Reserved
Areas of Possible Future Work
• APIs final in Qt 5.8.0
• Support/plugins for more hardware back ends
• Higher level protocols?
- 37. © Integrated Computer Solutions, Inc. All Rights Reserved
References
1. https://en.wikipedia.org/wiki/CAN_bus
2. https://en.wikipedia.org/wiki/Modbus
3. https://doc-snapshots.qt.io/qt5-dev/qtserialbus-index.html
4. http://code.qt.io/cgit/qt/qtserialbus.git/
5. http://www.modbus.org
6. http://opengarages.org
7. ftp://ftp.ics.com/pub/pickup/qtserialbusexamples.zip
- 39. © Integrated Computer Solutions, Inc. All Rights Reserved
QtSerialBus: Using Modbus
and CAN bus with Qt
Jeff Tranter <jtranter@ics.com>
Integrated Computer Solutions, Inc.