久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

標題: 關于modbus協議的問題 [打印本頁]

作者: kind_51    時間: 2018-5-17 15:50
標題: 關于modbus協議的問題
怎樣在以太網模塊中實現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;
}

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

關于程序

關于程序

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

0_1320213051036W_gif.jpg

作者: kerty80    時間: 2018-5-17 17:12
在 UDP 或者 TCP 數據 發送 段  加入  協議數據  即可, 同時   CRC16 校驗 可以 去掉 ,  TCP/IP  有自己的 校驗 可以去掉
作者: kind_51    時間: 2018-5-18 13:10
kerty80 發表于 2018-5-17 17:12
在 UDP 或者 TCP 數據 發送 段  加入  協議數據  即可, 同時   CRC16 校驗 可以 去掉 ,  TCP/IP  有自己 ...

感謝





歡迎光臨 (http://www.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 欧美美女一区二区 | 日韩不卡一区二区 | 欧美国产视频一区二区 | 国产精品有限公司 | 亚洲福利一区 | 国产精品 欧美精品 | 一区二区三区成人 | 男人的天堂亚洲 | 国产一区91精品张津瑜 | 日韩欧美国产不卡 | 精品久久香蕉国产线看观看亚洲 | 久久爱一区 | 四虎影音 | 日韩在线播放第一页 | 国产农村妇女毛片精品久久麻豆 | 日韩成人国产 | 日韩高清一区 | 国产精品福利网 | 欧美久久精品 | 国产精品小视频在线观看 | 美国av片在线观看 | 99福利视频| 伊人艹 | 久久99精品久久久久久狂牛 | 一级毛片在线视频 | 国产日韩av一区二区 | 国产成人精品一区二区三区在线 | 欧美片网站免费 | 久久久久久九九九九 | 成人国产免费视频 | 日本一区二区高清不卡 | 中文精品一区二区 | av网站免费看 | 国产午夜精品一区二区三区四区 | 99久久99| 精品久久久久久久久久久久 | 午夜视频在线观看网址 | 中文日韩在线视频 | 激情国产在线 | 久久精品久久久 | 日韩精品视频中文字幕 |