modbus_rtu_test/global.h

196 lines
5.3 KiB
C
Raw Normal View History

2023-09-27 17:30:27 +00:00
#ifndef GLOBAL_H
#define GLOBAL_H
#include "Qss.h"
#include <QWidget>
#include <QDebug>
#include <QMutex>
2023-10-02 16:24:09 +00:00
#include <QDateTime>
2023-09-27 17:30:27 +00:00
extern "C"{
#include "libmodbus/modbus.h"
}
2023-10-03 16:11:49 +00:00
#if defined(_MSC_VER) && (_MSC_VER >= 1600)
# pragma execution_character_set("utf-8")
#endif
2023-09-27 17:30:27 +00:00
typedef struct {
2023-09-30 04:06:01 +00:00
int addr;
2023-10-03 15:48:41 +00:00
uint16_t *val1;
uint16_t len;
2023-09-30 04:06:01 +00:00
QString time;
2023-10-02 16:24:09 +00:00
bool succ;
2023-09-27 17:30:27 +00:00
}CapData;
2023-09-30 04:06:01 +00:00
typedef struct{
uint16_t addr;
uint16_t len;
}Req;
2023-09-27 17:30:27 +00:00
typedef struct T_Config{
QString com;
2023-10-01 17:54:37 +00:00
int baurate;
2023-09-27 17:30:27 +00:00
int addr;
int rate;
2023-09-30 04:06:01 +00:00
QList<Req> req;
2023-09-27 17:30:27 +00:00
}Config;
class ASyncReadData :public QSSASyncProcess{
public:
ASyncReadData(QWidget *parent)
:QSSASyncProcess(parent){
2024-01-14 16:26:05 +00:00
mRuning = true;
2023-09-27 17:30:27 +00:00
}
~ASyncReadData(){
if(mMod)
2023-10-01 09:15:57 +00:00
modbus_close(mMod);
2023-09-27 17:30:27 +00:00
}
void Stop(){
mRuning = false;
}
void AddMonitor(QString addr){
}
2023-09-30 04:06:01 +00:00
void AddConfig(Config *c){
if(nullptr != c){
2023-10-01 09:15:57 +00:00
mMux.lock();
2023-09-30 04:06:01 +00:00
for(auto itr = mConf.begin();itr != mConf.end();
itr++){
if (c == *itr){
2023-10-01 09:15:57 +00:00
mMux.unlock();
2023-09-30 04:06:01 +00:00
return;
}
}
this->mConf.append(c);
2023-10-01 09:15:57 +00:00
mMux.unlock();
2023-09-30 04:06:01 +00:00
}
2023-09-27 17:30:27 +00:00
}
2023-10-04 16:30:14 +00:00
void Pause(){
mPause = true;
}
void Continue(){
mPause = false;
}
2023-09-27 17:30:27 +00:00
void Run(void *v) override{
2024-01-14 16:26:05 +00:00
qDebug()<<"start"<<mRuning;
2023-09-27 17:30:27 +00:00
// 读取1
float x = 0;
while(mRuning){
2023-10-01 09:15:57 +00:00
int count = 0;
2024-01-14 16:26:05 +00:00
2023-10-04 15:48:12 +00:00
2023-10-01 09:15:57 +00:00
for(int it = 0;it < mConf.size();it++) {
2023-09-30 04:06:01 +00:00
auto itr = mConf.at(it);
2023-09-27 17:30:27 +00:00
uint16_t dat[10] = {0};
2023-10-04 15:48:12 +00:00
if(mMod == nullptr){
mMod = modbus_new_rtu(itr->com.toStdString().c_str(),
itr->baurate, 'N', 8, 1); //相同的端口只能同时打开一个
modbus_set_debug(mMod,true);
modbus_set_slave(mMod,itr->addr); //设置modbus从机地址
modbus_connect(mMod);
modbus_set_response_timeout(mMod,3,0);
}
2023-09-27 17:30:27 +00:00
2023-09-30 18:23:57 +00:00
for(auto s = 0;s < itr->req.length();s++){
2023-10-04 15:48:12 +00:00
QThread::msleep(itr->rate);
2023-10-01 09:15:57 +00:00
int ret = modbus_read_registers(mMod,itr->req[s].addr,itr->req[s].len,dat);
mMux.lock();
2023-10-03 16:11:49 +00:00
QString log = QDateTime::currentDateTime().toString("yyyy-mm-dd hh:MM:ss ") + "发送数据:";
for(int i =0;i < req_length;i++){
log += QString::asprintf("0x%02x ",req[i]);
}
emit(Info(log));
log = QDateTime::currentDateTime().toString("yyyy-mm-dd hh:MM:ss ") + "接收数据:";
2023-10-04 15:48:12 +00:00
2023-10-03 16:11:49 +00:00
for(int i =0;i < resp_length;i++){
log += QString::asprintf("0x%02x ",rsp[i]);
}
2023-10-04 15:48:12 +00:00
if(ret < 0){
log += "超时";
}
2023-10-03 16:11:49 +00:00
emit(Info(log));
2023-10-02 16:24:09 +00:00
bool succ = true;
2023-10-01 09:15:57 +00:00
if(mListData.contains(itr->addr)){
2023-10-02 16:24:09 +00:00
if(ret < 0){
succ = false;
}
2023-10-03 15:48:41 +00:00
uint16_t *val = new uint16_t[itr->req[s].len];
memcpy(val,dat,itr->req[s].len*2);
2023-10-01 09:15:57 +00:00
mListData[itr->addr]->push_front(new CapData{
2023-10-01 17:54:37 +00:00
itr->req[s].addr,
2023-10-03 15:48:41 +00:00
val,
itr->req[s].len,
2023-10-03 03:49:35 +00:00
QDateTime::currentDateTime().toString("yyyy-mm-dd hh:MM:ss"),
2023-10-02 16:24:09 +00:00
true
2023-10-01 09:15:57 +00:00
});
}else{
2023-10-03 15:48:41 +00:00
uint16_t *val = new uint16_t[itr->req[s].len];
2023-10-01 09:15:57 +00:00
mListData[itr->addr] = new QList<CapData *>;
mListData[itr->addr]->push_front(new CapData{
2023-10-01 17:54:37 +00:00
itr->req[s].addr,
2023-10-03 15:48:41 +00:00
val,
itr->req[s].len,
2023-10-03 03:49:35 +00:00
QDateTime::currentDateTime().toString("yyyy-mm-dd hh:MM:ss"),
2023-10-02 16:24:09 +00:00
succ
2023-10-01 09:15:57 +00:00
});
}
mMux.unlock();
2023-09-30 18:23:57 +00:00
}
2023-10-01 09:15:57 +00:00
}
qDebug()<<"keep alive";
QThread::msleep(100);
2023-10-04 15:48:12 +00:00
2023-10-01 09:15:57 +00:00
}
2023-10-04 15:48:12 +00:00
modbus_close(mMod);
2023-09-27 17:30:27 +00:00
// 读取2
}
int TakeLast(int addr,CapData **p){
if(this->mListData.contains(addr)){
mMux.lock();
if(mListData[addr]->size() > 0){
*p = (CapData *)mListData[addr]->takeLast();
mMux.unlock();
return 0;
}else{
mMux.unlock();
return -1;
}
}else{
return -1;
}
}
QMap<int,QList<CapData *>*> mListData;
QMutex mMux;
2023-10-04 15:48:12 +00:00
modbus_t* mMod = nullptr;
2023-09-27 17:30:27 +00:00
bool mRuning;
2023-09-30 04:06:01 +00:00
QList<Config*> mConf;
2023-10-04 16:30:14 +00:00
bool mPause = false;
2023-09-27 17:30:27 +00:00
};
extern ASyncReadData *gAsyncData;
#endif // GLOBAL_H