QT5.3.0 对扫码枪和三菱PLC的通信

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 
#include 
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:

    QSerialPort *SaoMQ=new QSerialPort(this);
    QSerialPort *PLC=new QSerialPort(this);
    QTimer *t1=new QTimer(this);
    explicit MainWindow(QWidget *parent = 0);

    ~MainWindow();

private slots:


    void on_SaoMQCOM_activated(const QString &arg1);

    void on_PLCCOM_activated(const QString &arg1);
    char ConvertHexChar(char ch);
    void on_SMQstart_clicked();
    void StringToHex(QString str, QByteArray & senddata);//STRing转16进制
    void on_SMQstop_clicked();

    void on_PLCOK_clicked();

    void on_PLCNG_clicked();

    void on_closePLCcom_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //把电脑上的COM口列出来并设置到下拉框里面
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
       {
           QSerialPort a;
           a.setPort(info);
           if(a.open(QIODevice::ReadWrite))
           {
               ui->SaoMQCOM->addItem(a.portName());//设置下拉框
               ui->PLCCOM->addItem(a.portName());
               a.close();
           }
    }
    ui->SMQstart->setEnabled(false);
    ui->SMQstop->setEnabled(false);
    ui->PLCOK->setEnabled(false);
    ui->PLCNG->setEnabled(false);
    connect(t1,SIGNAL(timeout()),this,SLOT(on_SMQstop_clicked()));
    QString str1="200";
    int l=str1.toInt(0,10);
    qDebug("intensity:%d",l);

}

MainWindow::~MainWindow()
{
        delete ui;
}
void MainWindow::on_SaoMQCOM_activated(const QString &arg1)
{//扫码枪

    SaoMQ->setPortName(arg1);
    SaoMQ->setBaudRate(115200);//波特率
    SaoMQ->setDataBits(QSerialPort::Data8);//数据位
    SaoMQ->setParity(QSerialPort::NoParity);//校验
    SaoMQ->setStopBits(QSerialPort::OneStop);//停止位
    SaoMQ->setFlowControl(QSerialPort::NoFlowControl);//控制流
    SaoMQ->open(QIODevice::ReadWrite);//以可读可写模式打开串口
    ui->SMQstart->setEnabled(true);
    ui->SMQstop->setEnabled(true);
    qDebug("扫码枪串口已设定并打开");
}

void MainWindow::on_PLCCOM_activated(const QString &arg1)
{
    //PLC
    PLC->setPortName(arg1);
    PLC->setBaudRate(9600);
    PLC->setDataBits(QSerialPort::Data7);
    PLC->setParity(QSerialPort::EvenParity);
    PLC->setStopBits(QSerialPort::OneStop);
    PLC->setFlowControl(QSerialPort::NoFlowControl);
    PLC->open(QIODevice::ReadWrite);//以可读可写模式打开串口
    ui->PLCOK->setEnabled(true);
    ui->PLCNG->setEnabled(true);
    qDebug("PLC串口已设定并打开");

}

void MainWindow::on_SMQstart_clicked()
{
    ui->SMQstart->setEnabled(false);
    QString str="16540d";//扫码枪启动指令字符串
    QByteArray senddata;
    StringToHex(str,senddata);//调用函数把字符串转化为16进制
    SaoMQ->write(senddata);//
    t1->setSingleShot(true);//
    //把输入框里面的数字转化为整形
    QString str1=ui->SMQtime->text();
    int i=str1.toInt(0,10);
    t1->start(i);
}
void MainWindow::StringToHex(QString str, QByteArray & senddata)//string 转16进制
{
    int hexdata,lowhexdata;
    int hexdatalen = 0;
    int len = str.length();
    senddata.resize(len/2);
    char lstr,hstr;
    for(int i=0; i= len)
            break;
        lstr = str[i].toLatin1();
        hexdata = ConvertHexChar(hstr);
        lowhexdata = ConvertHexChar(lstr);
        if((hexdata == 16) || (lowhexdata == 16))
            break;
        else
            hexdata = hexdata*16+lowhexdata;
        i++;
        senddata[hexdatalen] = (char)hexdata;
        hexdatalen++;
    }
    senddata.resize(hexdatalen);
}
char MainWindow::ConvertHexChar(char ch)
{
    if((ch >= '0') && (ch <= '9'))
            return ch-0x30;
        else if((ch >= 'A') && (ch <= 'F'))
            return ch-'A'+10;
        else if((ch >= 'a') && (ch <= 'f'))
            return ch-'a'+10;
//        else return (-1);
        else return ch-ch;//
}
void MainWindow::on_SMQstop_clicked()
{
    QString str="16550d";//扫码枪停止指令字符串
    QByteArray senddata;
    StringToHex(str,senddata);//调用函数把字符串转化为16进制
    SaoMQ->write(senddata);//
    t1->stop();
    ui->SMQstart->setEnabled(true);
}

void MainWindow::on_PLCOK_clicked()
{
    //三菱PLC D40发送1 OK
    QString str="02 31 31 30 35 30 30 32 30 31 30 30 03 31 44";
    QByteArray senddata;
    StringToHex(str,senddata);//调用函数把字符串转化为16进制
    PLC->write(senddata);//
}

void MainWindow::on_PLCNG_clicked()
{
    //三菱PLC D40发送2  NG
    QString str="02 31 31 30 35 30 30 32 30 30 32 30 03 31 45";
    QByteArray senddata;
    StringToHex(str,senddata);//调用函数把字符串转化为16进制
    PLC->write(senddata);//
}

void MainWindow::on_closePLCcom_clicked()
{
    PLC->close();
    ui->PLCOK->setEnabled(false);
    ui->PLCNG->setEnabled(false);
    ui->PLCCOM->setEditable(true);
}

QT5.3.0 对扫码枪和三菱PLC的通信_第1张图片

你可能感兴趣的:(QT5.3.0 对扫码枪和三菱PLC的通信)