138 lines
3.1 KiB
C++
138 lines
3.1 KiB
C++
#ifndef GLOBAL_H
|
|
#define GLOBAL_H
|
|
|
|
#include "Qss.h"
|
|
#include <QWidget>
|
|
#include <QDebug>
|
|
#include <QMutex>
|
|
|
|
extern "C"{
|
|
#include "libmodbus/modbus.h"
|
|
}
|
|
|
|
typedef struct {
|
|
int addr;
|
|
float val1;
|
|
QString time;
|
|
}CapData;
|
|
|
|
|
|
typedef struct{
|
|
uint16_t addr;
|
|
uint16_t len;
|
|
|
|
}Req;
|
|
|
|
typedef struct T_Config{
|
|
QString com;
|
|
int addr;
|
|
int rate;
|
|
QList<Req> req;
|
|
}Config;
|
|
|
|
|
|
class ASyncReadData :public QSSASyncProcess{
|
|
public:
|
|
ASyncReadData(QWidget *parent)
|
|
:QSSASyncProcess(parent){
|
|
|
|
}
|
|
~ASyncReadData(){
|
|
if(mMod)
|
|
modbus_close(mMod);
|
|
}
|
|
void Stop(){
|
|
mRuning = false;
|
|
}
|
|
void AddMonitor(QString addr){
|
|
|
|
}
|
|
void AddConfig(Config *c){
|
|
if(nullptr != c){
|
|
for(auto itr = mConf.begin();itr != mConf.end();
|
|
itr++){
|
|
if (c == *itr){
|
|
return;
|
|
}
|
|
}
|
|
this->mConf.append(c);
|
|
}
|
|
}
|
|
void Run(void *v) override{
|
|
// 读取1
|
|
float x = 0;
|
|
while(mRuning){
|
|
|
|
int it = 0;
|
|
for(;it < mConf.size();
|
|
it++){
|
|
auto itr = mConf.at(it);
|
|
uint16_t dat[10] = {0};
|
|
mMod = modbus_new_rtu(itr->com.toStdString().c_str(),
|
|
57600, 'N', 8, 1); //相同的端口只能同时打开一个
|
|
|
|
modbus_set_debug(mMod,true);
|
|
modbus_set_slave(mMod,itr->addr); //设置modbus从机地址
|
|
modbus_connect(mMod);
|
|
modbus_set_response_timeout(mMod,1000,1000);
|
|
QThread::msleep(300);
|
|
|
|
for(auto s = 0;s < itr->req.length();s++){
|
|
|
|
|
|
QThread::msleep(1000);
|
|
int ret = modbus_read_registers(mMod,itr->req[s].addr,itr->req[s].len,dat);
|
|
|
|
mMux.lock();
|
|
if(mListData.contains(itr->addr)){
|
|
mListData[itr->addr]->push_front(new CapData{
|
|
itr->addr,
|
|
float(float(dat[0])),
|
|
});
|
|
}else{
|
|
mListData[itr->addr] = new QList<CapData *>;
|
|
mListData[itr->addr]->push_front(new CapData{
|
|
itr->addr,
|
|
float(float(dat[0])),
|
|
});
|
|
}
|
|
x += 3.1415*2 /256;
|
|
|
|
mMux.unlock();
|
|
|
|
}
|
|
modbus_close(mMod);
|
|
}
|
|
QThread::msleep(1000*pcom->rate);
|
|
}
|
|
// 读取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;
|
|
modbus_t* mMod;
|
|
bool mRuning;
|
|
QList<Config*> mConf;
|
|
};
|
|
|
|
extern ASyncReadData *gAsyncData;
|
|
|
|
|
|
|
|
#endif // GLOBAL_H
|