极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9734|回复: 0

arduino与python通过udp通信(测试通过 初级简单)

[复制链接]
发表于 2014-9-10 19:50:36 | 显示全部楼层 |阅读模式
本帖最后由 zhaopengxslc 于 2014-9-10 20:31 编辑

Arduino代码
  1. /*
  2.   UDPSendReceive.pde:
  3. This sketch receives UDP message strings, prints them to the serial port
  4. and sends an "acknowledge" string back to the sender

  5. A Processing sketch is included at the end of file that can be used to send
  6. and received messages for testing with a computer.

  7. created 21 Aug 2010
  8. by Michael Margolis

  9. This code is in the public domain.
  10. */


  11. #include <SPI.h>         // needed for Arduino versions later than 0018
  12. #include <Ethernet.h>
  13. #include <EthernetUdp.h>         // UDP library from: [email][email protected][/email] 12/30/2008


  14. // Enter a MAC address and IP address for your controller below.
  15. // The IP address will be dependent on your local network:
  16. byte mac[] = {  
  17.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  18. IPAddress ip(192, 168, 2, 177);

  19. unsigned int localPort = 8000;      // local port to listen on

  20. // buffers for receiving and sending data
  21. char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  22. char  ReplyBuffer[] = "acknowledged";       // a string to send back

  23. // An EthernetUDP instance to let us send and receive packets over UDP
  24. EthernetUDP Udp;

  25. void setup() {
  26.   // start the Ethernet and UDP:
  27.   Ethernet.begin(mac,ip);
  28.   Udp.begin(localPort);

  29.   Serial.begin(9600);
  30. }

  31. void loop() {
  32.   // if there's data available, read a packet
  33.   int packetSize = Udp.parsePacket();
  34.   if(packetSize)
  35.   {
  36.     Serial.print("Received packet of size ");
  37.     Serial.println(packetSize);
  38.     Serial.print("From ");
  39.     IPAddress remote = Udp.remoteIP();
  40.     for (int i =0; i < 4; i++)
  41.     {
  42.       Serial.print(remote[i], DEC);
  43.       if (i < 3)
  44.       {
  45.         Serial.print(".");
  46.       }
  47.     }
  48.     Serial.print(", port ");
  49.     Serial.println(Udp.remotePort());

  50.     // read the packet into packetBufffer
  51.     Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
  52.     Serial.println("Contents:");
  53.     Serial.println(packetBuffer);
  54.     memset(packetBuffer,'\0',UDP_TX_PACKET_MAX_SIZE);

  55.     // send a reply, to the IP address and port that sent us the packet we received
  56.     Udp.beginPacket(Udp.remoteIP(),Udp.remotePort());
  57.     Udp.write(ReplyBuffer);
  58.     Udp.endPacket();
  59.   }
  60.   delay(10);
  61. }
复制代码


python代码
[pre lang="python" line="1"]from socket import *

HOST="192.168.2.177"
PORT=8000
BUFSIZE=1024
ADDR=(HOST,PORT)

udpCliSock=socket(AF_INET,SOCK_DGRAM)
while True:
    data=raw_input(">")
    if not data:
        break
    udpCliSock.sendto(data,ADDR)
    data,ADDR=udpCliSock.recvfrom(BUFSIZE)
    if not data:
        break
    print data,ADDR
udpCliSock.close()
[/code]

说明:
arduino作为UDP服务器
python作为UDP客户端
回复

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 09:33 , Processed in 0.045754 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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