处理modbus RTU数据电压 0x00 0x01 0x96 0x40 电流0x00 0x00 0x06 0x73
如下,
//串口接收函数,数据来源于上位机
static void rx0CB(uint8 port,uint8 event)
{
(void)port;
uint8 uart0buf[12];//获取控制命令
unsigned int v_value;
uint16 i_value;
uint8 v[3];
uint8 i[2];
HalUARTRead(0,uart0buf,12);//参数下发
if(uart0buf[0]==0x01&&uart0buf[1]==0x03&&uart0buf[2]==0x08)
{
v_value=uart0buf[4]*65536+uart0buf[5]*256+uart0buf[6];
rftx.v_num=v_value/1000; //3位有效数
i_value=uart0buf[9]*256+uart0buf[10];
rftx.i_num=i_value/100; //2位有效数
v[0]=rftx.v_num/100+'0';
v[1]=rftx.v_num%100/10+'0';
v[2]=rftx.v_num%10+'0';
HalUARTWrite(1,v,3);
i[0]=rftx.i_num/10+'0';
i[1]=rftx.i_num%10+'0';
HalUARTWrite(1," ",1);
HalUARTWrite(1,i,2);
}
}
这是结构体
struct RFTX
{
uint8 num;
uint8 relay_state[6];
uint8 Temp[2];
uint8 Vcc[2];
uint16 v_num;
uint8 i_num;
}rftx={1};
这是uint8等定义
typedef signed char int8;
typedef unsigned char uint8; //无符号1字节
typedef signed short int16;
typedef unsigned short uint16;
typedef signed long int32;
typedef unsigned long uint32;
typedef unsigned char bool;
typedef uint8 halDataAlign_t;
不知为何最后电压串口数据并不对,电流是正确的