一.TCP数据通信、DHCP自动获取IP
1.获取官方资料
打开寄存器方式客户端模式例程 关闭网络连接,手动设置ip,网关,子网掩码 烧录程序,打开官方的调试软件进行测试:
二.应用层modbus协议编程
主函数:
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SPI1_Init();
MX_USART1_UART_Init();
System_Initialization();
Load_Net_Parameters();
W5500_Hardware_Reset();
W5500_Initialization();
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);
}
}
简单响应函数:
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++;
}
效果:
参考
https://blog.csdn.net/qq_47281915/article/details/121927294 https://blog.csdn.net/tcjy1000/article/details/22962067
|