int main(void)
{
uint16 len=0;
uint8 SocketChanel=0;
uint16 modbus_len;
RCC_Configuration();
GPIO_Configuration();
Systick_Init(72);/* 初始化Systick工作时钟*/
USART1_Init(); /*初始化串口通信:9600@8-n-1*/
printf("STM32 initialization over.\r\n");
Reset_W5500();/*硬重启W5500*/
WIZ_SPI_Init();/*初始化SPI接口*/
printf("W5500 initialized!\r\n");
setSHAR(mac);/*配置Mac地址*/
setSUBR(sub);/*配置子网掩码*/
setGAR(gw);/*配置默认网关*/
setSIPR(lip);/*配置Ip地址*/
//Init. TX & RX Memory size of w5500
sysinit(txsize, rxsize); /*初始化8个socket*/
setRTR(2000);/*设置溢出时间值*/
setRCR(5);/*设置最大重新发送次数*/
getSIPR (ip);
printf("IP : %d.%d.%d.%d\r\n", ip[0],ip[1],ip[2],ip[3]);
getSUBR(ip);
printf("SN : %d.%d.%d.%d\r\n", ip[0],ip[1],ip[2],ip[3]);
getGAR(ip);
printf("GW : %d.%d.%d.%d\r\n", ip[0],ip[1],ip[2],ip[3]);
printf("Network is ready.\r\n");
setkeepalive(0);
while (1)
{
switch(getSn_SR(SocketChanel))/*获取socket0的状态*/
{
case SOCK_INIT:/*socket初始化完成*/
listen(SocketChanel);/*在TCP模式下向服务器发送连接请求*/
break;
case SOCK_ESTABLISHED:/*socket连接建立*/
if(getSn_IR(SocketChanel) & Sn_IR_CON)
{
setSn_IR(SocketChanel, Sn_IR_CON);/*Sn_IR的第0位置1*/
}
len=getSn_RX_RSR(SocketChanel);/*len为已接收数据的大小*/
if(len>0)
{
memset(buffer,0x00,len+1);
recv(SocketChanel,buffer,len);/*W5200接收来自Sever的数据*/
modbus_len = tcp_modslv(buffer,modbus_send);
if(modbus_len > 6)
{
send(SocketChanel,modbus_send,modbus_len);/*W5200向Server发送数据*/
}
}
send_ka(SocketChanel); /*防止网线断开*/
break;
case SOCK_CLOSE_WAIT:/*socket等待关闭状态*/
disconnect(SocketChanel); //防止上位机软件 断开重新连接的时候还能打开SOCKET
break;
case SOCK_CLOSED:/*socket关闭*/
socket(SocketChanel,Sn_MR_TCP,MYPORT,Sn_MR_ND);/*打开socket0的一个端口*/
break;
}
if((rtubuf1[0] & 0x01) == 0x01) LED_ON;
else LED_OFF;
}
}
|