回复 #7楼 pqsh
//C++中连接和发送的语句
static int acpt = -1, ct = -1;
if(ct != 0)
{
ct = tcpModbusSocket.Connect("192.168.1.5",502);
if(ct == 0)
{
tcpModbusSocket.SendtoService("192.168.1.5",502,(char*)&modbusSRPacket,sizeof(ModbusSRPacket));
}
}
else
{
tcpModbusSocket.SendtoService("192.168.1.5",502,(char*)&modbusSWPacket,sizeof(ModbusSWPacket));
tcpModbusSocket.SendtoService("192.168.1.5",502,(char*)&modbusSRPacket,sizeof(ModbusSRPacket));
}
发送的数据包
//modbus发送读数据请求数据包
struct ModbusSRPacket
{
ModbusSRPacket()
{
ask_high = 0x00;
ask_low = 0x00;
sign_high = 0x00;
sign_low = 0x00;
length_high = 0x00;
length_low = 0x06;
unit_sign = 0x01;
func_code = 0x03;
start_addr_high = 0x00;
start_addr_low = 0x04;
num_register_high = 0x00;
num_register_low = 0x05;
}
byte ask_high;//事务处理标识符
byte ask_low;//事务处理标识符
byte sign_high;//协议标识符
byte sign_low;//协议标识符
byte length_high;//数据长度
byte length_low;//数据长度
byte unit_sign;//设备地址
byte func_code;//功能码
byte start_addr_high;//起始地址
byte start_addr_low;//起始地址
byte num_register_high;//寄存器数量
byte num_register_low;//寄存器数量
};
//modbus发送写数据的数据包
struct ModbusSWPacket
{
ModbusSWPacket()
{
ask_high = 0x00;
ask_low = 0x00;
sign_high = 0x00;
sign_low = 0x00;
length_high = 0x00;
length_low = 0x0F;
unit_sign = 0x01;
func_code = 0x10;
start_addr_high = 0x00;
start_addr_low = 0x00;
num_register_high = 0x00;
num_register_low = 0x04;
num_write_bt = 0x08;
Self_Alignment_high = 0x00;
Self_Alignment_low = 0x00;
My_ShakeVal_high=0x00;
My_ShakeVal_low=0x00;
My_ShakeFre_high=0x00;
My_ShakeFre_low=0x00;
Steer_Damp_high=0x00;
Steer_Damp_low=0x35;
}
byte ask_high;//事务处理标识符
byte ask_low;//事务处理标识符
byte sign_high;//协议标识符
byte sign_low;//协议标识符
byte length_high;//数据长度
byte length_low;//数据长度
byte unit_sign;//设备地址
byte func_code;//功能码
byte start_addr_high;//起始地址
byte start_addr_low;//起始地址
byte num_register_high;//寄存器数量
byte num_register_low;//寄存器数量
byte num_write_bt;//写字节的个数
byte Self_Alignment_high;//
byte Self_Alignment_low;//方向盘是否回正 0回 1不回 3震动
byte My_ShakeVal_high;
byte My_ShakeVal_low;//方向盘晃动量 幅度 100
byte My_ShakeFre_high;
byte My_ShakeFre_low;//方向盘晃动频率 600
byte Steer_Damp_high;
byte Steer_Damp_low;//扭矩 阻尼 45
};