Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

3,905 views

Published on

Slides from the QtSerialBus: Using Modbus and CAN bus with Qt webinar given on December 1, 2016 by Jeff Tranter, Qt Consulting Manager at ICS.

Published in: Software
  • I've been struggling with maths for the past 2 years now. In the first mock paper I took, I got a 'U' grade. At this point, I felt hopeless. How was I ever going to pass my maths?! I did a Google search and came across Jeevan's stuff on Youtube. I was in a desperate situation so I had no choice but to purchase your material... Jeevan's resources are amazing. They have really helped me out. I am much more confident with my maths and I am able to answer past papers within the time limit. I recently had a mock exam at school and I achieved a 'D' grade! A massive improvement since my last score. If I continue to make this progress, there is no doubt that I will achieve my pass grade in my final exam. Thanks for your amazing resources Jeevan. They are so much better than the other revision guides I wasted money on! ➤➤ http://ishbv.com/jeevan91/pdf
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Even better than Qt documentation. Thanks
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

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

  1. 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. 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. 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.
  4. 4. © Integrated Computer Solutions, Inc. All Rights Reserved What is CAN bus?
  5. 5. © Integrated Computer Solutions, Inc. All Rights Reserved What is CAN bus?
  6. 6. © Integrated Computer Solutions, Inc. All Rights Reserved What is CAN bus?
  7. 7. © Integrated Computer Solutions, Inc. All Rights Reserved What is CAN bus?
  8. 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).
  9. 9. © Integrated Computer Solutions, Inc. All Rights Reserved What is Modbus?
  10. 10. © Integrated Computer Solutions, Inc. All Rights Reserved What is Modbus?
  11. 11. © Integrated Computer Solutions, Inc. All Rights Reserved What is Modbus?
  12. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  24. 24. © Integrated Computer Solutions, Inc. All Rights Reserved More Complete Example – CAN bus
  25. 25. © Integrated Computer Solutions, Inc. All Rights Reserved
  26. 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. 27. © Integrated Computer Solutions, Inc. All Rights Reserved Code Examples - Modbus QObject QModbusDevice QModbusClient QModbusServer QModbusSerialMaster QModbusTcpClient QModbusRtuSerialSlave QModbusTcpServer
  28. 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. 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. 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();
  31. 31. © Integrated Computer Solutions, Inc. All Rights Reserved More Complete Example - Modbus
  32. 32. © Integrated Computer Solutions, Inc. All Rights Reserved
  33. 33. © Integrated Computer Solutions, Inc. All Rights Reserved Documentation
  34. 34. © Integrated Computer Solutions, Inc. All Rights Reserved Qt Code Examples
  35. 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?
  36. 36. © Integrated Computer Solutions, Inc. All Rights Reserved Summary
  37. 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
  38. 38. © Integrated Computer Solutions, Inc. All Rights Reserved Questions?
  39. 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.

×