找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2231|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

關(guān)于modbus協(xié)議的問題

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
怎樣在以太網(wǎng)模塊中實(shí)現(xiàn)modbus協(xié)議?
#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;
}

0_1320213072Bxb2_gif.jpg (107.52 KB, 下載次數(shù): 37)

關(guān)于程序

關(guān)于程序

0_1320213051036W_gif.jpg (195.54 KB, 下載次數(shù): 33)

0_1320213051036W_gif.jpg
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:332297 發(fā)表于 2018-5-17 17:12 | 只看該作者
在 UDP 或者 TCP 數(shù)據(jù) 發(fā)送 段  加入  協(xié)議數(shù)據(jù)  即可, 同時(shí)   CRC16 校驗(yàn) 可以 去掉 ,  TCP/IP  有自己的 校驗(yàn) 可以去掉
回復(fù)

使用道具 舉報(bào)

板凳
ID:325252 發(fā)表于 2018-5-18 13:10 | 只看該作者
kerty80 發(fā)表于 2018-5-17 17:12
在 UDP 或者 TCP 數(shù)據(jù) 發(fā)送 段  加入  協(xié)議數(shù)據(jù)  即可, 同時(shí)   CRC16 校驗(yàn) 可以 去掉 ,  TCP/IP  有自己 ...

感謝
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表