|
怎样在以太网模块中实现modbus协议?
#include "uip.h"
#include "uipopt.h"
#include "uip_arp.h"
#include "enc28j60.h"
#include <stdio.h>
#include <string.h>
#define BUF ((struct uip_eth_hdr *)&uip_buf[0])
/* NULL */
#ifndef NULL
#define NULL (void *)0
#endif
#define MHz_22_1184
#define AUTO_RECONNECT 1
#define delay_us(x) delay(x)
#define delay_ms(x) delay(x*1000)
#define delay_sec(x) delay(x*1000*1000)
#define SERVER_PORT 5480
u16_t ipaddr[2];
/*12MHz crystal about 1u=1us*/
void delay(u32_t u)
{
#ifdef MHz_22_1184
u *= 2;
#endif
while(u--);
}
/*uIP call back func*/
void uip_appcall(void)
{
switch(uip_conn->rport) {
case HTONS(SERVER_PORT):
if(uip_timedout()||uip_aborted()||uip_closed()){
#if AUTO_RECONNECT
/*reconnect to server*/
delay_sec(5);
uip_connect(ipaddr, HTONS(SERVER_PORT));
#endif
}else if(uip_connected()){
#if 1
/*is connected ok*/
uip_send(uip_appdata,sprintf((char*)uip_appdata,"%s",
"Hello,I connected to you! thanks."));
#endif
}else if(uip_rexmit()){
#if 1
/*need retransmission last packet*/
uip_send(uip_appdata,sprintf((char*)uip_appdata,"%s",
"this is retransmission packet"));
#endif
}else if(uip_poll()){
/*poll connecte is idle*/
#if 1
// uip_send(uip_appdata,sprintf((char*)uip_appdata,"%s",
// "Hi, we are idle"));
uip_send("idle",4);
#endif
}else if(uip_acked()){
/*get a ack for send packet ok*/
#if 0
uip_send(uip_appdata,sprintf((char*)uip_appdata,"%s",
"this is a second packet."));
#endif
if(uip_newdata()){
goto newdata_with_acked;
}
}else if(uip_newdata()){
newdata_with_acked:
#if 1
/*receving a new data*/
uip_appdata[uip_len+0] = ((char*)&uip_len)[0];
uip_appdata[uip_len+1] = ((char*)&uip_len)[1];
uip_send(uip_appdata,uip_len+2);
#endif
}else{
/*error*/
}
break;
default:
/*discard packet*/
break;
}
}
/*-----------------------------------------------------------------------------------*/
int main(void)
{
idata u8_t i, arptimer;
/* Initialize the network device driver. */
dev_init();
/* Initialize the ARP */
uip_arp_init();
/* Initialize the uIP TCP/IP stack. */
uip_init();
/*set host MAC addr*/
uip_ethaddr.addr[0] = 0x12;
uip_ethaddr.addr[1] = 0x34;
uip_ethaddr.addr[2] = 0x56;
uip_ethaddr.addr[3] = 0x78;
uip_ethaddr.addr[4] = 0x90;
uip_ethaddr.addr[5] = 0xAB;
#if UIP_FIXEDADDR == 0
/*host ip addr*/
uip_ipaddr(ipaddr, 192,168,1,13);
uip_sethostaddr(ipaddr);
/*netmask addr*/
uip_ipaddr(ipaddr, 255,255,255,0);
uip_setnetmask(ipaddr);
/*router ip addr*/
uip_ipaddr(ipaddr, 192,168,1,1);
uip_setdraddr(ipaddr);
#endif
/*connect to server*/
uip_ipaddr(ipaddr, 192,168,1,10);
uip_connect(ipaddr, HTONS(SERVER_PORT));
arptimer = 0;
while(1) {
/* Let the tapdev network device driver read an entire IP packet
into the uip_buf. If it must wait for more than 0.5 seconds, it
will return with the return value 0. If so, we know that it is
time to call upon the uip_periodic(). Otherwise, the tapdev has
received an IP packet that is to be processed by uIP. */
uip_len = dev_poll();
if(uip_len == 0) {
for(i = 0; i < UIP_CONNS; i++) {
uip_periodic(i);
/* If the above function invocation resulted in data that
should be sent out on the network, the global variable
uip_len is set to a value > 0. */
if(uip_len > 0) {
uip_arp_out();
dev_send();
}
}
/* Call the ARP timer function every 10 seconds. */
if(++arptimer == 20) {
uip_arp_timer();
arptimer = 0;
}
delay_us(5000);
} else {
if(BUF->type == htons(UIP_ETHTYPE_IP)) {
uip_arp_ipin();
uip_input();
/* If the above function invocation resulted in data that
should be sent out on the network, the global variable
uip_len is set to a value > 0. */
if(uip_len > 0) {
uip_arp_out();
dev_send();
}
} else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
uip_arp_arpin();
/* If the above function invocation resulted in data that
should be sent out on the network, the global variable
uip_len is set to a value > 0. */
if(uip_len > 0) {
dev_send();
}
}
}
}
return 0;
}
|
|