[webinar] qtserialbus: using modbus and can bus with qt

39
© Integrated Computer Solutions, Inc. All Rights Reserved QtSerialBus: Using Modbus and CAN bus with Qt Jeff Tranter <[email protected]> Integrated Computer Solutions, Inc.

Upload: ics

Post on 16-Apr-2017

804 views

Category:

Software


23 download

TRANSCRIPT

Page 1: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

QtSerialBus: Using Modbus and CAN bus with Qt

Jeff Tranter <[email protected]>Integrated Computer Solutions, Inc.

Page 2: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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

Page 3: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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.

Page 4: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

What is CAN bus?

Page 5: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

What is CAN bus?

Page 6: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

What is CAN bus?

Page 7: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

What is CAN bus?

Page 8: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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).

Page 9: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

What is Modbus?

Page 10: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

What is Modbus?

Page 11: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

What is Modbus?

Page 12: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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.

Page 13: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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.

Page 14: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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.

Page 15: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

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

Page 16: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

Qt APIs - CAN bus

Six classes:QcanBus - Handles registration and creation of bus backendsQcanBusDevice::Filter - Defines a filter for CAN bus messagesQcanBusDevice - The interface class for CAN busQcanBusFactory - Factory class used as the plugin interfaceQcanBusFrame - Container class representing a single CAN frameQcanBusFrame::TimeStamp - Timestamp information with µsec precision

Page 17: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

Qt APIs - Modbus

14 classes:QModbusClient - The interface to send Modbus requestsQmodbusDataUnit - Container class representing entries in Modbus registerQmodbusDevice - base class for QModbusServer and QModbusClientQmodbusDeviceIdentification - Container class representing the physical and functional description of a Modbus serverQmodbusExceptionResponse - Container class containing the function and error code inside a Modbus ADUQmodbusPdu - Abstract container class containing the function code and payload that is stored inside a Modbus ADU

Page 18: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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 ADUQmodbusResponse - Container class containing the function code and payload that is stored inside a Modbus ADUQmodbusReply - Contains the data for a request sent with a QModbusClient derived classQmodbusRtuSerialMaster - Represents a Modbus client that uses a serial bus for its communication with the Modbus serverQmodbusRtuSerialSlave - Represents a Modbus server that uses a serial port for its communication with the Modbus client

Page 19: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

Qt APIs – Modbus (cont'd)

QmodbusServer - The interface to receive and process Modbus requestsQmodbusTcpClient - The interface class for Modbus TCP client deviceQmodbusTcpServer - Represents a Modbus server that uses a TCP server for its communication with the Modbus client

Page 20: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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.

Page 21: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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(); }

Page 22: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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();

Page 23: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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 cansudo modprobe can_rawsudo modprobe vcansudo ip link add dev vcan0 type vcansudo ip link set up vcan0ip link show vcan03: vcan0: <NOARP,UP,LOWER_UP> mtu 16 qdisc noqueue state UNKNOWN link/can

Page 24: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

More Complete Example – CAN bus

Page 25: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

Page 26: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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.

Page 27: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

Code Examples - Modbus

QObject

QModbusDeviceQModbusClient QModbusServer

QModbusSerialMaster QModbusTcpClient QModbusRtuSerialSlave QModbusTcpServer

Page 28: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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()

Page 29: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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(); }

Page 30: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© 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();

Page 31: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

More Complete Example - Modbus

Page 32: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

Page 33: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

Documentation

Page 34: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

Qt Code Examples

Page 35: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

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

Page 36: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

Summary

Page 37: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

References

1. https://en.wikipedia.org/wiki/CAN_bus2. https://en.wikipedia.org/wiki/Modbus3. https://doc-snapshots.qt.io/qt5-dev/qtserialbus-index.html4. http://code.qt.io/cgit/qt/qtserialbus.git/5. http://www.modbus.org6. http://opengarages.org7. ftp://ftp.ics.com/pub/pickup/qtserialbusexamples.zip

Page 38: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

Questions?

Page 39: [Webinar] QtSerialBus: Using Modbus and CAN bus with Qt

© Integrated Computer Solutions, Inc. All Rights Reserved

QtSerialBus: Using Modbus and CAN bus with Qt

Jeff Tranter <[email protected]>Integrated Computer Solutions, Inc.