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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2199|回復: 2
打印 上一主題 下一主題
收起左側

關于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
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:332297 發表于 2018-5-17 17:12 | 只看該作者
在 UDP 或者 TCP 數據 發送 段  加入  協議數據  即可, 同時   CRC16 校驗 可以 去掉 ,  TCP/IP  有自己的 校驗 可以去掉
回復

使用道具 舉報

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

感謝
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 久久久久中文字幕 | 亚洲精品久久久久久宅男 | 免费在线观看一级毛片 | 免费国产视频在线观看 | 四虎影院在线免费观看 | 一二三区视频 | 成人免费黄色 | 亚洲精品久久久一区二区三区 | 羞羞视频在线免费 | 麻豆91精品91久久久 | av在线一区二区三区 | 久草免费在线视频 | 成人精品一区亚洲午夜久久久 | 亚洲国产一区二区视频 | 国产福利在线视频 | 国产精品美女一区二区三区 | 久久精品国产亚洲a | 久久免费精品视频 | 亚洲中午字幕 | 国产成人综合网 | 欧美精品tv | 一区二区免费看 | 女人毛片a毛片久久人人 | 亚洲 中文 欧美 日韩 在线观看 | 中文字幕第十五页 | 久久精品一级 | av福利网站 | 国产精品国产精品国产专区不卡 | 日韩欧美一区二区在线播放 | 黄色片在线免费看 | www.色.com| 久久久久久久久久久高潮一区二区 | 亚洲天天 | 精品一区二区久久久久久久网站 | 色秀网站 | 亚洲三区视频 | 黄色网址大全在线观看 | 国产精品精品视频一区二区三区 | 91天堂网| 福利片在线观看 | 久久久久国产一区二区三区 |