极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11144|回复: 0

求教:如何用W5100实现上下位通讯?

[复制链接]
发表于 2015-6-2 14:54:39 | 显示全部楼层 |阅读模式
最近做一个项目需要通过以太网通讯,想问一下下位在接收是需不需要进行超时判断?如果哪位能提供一个简单的示例,感激不尽。

贴一下刚完成的接收函数(未测试)
  1. //W5100 variable define
  2. #define SF_WAIT_FRAME_HEAD                0X01
  3. #define SF_WAIT_FRAME_LEN                        0X02
  4. #define SF_WAIT_FRAME_CMD                        0X03
  5. #define SF_WAIT_FRAME_DATA                0X04
  6. #define SF_WAIT_FRAME_CHK                        0X05

  7. boolean DataReceivedOK = false;
  8. boolean alreadyConnected = false;
  9. boolean EnthernetIsOK = false;
  10. byte PacketBuffer[30];
  11. unsigned int LocalPort = 8888;

  12. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  13. IPAddress ip(192, 168, 1, 177);
  14. IPAddress gateway(192, 168, 1, 1);
  15. IPAddress subnet(255, 255, 255, 0);
  16. IPAddress Center(192, 168, 1, 113);
  17. // Enter the IP address of the server you're connecting to:
  18. IPAddress server(192, 168, 1, 113);
  19. EthernetServer AlarmServer;
  20. // Initialize the Ethernet client library
  21. // with the IP address and port of the server
  22. // that you want to connect to (port 23 is default for telnet;
  23. // if you're using Processing's ChatServer, use  port 10002):
  24. EthernetClient AlarmClient;
  25. byte ReceiveBuf[100];
  26. byte SendBuf[]={0xaa,0x03,0x05,0x28,0x11,0xbb};
  27. String str="";
  28. void setup()
  29. {
  30.         // start the Ethernet connection:
  31.         Ethernet.begin(mac, ip);
  32.         // Open serial communications and wait for port to open:
  33.         Serial.begin(9600);
  34.         // if you get a connection, report back via serial:
  35.         if (AlarmClient.connect(server, 9050))
  36.         {
  37.                 Serial.println("connected");
  38.         }
  39.         else
  40.         {
  41.                 // if you didn't get a connection to the server:
  42.                 Serial.println("connection failed");
  43.         }
  44. }

  45. void loop()
  46. {
  47.         // if there are incoming bytes available
  48.         // from the server, read them and print them:
  49.         if (client.available())
  50.         {
  51.                
  52.                 /*static char sn=0;
  53.                 static char len=0;*/
  54.                 char c = client.read();
  55.                 Serial.println(c);
  56.                 if (c == 0xaa)
  57.                 {
  58.                         Serial.println("Get Frame HEAD 0xAA");
  59.                 }
  60.                 else if (c == 0xbb)
  61.                 {
  62.                         Serial.println("Get Frame Length 0xBB");
  63.                 }
  64.                 else
  65.                 {
  66.                         Serial.print("Get other Byte value:");
  67.                         Serial.println(c);
  68.                 }

  69.         }

  70.         // as long as there are bytes in the serial queue,
  71.         // read them and send them out the socket if it's open:
  72.         while (Serial.available() > 0)
  73.         {
  74.                 char inChar = Serial.read();
  75.                 if (client.connected())
  76.                 {
  77.                         client.print(inChar);
  78.                 }
  79.         }

  80.         for(int i=0;i<6;i++)
  81.         {
  82.                 client.write(SendBuf[i]);
  83.         }
  84.         delay(1000);
  85.         // if the server's disconnected, stop the client:
  86.         if (!client.connected())
  87.         {
  88.                 Serial.println();
  89.                 Serial.println("disconnecting.");
  90.                 client.stop();
  91.                 // do nothing:
  92.                 while (true);
  93.         }
  94. }

  95. void EthernetInit()
  96. {
  97.         Ethernet.begin(mac, ip, gateway, subnet);
  98.         AlarmServer.begin();
  99. }

  100. void AlarmServerStart()
  101. {
  102.         EthernetClient client = AlarmServer.available();

  103.         if (client)
  104.         {
  105.                 if (!alreadyConnected)
  106.                 {
  107.                         client.flush();
  108.                         client.println("Hello,client!");
  109.                         alreadyConnected = true;
  110.                 }

  111.                 if (client.available()>0)
  112.                 {
  113.                         char thisChar = client.read();
  114.                         AlarmServer.write(thisChar);
  115.                 }
  116.         }
  117. }
  118. //连接server
  119. void AlarmClientStart()
  120. {
  121.         if (AlarmClient.connected())
  122.         {
  123.                 EnthernetIsOK = true;
  124.                 Serial.println("Connected");
  125.         }
  126.         else
  127.         {
  128.                 EnthernetIsOK = false;
  129.                 AlarmClient.connect(Center, LocalPort);
  130.         }
  131. }
  132. //读取一个字节
  133. byte AlarmClientRead()
  134. {
  135.         byte c;
  136.         if (AlarmClient.available())
  137.         {
  138.                 c = AlarmClient.read();
  139.                 FrameStateMachine(c);
  140.                 //Serial.print(c);
  141.         }
  142.         return c;
  143. }
  144. //接收报文状态机
  145. /*
  146. 报文格式:0xAA   len    cmd    data  check
  147. 0xaa : 报文头
  148. len:data区数据长度,不超过20
  149. cmd:命令字,一个byte
  150. data:len个byte
  151. check:校验字,等于len^cmd^data
  152. */
  153. void FrameStateMachine(byte c)
  154. {
  155.         static byte FSState = SF_WAIT_FRAME_HEAD;
  156.         static byte len = 0;
  157.         static byte CheckSum = 0;
  158.         //static byte TimeOutCounts = 0;//超时计数

  159.         switch (FSState)
  160.         {
  161.         case SF_WAIT_FRAME_HEAD:
  162.                 CheckSum = 0;
  163.                 len = 0;
  164.                 if (c == 0xAA)
  165.                 {
  166.                         PacketBuffer[0] = c;
  167.                         FSState = SF_WAIT_FRAME_LEN;
  168.                 }
  169.                 break;
  170.         case SF_WAIT_FRAME_LEN:
  171.                 if (c <= 0x14 && c >= 0x00)                                                                                                //数据长度不能大于20个字节
  172.                 {
  173.                         PacketBuffer[1] = c;
  174.                         CheckSum = CheckSum^PacketBuffer[1];
  175.                         FSState = SF_WAIT_FRAME_CMD;
  176.                 }
  177.                 else
  178.                 {
  179.                         FSState = SF_WAIT_FRAME_HEAD;
  180.                 }
  181.                 break;
  182.         case SF_WAIT_FRAME_CMD:
  183.                 if (c>0x01 && c<0x40)                                                                //command word is between 0x01 and 0x40
  184.                 {
  185.                         if (PacketBuffer[1]>0)
  186.                         {
  187.                                 PacketBuffer[2] = c;
  188.                                 CheckSum = CheckSum^PacketBuffer[2];
  189.                                 FSState = SF_WAIT_FRAME_DATA;
  190.                         }
  191.                         else
  192.                         {
  193.                                 PacketBuffer[2] = c;
  194.                                 CheckSum = CheckSum^PacketBuffer[2];
  195.                                 FSState = SF_WAIT_FRAME_CHK;                                        //如果数据长度为0,则进入校验值接收
  196.                         }
  197.                 }
  198.                 else
  199.                 {
  200.                         FSState = SF_WAIT_FRAME_HEAD;
  201.                 }
  202.                 break;
  203.         case SF_WAIT_FRAME_DATA:
  204.                 PacketBuffer[2 + len] = c;
  205.                 CheckSum = CheckSum^ c;
  206.                 len++;
  207.                 if (len>PacketBuffer[1])
  208.                 {
  209.                         FSState = SF_WAIT_FRAME_CHK;
  210.                 }
  211.                 break;
  212.         case SF_WAIT_FRAME_CHK:
  213.                 if (c == CheckSum)
  214.                 {
  215.                         FSState = SF_WAIT_FRAME_HEAD;
  216.                         DataReceivedOK = true;
  217.                 }
  218.                 else
  219.                 {
  220.                         FSState = SF_WAIT_FRAME_HEAD;
  221.                 }
  222.                 break;
  223.         default:
  224.                 FSState = SF_WAIT_FRAME_HEAD;
  225.                 break;
  226.         }
  227. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 17:50 , Processed in 0.034296 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表