一.线圈状态寄存器读写
- 项目效果如下
1. 写单个寄存器
MODBUS_API int modbus_write_bit(modbus_t *ctx, int coil_addr, int status);
int addr=ui->spinBoxwirte_addr->value();int data=ui->spinBoxwirte_data->value();int ret = modbus_write_bit(mb,addr,data);if(ret!=1){QMessageBox::information(this,"失败","写状态寄存器失败,地址:"+QString::number(data));}else{label_status->setText("写线圈状态寄存器成功!");}
2. 读单个寄存器
int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
int addr=ui->spinBoxRead_addr->value();int data=ui->spinBoxRead_data->value();//int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);uint8_t dest[4];int ret = modbus_read_bits(mb,addr,1,dest);if(ret!=1){QMessageBox::information(this,"失败","读状态寄存器失败,地址:"+QString::number(data));}else{label_status->setText("读线圈状态寄存器成功!");qDebug()<<"读出的数据为:"<<dest[0];ui->spinBoxRead_data->setValue(dest[0]);}
3. 写多个寄存器
int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data);
int addr=ui->spinBoxWriteMore_addr->value();//使用正则表达式,来处理字符串的分割QString str = ui->textEdit_xianWrite->toPlainText();//用正则表达式进行分割QRegExp separator= QRegExp("\t|\n|\r\n|,| |;");//进行分割QStringList list = str.split(separator,QString::SkipEmptyParts);uint8_t*value=new uint8_t[list.count()];for(int i=0;i<list.count();i++){value[i]=list[i].toInt();}//int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data);int ret = modbus_write_bits(mb,addr,list.count(),value);if(ret!=list.count()){QMessageBox::information(this,"写多个失败","读状态寄存器失败,地址:"+QString::number(addr));label_status->setText("写多个线圈状态寄存器失败!");}else{label_status->setText("写多个线圈状态寄存器成功!");}delete [] value;
4. 读多个寄存器
- int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
ui->textEditRead_xian->clear();int addr=ui->spinBoxReadMorexian_addr->value();int count=ui->spinBoxReadMorexian_num->value();uint8_t *values = new uint8_t[count];//int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);int ret=modbus_read_bits(mb,addr,count,values);if(ret!=count){QMessageBox::information(this,"读多个失败","读状态寄存器失败,地址:"+QString::number(addr));label_status->setText("读多个线圈状态寄存器失败!");}else{label_status->setText("读多个线圈状态寄存器成功!");QString str;for(int i=0;i<count;i++){str+=QString::number(values[i])+"\t";}ui->textEditRead_xian->setText(str);}delete [] values;
二.离散输入状态寄存器读写
- 效果展示如下
1. 读单个寄存器
int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
int addr=ui->spinBoxInput_one_addr->value();int data;//int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);uint8_t dest[4];int ret = modbus_read_input_bits(mb,addr,1,dest);if(ret!=1){QMessageBox::information(this,"失败","读单个离散输入寄存器失败,地址:"+QString::number(addr));}else{label_status->setText("读单个离散输入寄存器成功!");qDebug()<<"读出的数据为:"<<dest[0];ui->spinBoxInput_one_data->setValue(dest[0]);}
2. 读多个寄存器
int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
ui->textEdit_input->clear();int addr=ui->spinBoxInput_more_addr->value();int count=ui->spinBoxInput_more_num->value();//int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);uint8_t *dest=new uint8_t[count];int ret = modbus_read_input_bits(mb,addr,count,dest);if(ret!=count){QMessageBox::information(this,"失败","读多个离散输入寄存器失败,地址:"+QString::number(addr));}else{label_status->setText("读多个离散输入寄存器成功!");QString str;for(int i=0;i<count;i++){str+= QString::number(dest[i])+"\t";}ui->textEdit_input->setText(str);}
三.保持寄存器读写
- 效果如下:
1. 写单个寄存器
int modbus_write_register(modbus_t *ctx, int reg_addr, const uint16_t value);
//int modbus_write_register(modbus_t *ctx, int reg_addr, const uint16_t value);int addr=ui->spinBoxHoldRegWrite_addr->value();int value=ui->spinBoxHoldRegW_data->value();int ret =modbus_write_register(mb,addr,value);if(ret!=1){QMessageBox::information(this,"失败","保持寄存器写单个失败");}else{QMessageBox::information(this,"成功","保持寄存器写单个成功!!!");}
2. 读单个寄存器
- int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
int addr=ui->spinBox_HoldR_addr->value();uint16_t dest[4];//int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);int ret = modbus_read_registers(mb,addr,1,dest);if(ret!=1){QMessageBox::information(this,"失败","保持寄存器读单个失败");}else{QMessageBox::information(this,"成功","保持寄存器读单个成功!!!");ui->spinBox_HoldR_data->setValue(dest[0]);}
四.输入寄存器读写
- 项目效果
1. 读单个寄存器
- int modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
int addr=ui->spinBoxInput_one_addr->value();int data;//int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);uint8_t dest[4];int ret = modbus_read_input_registers(mb,addr,1,dest);if(ret!=1){QMessageBox::information(this,"失败","读单个离散输入寄存器失败,地址:"+QString::number(addr));}else{label_status->setText("读单个离散输入寄存器成功!");qDebug()<<"读出的数据为:"<<dest[0];ui->spinBoxInput_one_data->setValue(dest[0]);}
2. 读多个寄存器
int modbus_read_input_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
ui->textEdit_input->clear();int addr=ui->spinBoxInput_more_addr->value();int count=ui->spinBoxInput_more_num->value();//int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);uint8_t *dest=new uint8_t[count];int ret = modbus_read_input_registers(mb,addr,count,dest);if(ret!=count){QMessageBox::information(this,"失败","读多个离散输入寄存器失败,地址:"+QString::number(addr));}else{label_status->setText("读多个离散输入寄存器成功!");QString str;for(int i=0;i<count;i++){str+= QString::number(dest[i])+"\t";}ui->textEdit_input->setText(str);}