Python ConnectionResetError

时间:2018-06-09 13:07:48

标签: python server arduino client arduino-uno

我正在尝试使用Paul McWhorter的客户端服务器模型(https://www.youtube.com/watch?v=TWGV47WrKmQ),但连接会保持超时。我有以下代码:
Arduino:

#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>    
byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xEE};
IPAddress ip(169,254,168,97);
unsigned int localPort = 5000;
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(1500);
}

void loop() {
  packetSize = Udp.parsePacket();

  if(packetSize>0){

    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    String datReq(packetBuffer);

    if(datReq=="Red"){
      Udp.beginPacket(Udp.remoteIP(),Udp.remotePort());
      Udp.print("You are asking for Red");
      Udp.endPacket();
    }
  }
  memset(packetBuffer,0,UDP_TX_PACKET_MAX_SIZE);
}

Python:

import socket
import time

address = ('169.254.168.97',5000)
client_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
client_socket.settimeout(1)

while True:

    data = "Red"
    client_socket.sendto(data.encode('utf-8'),address)
    try:
        rec_data , addr = client_socket.recvfrom(2048)
        print(rec_data)
    except ConnectionResetError:
        print('Did not work')
        break
    time.sleep(2)

服务器可以ping通,但在使用客户端时我从未得到过响应。我怀疑remoteIP和remotePort功能不起作用,但我不知道如何继续。我不是计算机科学专业的学生,​​绝对不是这方面的专家。我有什么误解吗?
我正在使用python 3.5和spyder。

0 个答案:

没有答案