详细:ModBus/TCP协议分析
代码
void Load_Net_Parameters(void)
{
Gateway_IP[0] = 192;
Gateway_IP[1] = 168;
Gateway_IP[2] = 1;
Gateway_IP[3] = 1;
Sub_Mask[0]=255;
Sub_Mask[1]=255;
Sub_Mask[2]=255;
Sub_Mask[3]=0;
Phy_Addr[0]=0x0c;
Phy_Addr[1]=0x29;
Phy_Addr[2]=0xab;
Phy_Addr[3]=0x7c;
Phy_Addr[4]=0x00;
Phy_Addr[5]=0x01;
IP_Addr[0]=192;
IP_Addr[1]=168;
IP_Addr[2]=1;
IP_Addr[3]=199;
S0_Port[0] = 0x13;
S0_Port[1] = 0x88;
S0_Mode=TCP_SERVER;
}
void Process_Socket_Data(SOCKET s)
{
int len;
unsigned char msg[11]={0x00,0x00,0x00 ,0x00, 0x00, 0x05, 0x01, 0x03, 0x02, 0x00, 0x70};
len=sizeof(msg);
unsigned short size;
size=Read_SOCK_Data_Buffer(s, Rx_Buffer);
memcpy(Tx_Buffer, Rx_Buffer, size);
for (int j=0;j<size;j++){
printf("0x%02X ",Tx_Buffer[j]);
}
msg[0]=Tx_Buffer[0];
msg[1]=Tx_Buffer[1];
msg[2]=0x00;
msg[3]=0x00;
msg[4]=0x00;
msg[5]=0x05;
msg[6]=Tx_Buffer[6];
msg[7]=Tx_Buffer[7];
msg[8]=0x02;
msg[10]=data&0XFF;
msg[9]=data>>8;
memcpy(Tx_Buffer, msg, len);
Write_SOCK_Data_Buffer(0, Tx_Buffer, len);
data++;
}
while (1)
{
W5500_Socket_Set();
W5500_Interrupt_Process();
if((S0_Data & S_RECEIVE) == S_RECEIVE)
{
S0_Data&=~S_RECEIVE;
Process_Socket_Data(0);
}
HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);
}
设置
设置id、地址、读取位数 设置地址、端口
结果
参考
|