使用arduino客户端/python服务器通过以太网转储数据

使用arduino客户端/python服务器通过以太网转储数据,python,client-server,arduino,Python,Client Server,Arduino,我使用arduino以太网读取传感器的数据,然后将数据发送到另一栋楼的计算机,以驱动python软件中的逻辑/控制。我决定用python/arduino绘制一个简单的草图,通过以太网将文本从arduino发送到我的python程序: arduino客户端代码主要取自EthernetClient示例: #include <SPI.h> #include <Ethernet.h> byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0x40, 0x9

我使用arduino以太网读取传感器的数据,然后将数据发送到另一栋楼的计算机,以驱动python软件中的逻辑/控制。我决定用python/arduino绘制一个简单的草图,通过以太网将文本从arduino发送到我的python程序:

arduino客户端代码主要取自EthernetClient示例:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0x40, 0x9F};
byte ip[] = {192, 168, 0, 172};
byte server[] = {192,168,0,17};
int port = 1700;

EthernetClient client;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  Ethernet.begin(mac, ip);
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, port)) {
    Serial.println("connected.");
    //print text to the server
    client.println("This is a request from the client.");
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}
服务器的响应符合我的预期:

Listening for client . . .
Connected to client at  ('192.168.0.172', 1025)
Message received from client:
This is a request from the client.
Test message sent and connection closed.
但客户收到:

connecting...
connected.
This
disconnecting.
并且似乎在流之后停止从我的服务器接收文本。为什么呢

另一个问题:为什么我让我的arduino连接端口1700,但python声称它正在接收来自端口1025的请求


谢谢

我不确定,但您是否可以尝试在python代码中的conn.send(“这是服务器的响应”)和conn.close()之后放置一个等待,看看会发生什么?关于端口的问题,socket.accept()返回的地址是连接另一端的地址。i、 e.在arduino的1025端口和主机的1700端口(python代码正在运行)之间建立了连接。感谢您告诉我端口号——我意识到有点晚了,但这是有意义的:)我尝试添加了一个“等待”,但似乎没有多大作用——arduino仍然没有收到整个消息。从那以后,我改用web.py服务器,而不是编写自己的带有端口的服务器,到目前为止,这对我来说效果很好。
connecting...
connected.
This
disconnecting.