Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Compiling libmodbus on the Kylin Operating System and Setting Up a Development Environment

Tech May 15 1

To implement ModbusTcp and Modbus232/485 communication on the Kylin operating system, use the libmodbus library. This article details compiling the libmodbus library on the specified Kylin system and migrating the modbusTcpDemo project.

Related blogs include:

  • Qt modbus485 Debugging Tool (v1.3.0)
  • Qt modbusTcp Debugging Tool (v1.0.0)
  • Delta PLC Development Notes (I): Connecting to Delta PLC via 485 and Ethernet
  • Delta PLC Development Notes (II): Setting Host Communication Parameters to RTU and Establishing Communication
  • Seimens PLC Development Notes (I): Introduction to PLCs, S1200 Series Wiring, Programming, Downloading, and Simulation
  • libModbus Library Development Notes (I): Introduction, Compilation, and Basic Project Template
  • libModbus Library Development Notes (II): Introduction, Compilation, ModbusTcp Connection to Delta PLC, and Basic Template
  • Kylin System Development Notes (XIV): Compiling libmodbus Library, Setting Up a Development Environment, and Migrating Test Demo
  • Qt Siemens PLC Debugging Simulation Tool (v1.6.0)

The demo for modbus232/485 is not migrated here; refer to related blogs for migration steps, which are similar to those described here.

Kylin System Version

libModbus Overview

libmodbus is a free software library that sends/receives data according to the Modbus protocol. It is written in C and supports RTU (serial) and TCP (Ethernet) communication.

Compiling libmodbus (Kylin V10)

Step 1: Download and Extract https://github.com/stephane/libmodbus

Step 2: Configure

cd

cd work/src/libmodbus-3.1.6
cCd libmodbus-3.1.6
ls

For dynamic library:

./configure --prefix=/home/yang/work/src/libmodbus-3.1.6/install

For static library:

./configure --prefix=/home/yang/work/src/libmodbus-3.1.6/installStatic --enable-static

Step 3: Compile with make

make -j16

Check with single-threaded compilation.

Step 4: Install with make install

make install

Step 5: Verify files Compared to Windows, .a becomes .so, indicating a dynamic library. Static libraries can also be compiled:

Step 6: Install to System To avoid linking isues at runtime, reinstall into the system:

./configure
make -j8
make
sudo make install
sudo ldconfig

Obtaining the IP Adress of the Delta PLC (Note: Three Delta units were tested, and the default address was 192.168.1.5)

Step 1: Download and Install COMMGR Download the latest communication software from the Delta website. Website: https://downloadcenter.delta-china.com.cn/DownloadCenter?v=1&CID=06&itemID=060301&downloadID=AS%E7%B3%BB%E5%88%97&dataType=8&sort_expr=cdate&sort_dir=DESC

Download the latest exploration driver software:

Step 2: Detect Delta PLC Address Open the software and click "Search". The computer must be on the same network segment as the PLC, typically the same subnet:

After searching, the following result is obtained:

This provides the network address of the PLC.

Modularization

Adding compatibility for Kylin system on Windows:

modbusTcpManager.pri

INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD

HEADERS += 
    $$PWD/ModbusTcpManager.h

SOURCES += 
    $$PWD/ModbusTcpManager.cpp

# libmodbus-3.1.6

win {
    INCLUDEPATH += $$PWD/libmodbus-3.1.6/include
    LIBS += -L$$PWD/libmodbus-3.1.6/lib -lmodbus -lws2_32
}
unix {
    INCLUDEPATH += $$PWD/libmodbus-3.1.6_kylin/include
    LIBS += -L$$PWD/libmodbus-3.1.6_kylin/lib -lmodbus
}

Demo

#ifndef MODBUSTCPMANAGER_H
#define MODBUSTCPMANAGER_H

#include <QObject>
#include <QMutex>

union RealByte {
    float f;
    quint8 buf[4];
};

union DRealByte {
    double f;
    quint8 buf[8];
};

// libmodbus
#include "modbus/modbus.h"
#include "modbus/modbus-version.h"
#include "modbus/modbus-rtu.h"
#include "modbus/modbus-tcp.h"

class ModbusTcpManager : public QObject
{
    Q_OBJECT
private:
    explicit ModbusTcpManager(QObject *parent = 0);

public:
    bool testEnvAndRtu();

public:
    static ModbusTcpManager *getInstance();

public:
    void setIp(const QString &ip);
    void setPort(const quint16 &port);
    void setTimeOutMs(int timeOutMs);

public:
    QString getIp() const;
    quint16 getPort() const;
    int getTimeOutMs() const;

signals:
    void signal_connected(bool connected);
    void signal_disConnected();

    void signal_readBitsResult(bool result, int startAddr, QList<bool> listBool);
    void signal_readIntsResult(bool result, int startAddr, QList<int> listInt);
    void signal_readDIntsResult(bool result, int startAddr, QList<int> listInt);
    void signal_readRealsResult(bool result, int startAddr, QList<float> listFloat);
    void signal_readDRealsResult(bool result, int startAddr, QList<double> listDouble);

    void signal_writeBitsResult(bool result, int startAddr, QList<bool> listBool);
    void signal_writeIntsResult(bool result, int startAddr, QList<int> listInt);
    void signal_writeDIntsResult(bool result, int startAddr, QList<int> listInt);
    void signal_writeRealsResult(bool result, int startAddr, QList<float> listFloat);
    void signal_writeDRealsResult(bool result, int startAddr, QList<double> listDouble);

public slots:
    void slot_start();
    void slot_stop();
    void slot_connect();
    void slot_disConnect();

    void slot_readBits(int startAddr, int size);
    void slot_readInts(int startAddr, int size);
    void slot_readDInts(int startAddr, int size);
    void slot_readReals(int startAddr, int size);
    void slot_readDReals(int startAddr, int size);

    void slot_writeBits(int startAddr, QList<bool> listBool);
    void slot_writeInts(int startAddr, QList<int> listInt);
    void slot_writeDInts(int startAddr, QList<int> listInt);
    void slot_writeReals(int startAddr, QList<float> listFloat);
    void slot_writeDReals(int startAddr, QList<double> listDouble);

private:
    static ModbusTcpManager *_pInstance;
    static QMutex _mutex;

private:
    QString _ip;
    quint16 _port;
    int _timeOutMs;

private:
    bool _running;
    bool _connected;

private:
    modbus_t *_pModbus;

};

#endif // MODBUSTCPMANAGER_H

Demo Demonstration

Connecting to Delta PLC for testing:

Project Template v1.1.0

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.