赞
踩
提示:以下是本篇文章正文内容,下面案例可供参考
qt5.1.1+vs2017
DIOandLightControl .h:
class DIOandLightControl : public QObject { Q_OBJECT public: explicit DIOandLightControl(QObject *parent = nullptr); signals: void signalComOpenCB(unsigned short portNum, unsigned char ucHardwareMajorVer, unsigned char ucHardwareMinorVer, unsigned char ucHardwareRevVer, unsigned char ucFirmwareMajorVer, unsigned char ucFirmwareMinorVer, unsigned char ucFirmwareRevVer, unsigned char ucError, unsigned int uiErrorId); void ReadResult_DI(BYTE DI,bool state);//将读取输入结果发送给主线程mainwindow void openprocess(); void workWarn(QString); private: static void ComOpenCB(LC_CALLBACK_ARG_T Arg); static void VerInfo (LC_CALLBACK_ARG_T Arg);//功能块执行后调用的程序块,理解为回调函数 public: HMODULE hDIO;//设备句柄 DIOLC_LibraryBaseInit_t DIOLC_LibraryBaseInit;//初始化,成功return 1 DIOLC_LibraryBaseDeinit_t DIOLC_LibraryBaseDeinit;//取消初始化,成功return 1 DIOLC_Process_t DIOLC_Process;//后台服务进程 DIOLC_OpenDevice_t DIOLC_OpenDevice;//控制板连接,成功return 除-1外其他数 DIOLC_CloseDevice_t DIOLC_CloseDevice;//取消连接,成功return 除-1外其他数 DIOLC_IsDeviceOpened_t DIOLC_IsDeviceOpened;//检查控制板是否连接,成功return 1 LC_SetPwmParams_t LC_SetPwmParams;//设置光源控制参数 LC_GetPwmParams_t LC_GetPwmParams;//读取当前光源参数 DIOLC_GetGeneralParam_t DIOLC_GetGeneralParam;//读取设备参数 DIOLC_SetGeneralParam_t DIOLC_SetGeneralParam;//设置设备参数 // DIO functions //读取输入状态 DIO_PollingReadDiBit_t DIO_PollingReadDiBit; DIO_PollingReadDiByte_t DIO_PollingReadDiByte; DIO_PollingReadDiWord_t DIO_PollingReadDiWord; //控制输出 DIO_PollingWriteDoBit_t DIO_PollingWriteDoBit; DIO_PollingWriteDoByte_t DIO_PollingWriteDoByte; DIO_PollingWriteDoWord_t DIO_PollingWriteDoWord; bool opendevice(); bool initSetting(); public slots: void ioopen(); void ioopen_two(); void lightconne(); void press(); void ReadDI(BYTE diByteIndex, BYTE diBitIndex); void WriteDO(BYTE doByteIndex, BYTE doBitIndex, BYTE doBitValue); void SetLightcH(int ChIdx,int triggermode,int lightvalue,int holdingtime,int softonoff); void disconnectlight(); }
MainWindow.h:
class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void RecevieResult_ReadDI(BYTE DI,bool result); void RecevieWarnMessage(QString); signals: void connectio(); void apply_ReadDI(BYTE diByteIndex, BYTE diBitIndex);//发送读DI指令信号,参数diByteIndex输入0,参数diBitIndex为读取的通道,如读取DI0,则输入apply_ReadDI(0,0) void apply_WriteDO(BYTE doByteIndex, BYTE doBitIndex, BYTE doBitValue);//发送写DO指令信号,参数doByteIndex输入0,参数doBitIndex为写入的通道, // 参数doBitValue为写入的状态,如置位DO0,apply_WriteDO(0,0,0) void SetLightControlPara(int ChIdx, int triggermode, int lightvalue, int holdingtime, int softonoff); //CHIDx 表示光源通道 //ch0:0x01 ch1:0x02,ch2:0x04,ch3:0x08 //triggermode 触发模式 0:软触发,1:高电平触发,2:上升沿触发延时关断 //lightvalue 光源亮度 设置值:0-100 //holdingtime 上升沿触发模式下的延时时间,单位:s ,输入0-60整数,高电平触发时设置0 //softonoff 软触发开关 void lightconnect(); void closelightconnect(); private slots: void on_pushButton_clicked(); int lightchannel(int index); void on_DIAutotext_clicked(); void ApplyDI(); void DIresultdisp(); void on_DOall_clicked(); void on_DOOUT0_clicked(); void on_DOOUT1_clicked(); void on_DOOUT2_clicked(); void on_DOOUT3_clicked(); void on_DOOUT4_clicked(); void on_DOOUT5_clicked(); void on_DOOUT6_clicked(); void on_DOOUT7_clicked(); void on_pushButton_4_clicked(); void on_pushButton_2_clicked(); void on_mode_currentIndexChanged(int index); void on_pushButton_3_clicked(); private: Ui::MainWindow *ui; void initUI(); void initIO(); DIOandLightControl * ioandlight; QThread *iothread; DIOandLightControl * ioandlight_ReadAndWrite; QThread *iothread_ReadAndWrite; QTimer *DItexttimer ; bool DIresult[8]; bool softtrigger; };
dioandlightcontrol.cpp:
DIOandLightControl::DIOandLightControl(QObject *parent) : QObject(parent) { qRegisterMetaType<BYTE>("BYTE"); } void DIOandLightControl::ComOpenCB(LC_CALLBACK_ARG_T Arg) { } void DIOandLightControl::VerInfo(LC_CALLBACK_ARG_T Arg) { } //读取环境变量 bool DIOandLightControl::opendevice() { hDIO = GetModuleHandle(TEXT("./NKIOLCLIBx64.dll")); if (hDIO == NULL) { hDIO = LoadLibrary(TEXT("./NKIOLCLIBx64.dll")); } if (hDIO == NULL) { return false; } else { return true; } } //IO及光源部分函数初始化、导入配置文件 bool DIOandLightControl::initSetting() { DIOLC_LibraryBaseInit = (DIOLC_LibraryBaseInit_t)GetProcAddress(hDIO, "DIOLC_LibraryBaseInit"); DIOLC_LibraryBaseDeinit = (DIOLC_LibraryBaseDeinit_t)GetProcAddress(hDIO, "DIOLC_LibraryBaseDeinit"); DIOLC_Process = (DIOLC_Process_t)GetProcAddress(hDIO, "DIOLC_Process"); DIOLC_OpenDevice = (DIOLC_OpenDevice_t)GetProcAddress(hDIO, "DIOLC_OpenDevice"); DIOLC_CloseDevice = (DIOLC_CloseDevice_t)GetProcAddress(hDIO, "DIOLC_CloseDevice"); DIOLC_IsDeviceOpened = (DIOLC_IsDeviceOpened_t)GetProcAddress(hDIO, "DIOLC_IsDeviceOpened"); LC_GetVerInfo = (LC_GetVerInfo_t)GetProcAddress(hDIO, "LC_GetVerInfo"); LC_SetPwmParams = (LC_SetPwmParams_t)GetProcAddress(hDIO, "LC_SetPwmParams"); LC_GetPwmParams = (LC_GetPwmParams_t)GetProcAddress(hDIO, "LC_GetPwmParams"); DIOLC_GetGeneralParam = (DIOLC_GetGeneralParam_t)GetProcAddress(hDIO, "DIOLC_GetGeneralParam"); DIOLC_SetGeneralParam = (DIOLC_SetGeneralParam_t)GetProcAddress(hDIO, "DIOLC_SetGeneralParam"); // DIO functions DIO_PollingReadDiBit = (DIO_PollingReadDiBit_t)GetProcAddress(hDIO, "DIO_PollingReadDiBit"); DIO_PollingReadDiByte = (DIO_PollingReadDiByte_t)GetProcAddress(hDIO, "DIO_PollingReadDiByte"); DIO_PollingReadDiWord = (DIO_PollingReadDiWord_t)GetProcAddress(hDIO, "DIO_PollingReadDiWord"); DIO_PollingWriteDoBit = (DIO_PollingWriteDoBit_t)GetProcAddress(hDIO, "DIO_PollingWriteDoBit"); DIO_PollingWriteDoByte = (DIO_PollingWriteDoByte_t)GetProcAddress(hDIO, "DIO_PollingWriteDoByte"); DIO_PollingWriteDoWord = (DIO_PollingWriteDoWord_t)GetProcAddress(hDIO, "DIO_PollingWriteDoWord" ); if ((NULL != DIOLC_LibraryBaseInit) && (NULL != DIOLC_LibraryBaseDeinit) && (NULL != DIOLC_Process) && (NULL != DIOLC_OpenDevice) && (NULL != DIOLC_CloseDevice) && (NULL != DIOLC_IsDeviceOpened) && (NULL != LC_GetVerInfo) && (NULL != LC_SetPwmParams) && (NULL != LC_GetPwmParams) && (NULL != DIOLC_GetGeneralParam) && (NULL != DIOLC_SetGeneralParam) && (NULL != DIO_PollingReadDiBit) && (NULL != DIO_PollingReadDiByte) && (NULL != DIO_PollingReadDiWord) && (NULL != DIO_PollingWriteDoBit) && (NULL != DIO_PollingWriteDoByte) && (NULL != DIO_PollingWriteDoWord)) { //读取配置文件realeas文件夹下的NP-6122-H1中配置文件 QSettings set(QCoreApplication::applicationDirPath() + "/select.ini", QSettings::IniFormat); QString filePath = QCoreApplication::applicationDirPath() + set.value("/SELECTED/ConfigPath").toString(); QSettings configSet(filePath, QSettings::IniFormat); configSet.value("/NKLC/Enabled").toBool(); filePath.replace(QString("/"), QString("\\")); QByteArray ba = filePath.toLocal8Bit(); char *ch = ba.data(); int result=DIOLC_LibraryBaseInit(ch); if(result!=-1) { return 1; } } else { return 0; } } //线程1为DIOLC_Process函数循环 void DIOandLightControl::ioopen() { connect(this,&DIOandLightControl::openprocess,this,&DIOandLightControl::press); int result=Mission_succeed; result=opendevice(); if(result!=Mission_succeed) { return; } result=initSetting(); if(result!=Mission_succeed) { return; } emit openprocess(); } void DIOandLightControl::press() { while (1) { DIOLC_Process(); Sleep(1); } } //线程2读取、写入状态 void DIOandLightControl::ioopen_two() { int result=Mission_succeed; result=opendevice(); if(result!=Mission_succeed) { return; } result=initSetting(); if(result!=Mission_succeed) { return; } // lightconne();// emit opened(); } void DIOandLightControl::lightconne() { DIOLC_OpenDevice(3,ComOpenCB); if(DIOLC_IsDeviceOpened(3,0x01)==0 || DIOLC_IsDeviceOpened(3,0x02)==0 || DIOLC_IsDeviceOpened(3,0x04)==0 || DIOLC_IsDeviceOpened(3,0x08)==0) { emit workWarn("光源控制连接失败"); } else { emit workWarn("光源控制连接成功"); } } void DIOandLightControl::ReadDI(BYTE diByteIndex, BYTE diBitIndex) { emit ReadResult_DI(diBitIndex, DIO_PollingReadDiBit(diByteIndex,diBitIndex)); } void DIOandLightControl::WriteDO(BYTE doByteIndex, BYTE doBitIndex, BYTE doBitValue) { DIO_PollingWriteDoBit (doByteIndex,doBitIndex,doBitValue); } void DIOandLightControl::SetLightcH(int ChIdx, int triggermode, int lightvalue, int holdingtime, int softonoff) { LC_SetPwmParams(0x01,ChIdx,triggermode,lightvalue,holdingtime,softonoff,VerInfo); } void DIOandLightControl::disconnectlight() { DIOLC_CloseDevice(3,ComOpenCB); }
mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); initUI(); DItexttimer = new QTimer; connect(DItexttimer,&QTimer::timeout,this,&MainWindow::ApplyDI); connect(DItexttimer,&QTimer::timeout,this,&MainWindow::DIresultdisp); for(int num=0;num<8;num++) { DIresult[num]=0; } softtrigger=0; ui->pushButton_3->setEnabled(0); } MainWindow::~MainWindow() { delete ui; } void MainWindow::initUI() { ui->tabWidget->hide(); ui->label->resize(400,200); ui->label->move(200,100); ui->label->setText("工控机型号:NP-6122-H1"); ui->pushButton->resize(100,70); ui->pushButton->move(300,300); } void MainWindow::on_pushButton_clicked()//start按钮 { ui->tabWidget->show(); ui->label->hide(); ui->pushButton->hide(); initIO();//启动IO线程,执行初始化等 } void MainWindow::initIO() { //IO线程1用来运行IO循环服务(process) iothread =new QThread; ioandlight= new DIOandLightControl; ioandlight->moveToThread(iothread); connect(this,&MainWindow::connectio,ioandlight,&DIOandLightControl::ioopen); connect(iothread, &QThread::finished, iothread, &QObject::deleteLater); //iothread->setPriority(QThread::TimeCriticalPriority); iothread->start(); //IO线程2用来接受控制指令 iothread_ReadAndWrite =new QThread; ioandlight_ReadAndWrite= new DIOandLightControl; ioandlight_ReadAndWrite->moveToThread(iothread_ReadAndWrite); connect(iothread_ReadAndWrite, &QThread::finished, iothread_ReadAndWrite, &QObject::deleteLater); connect(this,&MainWindow::connectio,ioandlight_ReadAndWrite,&DIOandLightControl::ioopen_two); connect(this,&MainWindow::apply_ReadDI,ioandlight_ReadAndWrite,&DIOandLightControl::ReadDI); connect(this,&MainWindow::apply_WriteDO,ioandlight_ReadAndWrite,&DIOandLightControl::WriteDO); connect(ioandlight_ReadAndWrite,&DIOandLightControl::ReadResult_DI,this,&MainWindow::RecevieResult_ReadDI); connect(ioandlight_ReadAndWrite,&DIOandLightControl::workWarn,this,&MainWindow::RecevieWarnMessage); connect(this,&MainWindow::SetLightControlPara,ioandlight_ReadAndWrite,&DIOandLightControl::SetLightcH); connect(this,&MainWindow::lightconnect,ioandlight_ReadAndWrite,&DIOandLightControl::lightconne); connect(this,&MainWindow::closelightconnect,ioandlight_ReadAndWrite,&DIOandLightControl::disconnectlight); iothread_ReadAndWrite->start(); emit connectio();//线程内初始化函数启动 } void MainWindow::RecevieWarnMessage(QString statebarmsg) { ui->statusBar->showMessage(statebarmsg,3000);//接受IO线程的返回信息 } void MainWindow::RecevieResult_ReadDI(BYTE DI,bool result) { DIresult[DI]=result; } int MainWindow::lightchannel(int index) { if(index==0) { return 0x01; } else if(index==1) { return 0x02; } else if(index==2) { return 0x04; } else if(index==3) { return 0x08; } } void MainWindow::on_DIAutotext_clicked() { static bool state=0; if(state==0) { ui->DI->setStyleSheet("background-color:red"); state=1; DItexttimer->start(300); } else { ui->DI->setStyleSheet("background-color:green"); state=0; DItexttimer->stop(); } } void MainWindow::ApplyDI() { for(int num=0;num<8;num++) { emit apply_ReadDI(0,num); } } void MainWindow::DIresultdisp() { if(DIresult[0]==0) { ui->DI0->setStyleSheet("background-color:rgb(255, 255, 255)"); } else { ui->DI0->setStyleSheet("background-color:green"); } if(DIresult[1]==0) { ui->DI1->setStyleSheet("background-color:rgb(255, 255, 255)"); } else { ui->DI1->setStyleSheet("background-color:green"); } if(DIresult[2]==0) { ui->DI2->setStyleSheet("background-color:rgb(255, 255, 255)"); } else { ui->DI2->setStyleSheet("background-color:green"); } if(DIresult[3]==0) { ui->DI3->setStyleSheet("background-color:rgb(255, 255, 255)"); } else { ui->DI3->setStyleSheet("background-color:green"); } if(DIresult[4]==0) { ui->DI4->setStyleSheet("background-color:rgb(255, 255, 255)"); } else { ui->DI4->setStyleSheet("background-color:green"); } if(DIresult[5]==0) { ui->DI5->setStyleSheet("background-color:rgb(255, 255, 255)"); } else { ui->DI5->setStyleSheet("background-color:green"); } if(DIresult[6]==0) { ui->DI6->setStyleSheet("background-color:rgb(255, 255, 255)"); } else { ui->DI6->setStyleSheet("background-color:green"); } if(DIresult[7]==0) { ui->DI7->setStyleSheet("background-color:rgb(255, 255, 255)"); } else { ui->DI7->setStyleSheet("background-color:green"); } } void MainWindow::on_DOall_clicked() { static bool state=0; if(state==1) { ui->DO->setStyleSheet("background-color:green"); ui->DOall->setText("全部输出"); state=0; for (int num=0;num<8;num++) { emit apply_WriteDO(0,num,0); } ui->DO0->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DO1->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DO2->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DO3->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DO4->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DO5->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DO6->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DO7->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DOOUT0->setEnabled(1); ui->DOOUT1->setEnabled(1); ui->DOOUT2->setEnabled(1); ui->DOOUT3->setEnabled(1); ui->DOOUT4->setEnabled(1); ui->DOOUT5->setEnabled(1); ui->DOOUT6->setEnabled(1); ui->DOOUT7->setEnabled(1); } else { ui->DO->setStyleSheet("background-color:red"); ui->DOall->setText("全部关闭"); state=1; for (int num=0;num<8;num++) { emit apply_WriteDO(0,num,1); } ui->DO0->setStyleSheet("background-color:green"); ui->DO1->setStyleSheet("background-color:green"); ui->DO2->setStyleSheet("background-color:green"); ui->DO3->setStyleSheet("background-color:green"); ui->DO4->setStyleSheet("background-color:green"); ui->DO5->setStyleSheet("background-color:green"); ui->DO6->setStyleSheet("background-color:green"); ui->DO7->setStyleSheet("background-color:green"); ui->DOOUT0->setEnabled(0); ui->DOOUT1->setEnabled(0); ui->DOOUT2->setEnabled(0); ui->DOOUT3->setEnabled(0); ui->DOOUT4->setEnabled(0); ui->DOOUT5->setEnabled(0); ui->DOOUT6->setEnabled(0); ui->DOOUT7->setEnabled(0); } } void MainWindow::on_DOOUT0_clicked() { static bool state=0; if(state==0) { state=1; apply_WriteDO(0,0,0); ui->DO0->setStyleSheet("background-color:green"); ui->DOOUT0->setText("关闭"); } else { state=0; emit apply_WriteDO(0,0,1); ui->DO0->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DOOUT0->setText("输出"); } } void MainWindow::on_DOOUT1_clicked() { static bool state=0; if(state==0) { state=1; apply_WriteDO(0,1,0); ui->DO1->setStyleSheet("background-color:green"); ui->DOOUT1->setText("关闭"); } else { state=0; emit apply_WriteDO(0,1,1); ui->DO1->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DOOUT1->setText("输出"); } } void MainWindow::on_DOOUT2_clicked() { static bool state=0; if(state==0) { state=1; apply_WriteDO(0,2,0); ui->DO2->setStyleSheet("background-color:green"); ui->DOOUT2->setText("关闭"); } else { state=0; emit apply_WriteDO(0,2,1); ui->DO2->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DOOUT2->setText("输出"); } } void MainWindow::on_DOOUT3_clicked() { static bool state=0; if(state==0) { state=1; apply_WriteDO(0,3,0); ui->DO3->setStyleSheet("background-color:green"); ui->DOOUT3->setText("关闭"); } else { state=0; emit apply_WriteDO(0,3,1); ui->DO3->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DOOUT3->setText("输出"); } } void MainWindow::on_DOOUT4_clicked() { static bool state=0; if(state==0) { state=1; apply_WriteDO(0,4,0); ui->DO4->setStyleSheet("background-color:green"); ui->DOOUT4->setText("关闭"); } else { state=0; emit apply_WriteDO(0,4,1); ui->DO4->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DOOUT4->setText("输出"); } } void MainWindow::on_DOOUT5_clicked() { static bool state=0; if(state==0) { state=1; apply_WriteDO(0,5,0); ui->DO5->setStyleSheet("background-color:green"); ui->DOOUT5->setText("关闭"); } else { state=0; emit apply_WriteDO(0,5,1); ui->DO5->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DOOUT5->setText("输出"); } } void MainWindow::on_DOOUT6_clicked() { static bool state=0; if(state==0) { state=1; apply_WriteDO(0,6,0); ui->DO6->setStyleSheet("background-color:green"); ui->DOOUT6->setText("关闭"); } else { state=0; emit apply_WriteDO(0,6,1); ui->DO6->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DOOUT6->setText("输出"); } } void MainWindow::on_DOOUT7_clicked() { static bool state=0; if(state==0) { state=1; apply_WriteDO(0,7,0); ui->DO7->setStyleSheet("background-color:green"); ui->DOOUT7->setText("关闭"); } else { state=0; emit apply_WriteDO(0,7,1); ui->DO7->setStyleSheet("background-color:rgb(255, 255, 255)"); ui->DOOUT7->setText("输出"); } } void MainWindow::on_pushButton_4_clicked() { static bool state =0; if(state==0) { ui->pushButton_4->setStyleSheet("background-color: green; border: none;"); emit lightconnect(); state=1; ui->pushButton_4->setText("断开"); } else { ui->pushButton_4->setStyleSheet("background-color: red; border: none;"); emit closelightconnect(); state=0; ui->pushButton_4->setText("连接"); } } void MainWindow::on_pushButton_2_clicked() { emit SetLightControlPara(lightchannel(ui->LightChannel->currentIndex()),ui->mode->currentIndex(),ui->lightvalue->value(),ui->delaytime->value(),softtrigger); } void MainWindow::on_mode_currentIndexChanged(int index) { if(index==0) { ui->pushButton_3->setEnabled(1); } else { ui->pushButton_3->setEnabled(0); } } void MainWindow::on_pushButton_3_clicked() { if(softtrigger==0) { softtrigger=1; ui->pushButton_4->setStyleSheet("background-color: green; border: none;"); ui->pushButton_4->setText("关闭"); } else { softtrigger=0; ui->pushButton_4->setStyleSheet("background-color: rgb(187, 194, 200); border: none;"); ui->pushButton_4->setText("打开"); } emit SetLightControlPara(lightchannel(ui->LightChannel->currentIndex()),ui->mode->currentIndex(),ui->lightvalue->value(),ui->delaytime->value(),softtrigger); }
DIOtext:
LightControl:
工控机IO控制一般都不复杂,但诺达佳的控制上与别的厂商存在一些差异,所以在刚接触时可能觉得不太容易,笔者也是走了一些弯路后才尝试成功,特将一些心得总结于程序内,希望能帮到有需要的小伙伴。同时由于编程水平及表达能力有限,文章内不免有些难以通达之处,如有不足之处欢迎批评指正。wechat:Smart_Proud_Lee
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。