当前位置:   article > 正文

qt平台下诺达佳工控机二次开发实例分享(For IO & Light Control)_qt在工控领域应用实例

qt在工控领域应用实例

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);//将读取输入结果发送给主线程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();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

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;
    
};

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

程序文件

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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181

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);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461

程序界面

DIOtext:
在这里插入图片描述
LightControl:
在这里插入图片描述

总结

工控机IO控制一般都不复杂,但诺达佳的控制上与别的厂商存在一些差异,所以在刚接触时可能觉得不太容易,笔者也是走了一些弯路后才尝试成功,特将一些心得总结于程序内,希望能帮到有需要的小伙伴。同时由于编程水平及表达能力有限,文章内不免有些难以通达之处,如有不足之处欢迎批评指正。wechat:Smart_Proud_Lee

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/109939
推荐阅读
相关标签
  

闽ICP备14008679号