前言
西门子PLC、台达PLC、法兰克机床等等多年以前玩得比较多,有tcp/ip通讯也有modbus通讯,modbus又分为网络,485,232等。
医疗项目,焊接机器人项目,工控机床项目,数控项目,物联网项目用的挺多的,将关键的通信技术抽离出来,单独弄成工具。
需求
与各种其他行业仪器通讯软件开发的通讯核心模块,modbus功能模块,制作成调试工具,可满足一般情况的调试。
1.支持通过串口232,485端口进行通讯;
2.支持设置打开端口的参数;
3.支持写入bool,int,dInt,real,dReal参数类型数据;
4.支持读取bool,int,dInt,real,dReal参数类型数据;
5.增加10种主题风格切换(仅用于预选择主题风格);
Demo
工具下载地址
CSDN(0积分下载):https://download.csdn.net/download/qq21497936/19236703
QQ群:1047134658(点击“文件”搜索“plcModbus ”,群内与博文同步更新)
十种主题风格切换
相关头文件源码
PlcModbusMasterWidget.h
#ifndef PLCMODBUSMASTERWIDGET_H #define PLCMODBUSMASTERWIDGET_H #include <QWidget> #include <QThread> #include <QDateTime> #include <QElapsedTimer> #include <QHash> #include <QFile> #include <QMessageBox> #include <QAbstractButton> #include "ModbusManager.h" namespace Ui { class PlcModbusMasterWidget; } class PlcModbusMasterWidget : public QWidget { Q_OBJECT public: explicit PlcModbusMasterWidget(QWidget *parent = 0); ~PlcModbusMasterWidget(); protected slots: void slot_opened(bool opened); void slot_readBitsResult(bool result, int startAddr, QList<bool> listBool); void slot_readIntsResult(bool result, int startAddr, QList<int> listInt); void slot_readDIntsResult(bool result, int startAddr, QList<int> listInt); void slot_readRealsResult(bool result, int startAddr, QList<float> listFloat); void slot_readDRealsResult(bool result, int startAddr, QList<double> listDouble); void slot_writeBitsResult(bool result, int startAddr, QList<bool> listBool); void slot_writeIntsResult(bool result, int startAddr, QList<int> listInt); void slot_writeDIntsResult(bool result, int startAddr, QList<int> listInt); void slot_writeRealsResult(bool result, int startAddr, QList<float> listFloat); void slot_writeDRealsResult(bool result, int startAddr, QList<double> listDouble); private slots: void on_comboBox_writeType_currentIndexChanged(int index); void on_lineEdit_readStartAddr_textChanged(const QString &arg1); void on_lineEdit_writeStartAddr_textChanged(const QString &arg1); void on_lineEdit_readStartAddrHex_textChanged(const QString &arg1); void on_lineEdit_writeStartAddrHex_textChanged(const QString &arg1); protected slots: void slot_buttonClicked(int index); private slots: void on_pushButton_open_clicked(); void on_pushButton_close_clicked(); void on_pushButton_read_clicked(); void on_pushButton_write_clicked(); void on_pushButton_clearM_clicked(); private: Ui::PlcModbusMasterWidget *ui; QThread *_pModbusManagerThread; QElapsedTimer _elapsedTimer; QHash<int, QString> _hashInt2Str; }; #endif // PLCMODBUSMASTERWIDGET_H
ModbusManager.h
#ifndef MODBUSMANAGER_H #define MODBUSMANAGER_H #include <QObject> #include <QMutex> class ModbusManager : public QObject { Q_OBJECT private: explicit ModbusManager(QObject *parent = 0); public: bool testEnvAndRtu(); public: static ModbusManager *getInstance(); public: void setPortName(const QString &portName); void setBaud(int baud); void setParity(char parity); void setDataBit(int dataBit); void setStopBit(int stopBit); void setTimeOutMs(int timeOutMs); void setSlaveAddr(int slaveAddr); public: QString getPortName() const; int getBaud() const; char getParity() const; int getDataBit() const; int getStopBit() const; int getTimeOutMs() const; int getSlaveAddr() const; signals: void signal_opened(bool opened); 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_open(); void slot_close(); 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 ModbusManager *_pInstance; static QMutex _mutex; private: QString _portName; int _baud; char _parity; int _dataBit; int _stopBit; int _timeOutMs; int _slaveAddr; private: bool _running; bool _opened; }; #endif // MODBUSMANAGER_H