qt平台下诺达佳工控机二次开发实例分享(For IO & Light Control)
前言
点击查看本文源码
提示:以下是本篇文章正文内容,下面案例可供参考
一、编程环境
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);
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;
DIOLC_LibraryBaseDeinit_t DIOLC_LibraryBaseDeinit;
DIOLC_Process_t DIOLC_Process;
DIOLC_OpenDevice_t DIOLC_OpenDevice;
DIOLC_CloseDevice_t DIOLC_CloseDevice;
DIOLC_IsDeviceOpened_t DIOLC_IsDeviceOpened;
LC_SetPwmParams_t LC_SetPwmParams;
LC_GetPwmParams_t LC_GetPwmParams;
DIOLC_GetGeneralParam_t DIOLC_GetGeneralParam;
DIOLC_SetGeneralParam_t DIOLC_SetGeneralParam;
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);
void apply_WriteDO(BYTE doByteIndex, BYTE doBitIndex, BYTE doBitValue);
void SetLightControlPara(int ChIdx, int triggermode,
int lightvalue, int holdingtime, int 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;
}
}
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_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))
{
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;
}
}
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);
}
}
void DIOandLightControl::ioopen_two()
{
int result=Mission_succeed;
result=opendevice();
if(result!=Mission_succeed)
{
return;
}
result=initSetting();
if(result!=Mission_succeed)
{
return;
}
}
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()
{
ui->tabWidget->show();
ui->label->hide();
ui->pushButton->hide();
initIO();
}
void MainWindow::initIO()
{
iothread =new QThread;
ioandlight= new DIOandLightControl;
ioandlight->moveToThread(iothread);
connect(this,&MainWindow::connectio,ioandlight,&DIOandLightControl::ioopen);
connect(iothread, &QThread::finished, iothread, &QObject::deleteLater);
iothread->start();
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);
}
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
|