基于libmodbus的QT串口相关(零)——源代码

一.源代码

今晚在csdn上找了一个基于libmodbus的QT代码,抛开libmodbus代码不谈,先上这个源代码。

widget.h的内容:

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include "modbus.h"
#include "modbus-rtu.h"
#include "modbus-tcp.h"
#include "modbus-version.h"
using namespace std;
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
private slots:
    void on_comboBox_BoTeLv_currentIndexChanged(const QString &arg1);

    void on_comboBox_DataBit_currentIndexChanged(const QString &arg1);

    void on_comboBox_Port_currentIndexChanged(const QString &arg1);

    void on_comboBox_JiaoYanBit_currentIndexChanged(const QString &arg1);

    void on_comboBox_StopBit_currentIndexChanged(const QString &arg1);

    void on_BTN_OpenPLC_clicked();

    void on_BTN_ClosePLC_clicked();

    void on_BTN_Write_clicked();

    void on_radioButton_XianQuan_clicked();

    void on_radioButton_JiCunQI_clicked();

    void on_BTN_Read_clicked();

private:
    Ui::Widget *ui;
    modbus_t *plc;
};

#endif

widget.cpp

#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        QSerialPort serial;
        serial.setPort(info);
        if(serial.open(QIODevice::ReadWrite))
        {
            ui->comboBox_Port->addItem(serial.portName());
            serial.close();
        }
    }
    ui->comboBox_BoTeLv->addItem("4800");
    ui->comboBox_BoTeLv->addItem("9600");
    ui->comboBox_BoTeLv->addItem("19200");
    ui->comboBox_BoTeLv->addItem("115200");
    ui->comboBox_BoTeLv->setCurrentText("9600");
    ui->comboBox_DataBit->addItem("6");
    ui->comboBox_DataBit->addItem("7");
    ui->comboBox_DataBit->addItem("8");
    ui->comboBox_DataBit->setCurrentText("8");
    ui->comboBox_JiaoYanBit->addItem("O");
    ui->comboBox_JiaoYanBit->addItem("E");
    ui->comboBox_JiaoYanBit->addItem("N");
    ui->comboBox_JiaoYanBit->setCurrentText("N");
    ui->comboBox_StopBit->addItem("0");
    ui->comboBox_StopBit->addItem("1");
    ui->comboBox_StopBit->setCurrentText("1");
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_comboBox_BoTeLv_currentIndexChanged(const QString &arg1)
{
    qDebug() << ui->comboBox_BoTeLv->currentText();
}

void Widget::on_comboBox_DataBit_currentIndexChanged(const QString &arg1)
{
    qDebug() << ui->comboBox_DataBit->currentText();
}

void Widget::on_comboBox_Port_currentIndexChanged(const QString &arg1)
{
    qDebug() << ui->comboBox_Port->currentText();
}

void Widget::on_comboBox_JiaoYanBit_currentIndexChanged(const QString &arg1)
{
    qDebug() << ui->comboBox_JiaoYanBit->currentText();
}

void Widget::on_comboBox_StopBit_currentIndexChanged(const QString &arg1)
{
    qDebug() << ui->comboBox_StopBit->currentText();
}

void Widget::on_BTN_OpenPLC_clicked()
{
    QString Port = ui->comboBox_Port->currentText();
    int BoTeLv = ui->comboBox_BoTeLv->currentText().toInt();
    QString JiaoYanBit = ui->comboBox_JiaoYanBit->currentText();
    int DataBit = ui->comboBox_DataBit->currentText().toInt();
    int StopBit = ui->comboBox_StopBit->currentText().toInt();
    int SheBeiDiZhi = ui->lineEdit_SheBeiDiZhi->text().toInt();
    const char *port = Port.toLatin1().constData();
    if(JiaoYanBit == "O")
    {
        plc = modbus_new_rtu(port, BoTeLv, 'O', DataBit, StopBit);
    }
    else if(JiaoYanBit == "E")
    {
        plc = modbus_new_rtu(port, BoTeLv, 'E', DataBit, StopBit);
    }
    else if(JiaoYanBit == "N")
    {
        plc = modbus_new_rtu(port, BoTeLv, 'N', DataBit, StopBit);
    }
    qDebug() << Port;
    qDebug() << QString::number(BoTeLv, 10);
    qDebug() << JiaoYanBit;
    qDebug() << QString::number(DataBit, 10);
    qDebug() << QString::number(StopBit, 10);
    plc = modbus_new_rtu(port, BoTeLv, 'N', DataBit, StopBit);
    modbus_set_slave(plc, SheBeiDiZhi);
    qDebug() << QString::number(SheBeiDiZhi, 10);
    int plccount = modbus_connect(plc);
    struct timeval time;
    time.tv_sec = 0;
    time.tv_usec = 1000000;
    modbus_set_response_timeout(plc, (int)&time.tv_sec, (int)&time.tv_usec);
    if(plccount == 0)
    {
        qDebug() << "连接PLC成功";
    }
    else
    {
        qDebug() << "连接PLC失败";
    }
}

void Widget::on_BTN_ClosePLC_clicked()
{
    modbus_close(plc);
    modbus_free(plc);
}


void Widget::on_BTN_Write_clicked()
{
    int Adress = ui->lineEdit_JiCunQiDIZhiWrite->text().toInt();
    int ShuZhi = ui->lineEdit_JiCunQiShuZhi->text().toInt();
    if(ui->radioButton_XianQuan->isChecked() == true)
    {
        qDebug() << "写入线圈";
        modbus_write_bit(plc, Adress, ShuZhi);
    }
    else
    {
       qDebug() << "写入寄存器";
       modbus_write_register(plc, Adress, ShuZhi);
    }
}

void Widget::on_radioButton_XianQuan_clicked()
{
    qDebug() << "线圈";
}

void Widget::on_radioButton_JiCunQI_clicked()
{
    qDebug() << "寄存器";
}

void Widget::on_BTN_Read_clicked()
{
    uint16_t read_reg[255] = {0};
    uint8_t read_reg_bit[255] = {0};
    int Adress = ui->lineEdit_JiCunQiDIZhiRead->text().toInt();
    int Count = ui->lineEdit_Count->text().toInt();
    if(ui->radioButton_XianQuan->isChecked() == true)
    {
        qDebug() << "读取线圈";
        for (int i = 0; i < Count; i++)
        {
            modbus_read_bits(plc, Adress, Count, read_reg_bit);
            qDebug() << read_reg_bit[i];
        }
    }
    else
    {
       qDebug() << "读取寄存器";
       for (int i = 0; i < Count; i++)
       {
           modbus_read_registers(plc, Adress, Count, read_reg);
           qDebug() << read_reg[i];
       }
    }
}











main.cpp

#include "widget.h"
#include 

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

    return a.exec();
}

 

你可能感兴趣的:(通信)