【问题标题】:can't send packets from arduino to python client using UDP socket over ethernet无法使用以太网上的 UDP 套接字将数据包从 arduino 发送到 python 客户端
【发布时间】:2016-03-07 17:42:40
【问题描述】:

我正在尝试在 arduino Galielo Gen 2 和 python client 之间打开一个 UDP 套接字。我想将温度传感器捕获的值从 arduino 发送到客户端并接收来自客户端的响应。

Arduino 代码:

#include <Ethernet.h> //Load Ethernet Library
#include <EthernetUdp.h> //Load UDP Library
#include <SPI.h> //Load the SPI Library

byte mac[] = { 0x98, 0x4F, 0xEE, 0x01, 0xF1, 0xBE }; //Assign a mac address
IPAddress ip( 192,168,1,207);
//IPAddress gateway(192,168,1, 1);
//IPAddress subnet(255, 255, 255, 0);
unsigned int localPort = 5454; 
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; 
String datReq;  
int packetSize; 
EthernetUDP Udp; 

void setup() {
Serial.begin(9600); 
Ethernet.begin(mac, ip);
Udp.begin(localPort); 
delay(2000);
}

void loop() {

   int sensor = analogRead (A0); 
   float voltage = ((sensor*5.0)/1023.0);
   float temp = voltage *100;
   Serial.println(temp);  
  packetSize = Udp.parsePacket();
  if(packetSize>0)
  { 
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

  Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); 
  Serial.println("Contents:");
  Serial.println(packetBuffer);
  String datReq(packetBuffer); 
  Udp.beginPacket(Udp.remoteIP(), 5454 ); 
  Udp.print(temp);
  Udp.endPacket(); 
  }
  delay(50);
}

python 代码:

from socket import *
import time

address = ( '192.168.1.207', 5454) 
client_socket = socket(AF_INET, SOCK_DGRAM) 
client_socket.settimeout(5)

while(1):

    data = "Temperature" 
    client_socket.sendto(data, address)
    rec_data, addr = client_socket.recvfrom(2048)
    print rec_data 

尝试代码后,这是 arduino 上的结果:

收到大小为 11 的数据包 从 255.255.255.255,端口 0 内容: 温度

在 python 上,我收到了这条消息: 回溯(最近一次通话最后): 文件“C:/Users/enwan/Desktop/te/temp.py”,第 12 行,在 rec_data, addr = client_socket.recvfrom(2048) timeout: 超时

有什么帮助吗?

【问题讨论】:

    标签: python sockets arduino udp intel-galileo


    【解决方案1】:

    你还没有初始化运行python代码的电脑地址。

    IPAddress remote = Udp.remoteIP();
    

    这被初始化为地址 255.255.255.255,这不是一个有效的 IP 地址。它似乎没有获取到远程 IP。

    此外,远程端口未在以下行中检索,而是设置为默认值 0:

    Udp.remotePort()
    

    因此,arduino 正在尝试将数据发送到端口 0 的 ip 地址 255.255.255.255。结果,由于 arduino 未正确寻址 PC,python 代码出现超时。

    您需要直接访问您的 python PC,即。设置:

    IPAddress remoteip(192,168,1,X);     // whatever your PC ip address is
    Udp.beginPacket(remoteip, 5454 ); 
    Udp.print(temp);
    Udp.endPacket(); 
    

    UDP 库可能有一种方法可以根据您在 arduino 上收到的数据包设置 ip 和端口,但您必须阅读如何获取该信息。

    【讨论】:

      【解决方案2】:

      您从未在 Python 脚本中调用 bind() 将 UDP 套接字绑定到端口,因此操作系统不知道您希望 UDP 套接字接收任何数据包,因此永远不会传递任何数据包。

      这是你需要的:

      client_socket = socket(AF_INET, SOCK_DGRAM) 
      client_socket.bind(("", portNum))  # where portNum is the port number your Arduino is sending to
      [...]
      rec_data, addr = client_socket.recvfrom(2048)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-17
        • 2018-10-11
        • 1970-01-01
        • 2020-06-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多