关于STM32+w5500实现的MODBUS的过程!
各位好!经过摸索,这段时间主要实现了一个STM32与W5500的实现MODBUS的过程!
这方面的资料,网上可能也有不少!
现在给大家共享一下自己的研究结果。
另外 还是想实现FREEMODBUS!
请看截图! #include "main.h"
uint8 mac={0x00,0x08,0xdc,0x11,0x11,0x11};/*定义Mac变量*/
uint8 lip={192,168,5,101};/*定义lp变量*/
uint16 MYPORT=6000;
uint8 sub={255,255,255,0};/*定义subnet变量*/
uint8 gw={192,168,5,1};/*定义gateway变量*/
uint8 ip;
uint8 buffer;/*定义一个2KB的缓存*/
uint8 modbus_send;/*定义一个2KB的缓存*/
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,ip,ip,ip);
getSUBR(ip);
printf("SN : %d.%d.%d.%d\r\n", ip,ip,ip,ip);
getGAR(ip);
printf("GW : %d.%d.%d.%d\r\n", ip,ip,ip,ip);
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 & 0x01) == 0x01)LED_ON;
else LED_OFF;
}
}
void RCC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
| RCC_APB2Periph_AFIO| RCC_APB2Periph_USART1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = LED;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, LED); // led off
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin =USART1_TX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = USART1_RX;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// SPI
/* Configure SPIy pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = WIZ_SCLK | WIZ_MISO | WIZ_MOSI;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = WIZ_SCS|LED;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, WIZ_SCS);
GPIO_InitStructure.GPIO_Pin = WIZ_RESET ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, WIZ_RESET);
GPIO_InitStructure.GPIO_Pin = WIZ_INT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
} 下面上传一下 压缩包 关于 这个实现MODBUS的例程 好大的一个数组,震惊了,4096,4k的字节就这么没了。 W5500是什么芯片呢?没有用过 w5500是以太网芯片,我还以为是什么呢 是啊!4K的字节算什么啊?
现在都是 几百KB的了! 很好 谢谢分享 是啊!
很好 谢谢分享
页:
[1]