#ifndef GLOBAL_H #define GLOBAL_H #include "Qss.h" #include #include #include extern "C"{ #include "libmodbus/modbus.h" } typedef struct { float val1; float val2; float val3; float val4; float val5; float val6; }CapData; typedef struct T_Config{ QString com; int addr; int rate; }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){ this->mConf.append(c); } void Run(void *v) override{ Config *pcom = (Config*)v; qDebug()<<"start"<com; // 读取1 float x = 0; while(mRuning){ for(auto itr = mConf.begin();itr != mConf.end(); itr++){ uint16_t dat[10] = {0}; uint16_t dat2[10] = {0}; uint16_t dat3[10] {0}; uint16_t dat4[4] = {0}; qDebug()<com<addr; 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); QThread::msleep(1000); int ret = modbus_read_registers(mMod,1,1,dat); mMux.lock(); qDebug()<addr)){ mListData[itr->addr]->push_front(new CapData{ float(float(dat2[0])), float(float(dat3[0])), float(float(dat3[1])), float(dat[0]), float(dat[1]), float(dat4[0]/10), }); }else{ mListData[itr->addr] = new QList; mListData[itr->addr]->push_front(new CapData{ float(float(dat2[0])), float(float(dat3[0])), float(float(dat3[1])), float(dat[0]), float(dat[1]), float(dat4[0]/10), }); } x += 3.1415*2 /256; mMux.unlock(); modbus_close(mMod); } // modbus_set_slave(mMod,49); //设置modbus从机地址 // modbus_connect(mMod); // modbus_set_response_timeout(mMod,1,1); // QThread::msleep(300); // modbus_write_register(mMod,1500,0x2011); // 解锁生产权限 // QThread::msleep(100); // int ret = modbus_read_registers(mMod,300,2,dat); // QThread::msleep(100); // ret = modbus_read_registers(mMod,0x8008,1,dat2); // QThread::msleep(100); // ret = modbus_read_registers(mMod,0x8001,3,dat3); // QThread::msleep(100); // ret = modbus_read_registers(mMod,107,3,dat4); // mMux.lock(); // qDebug()<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*> mListData; QMutex mMux; modbus_t* mMod; bool mRuning; QList mConf; }; extern ASyncReadData *gAsyncData; #endif // GLOBAL_H