QT-C++Modbuse TCP后台线程批量读写操作

QT-C++Modbuse TCP读写操作

  • 前言
  • 一、效果演示
  • 二、关键代码
  • 三、程序链接


前言

Modbuse TCP读写操作、支持批量读写,线圈和D寄存器读写,后台线程自动批量缓存,上层接口读取不阻塞

一、效果演示

QT-C++Modbuse TCP后台线程批量读写操作_第1张图片

二、关键代码

// 写寄存器值(单个)
 bool cModbusObject::writeRegister(int nAddr, uint16_t nValue)
 {
	 bool bRet = false;
	 if (!d_ptr->bConnected || nAddr < 0 )
		 return bRet;

	 if (modbus_write_registers(d_ptr->ctx, nAddr,(int)MAX_RW_COUNT,&nValue) != RETURN_ERROR)
		 bRet = true;
	 else
	 {
		 modbus_flush(d_ptr->ctx);
		qDebug() << QObject::tr("Modbus 寄存器写入错误:%1").arg(modbus_strerror(errno)) << "\n";
	 }
	 return bRet;
 }

// 写寄存器值(多个)
 int cModbusObject::writeRegisters(int nAddr, int nCount, uint16_t *valueArray)
 {
	 int nRet = 0;
	 if (!d_ptr->bConnected || nAddr < 0)
		 return nRet;

	 nRet = modbus_write_registers(d_ptr->ctx, nAddr, nCount, valueArray);

	 if (nRet == RETURN_ERROR)
	 {
		 modbus_flush(d_ptr->ctx);
		qDebug() << QObject::tr("Modbus 寄存器写入错误:%1").arg(modbus_strerror(errno)) << "\n";
	 }
	 return nRet;
 }

// 读寄存器值(单个)
 bool cModbusObject::readRegister(int nAddr, uint16_t &nValue)
 {
	 bool bRet = false;
	 if (!d_ptr->bConnected || nAddr < 0)
		 return bRet;

	 if (modbus_connect(d_ptr->ctx) == RETURN_ERROR)
	 {
		 qDebug() << QObject::tr("Modbus 通讯连接失败!错误信息:%1").arg(modbus_strerror(errno)) << "\n";
	 }

	 int nRCount = 0;
	 nRCount = modbus_read_registers(d_ptr->ctx, nAddr, (int)MAX_RW_COUNT, &nValue);
	 if (nRCount != RETURN_ERROR)
	 {
		 bRet = true;
	 }
	 else
	 {
		 qDebug() << QObject::tr("Modbus 寄存器:%1读取错误:%2").arg(nAddr).arg(modbus_strerror(errno)) << "\n";
		 //modbus_flush(d_ptr->ctx);
	 }

	 return bRet;
 }

// 读寄存器值(多个)
 int cModbusObject::readRegisters(int nAddr, int nCount, uint16_t *valueArray)
 {
	 if (modbus_connect(d_ptr->ctx) == RETURN_ERROR)
	 {
		 qDebug() << QObject::tr("Modbus 通讯连接失败!错误信息:%1").arg(modbus_strerror(errno)) << "\n";
	 }
	 int nRet = 0;
	  nRet = modbus_read_registers(d_ptr->ctx, (int)nAddr, (int)nCount, valueArray);
	 if (nRet < 0)
	 {
		 modbus_flush(d_ptr->ctx);
		qDebug() << QObject::tr("Modbus 寄存器:%1 数量:%2 读取错误:%3").arg(nAddr).arg(nCount).arg(modbus_strerror(errno)) << "\n";
	 }

	 return  nRet;
 }

// 读线圈值(多个)
 int cModbusObject::readCoilStatusBits(int nAddr, int nCount, uint8_t *valueArray)
 {
	 int nRet = 0;
	 nRet = modbus_read_bits(d_ptr->ctx, (int)nAddr, (int)nCount, valueArray);
	 if (nRet < 0)
	 {
		 modbus_flush(d_ptr->ctx);
	 }
	 return nRet;
 }

// 设置位状态
 bool cModbusObject::setBit(int nAddr, bool bState, eRegisterType nType, int nBit)
 {
	 bool bRet = false;
	 if (!d_ptr->bConnected ||nAddr < 0|| nBit < 0)
		 return bRet;

	 if (nType == RT_D_READ_HOLDING_REGISTERS)
	 {
		 uint16_t nValue = 0;
		 if (!readRegister(nAddr, nValue))
			 bRet = false;
		 else
		 {
			 if (bState)
				 nValue |= (1 << (nBit % 16));
			 else
				 nValue &= ~(1 << (nBit % 16));

			 bRet = writeRegister(nAddr, nValue);
		 }
	 }
	 else if (nType == RT_M_COIL_STATUS)
	 {
		 uint8_t nBitWrite = bState ? 1 : 0;
		 if (modbus_write_bits(d_ptr->ctx,nAddr, MAX_RW_COUNT,&nBitWrite) != RETURN_ERROR)
			 bRet = true;
		 else
		 {
			 modbus_flush(d_ptr->ctx);
			 qDebug() << QObject::tr("Modbus 寄存器位写入错误:%1").arg(modbus_strerror(errno)) << "\n";
		 }

	 }

	 return bRet;
 }

// 读取位状态
 bool cModbusObject::getBit(int nAddr, bool &bState, eRegisterType nType, int nBit)
 {
	 bool bRet = false;
	 if (!d_ptr->bConnected || nAddr < 0 || nBit < 0)
		 return bRet;

	 if (nType == RT_D_READ_HOLDING_REGISTERS)
	 {
		 uint16_t nValue = 0;
		 if (!readRegister(nAddr, nValue))
			 bRet = false;
		 else
		 {
			 bRet = true;
			 bState = (nValue & (1 << (nBit % 16))) > 0?true:false;
		 }
	 }
	 else if (nType == RT_M_COIL_STATUS)
	 {
		 uint8_t nBitRead = 0;
		 if (modbus_read_bits(d_ptr->ctx, nAddr, MAX_RW_COUNT, &nBitRead) != RETURN_ERROR)
			 bRet = true;
		 else
		 {
			 modbus_flush(d_ptr->ctx);
			 qDebug() << QObject::tr("Modbus 寄存器位读取错误:%1").arg(modbus_strerror(errno)) << "\n";
		 }
		 bState = (nBitRead == 1 ? true : false);
	 }

	 return bRet;
 }

三、程序链接

程序下载

你可能感兴趣的:(C++,Qt,qt,开发语言)