【问题标题】:How to Send Data from Arduino to PC如何将数据从 Arduino 发送到 PC
【发布时间】:2015-06-09 22:54:01
【问题描述】:

我是 arduino 的新手。
我有一个响应端口 8888 的服务器端应用程序(C# 应用程序)。
我已使用以太网将 Arduino 与我的笔记本电脑连接起来。
我的笔记本电脑有一个静态 IP 地址 192.168.1.23,我的 Arduino 有 192.168.1.22。
我无法连接到服务器端应用程序。

我有以下 Arduino 代码

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 192, 168, 1, 23 }; // Google

EthernetClient client;

void setup()
{
    Ethernet.begin(mac, ip);
    Serial.begin(9600);

    delay(1000);

    Serial.println("connecting...");
    Serial.println(Ethernet.localIP());

    if (client.connect(server,8888)) {
        Serial.println("connected");
    } else {
        Serial.println("connection failed");
    }
}

【问题讨论】:

  • 您知道您的 C# 服务器应用程序可以正常工作吗?如果是,请将该信息添加到您的问题中,如果否,请添加服务器端代码。
  • 你得到什么错误
  • 是的 Jan Doggen,它工作正常。当我制作 PC 客户端时,它可以正常工作。但是当我使用 arduino 作为客户端时,它无法正常工作。
  • @TobyAllen 我的程序在到达 if (client.connect(IPAddress(192,168,1,23),8888)) 我的服务器端应用程序后连接失败,该应用程序在具有静态 IP 192.168.1.23 的 PC 上运行,响应端口 8888

标签: c# arduino server tcpclient ethernet


【解决方案1】:

您可以检查您的连接是否正常,尝试从 Ardunio ping PC 或防火墙阻止消息?你可以试试这个代码

void loop()
{ 
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    while (client.connected()) {
      if (client.available()) {

}
}
}

【讨论】:

  • Fortan 我想做 arduino 客户端。我的程序在到达 if (client.connect(IPAddress(192,168,1,23),8888)) 我的服务器端应用程序后连接失败,在具有静态 IP 192.168.1.23 的 PC 上运行,响应端口 8888
  • 我没有任何错误。我的程序跳转到 else 语句,即 "Connection fail" 。无法建立连接。 if(client.connect(server,8888) 没有建立连接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多