如何将C#套接字与ESP8266(硬件TCP)互连

时间:2019-03-14 01:03:33

标签: c# tcp esp8266 nodemcu

我正在尝试为我已经开发的ESP8266项目创建GUI。开发板上使用的是NodeMCU固件,我正在通过ESP8266 Arduino Core加载程序。我在Windows上的GUI程序中使用的是C#。

这是我的ESP8266调试代码(在Arduino中):

#include <ESP8266WiFi.h>
String ConnectedMAC[8];
IPAddress ConnectedIP[8];
WiFiClient clientforcom;
void setup() {
  WiFi.mode(WIFI_AP_STA);
  Serial.begin(9600);
  int f = WiFi.scanNetworks();
  Serial.println("Scanning Networks");
  delay(5000);
  for (int n = 0; n < f; n++) {
    Serial.println(WiFi.SSID(n));
  }
  WiFi.begin(ssid, passtring); //SSID and passtring are my home network credentials
  delay(2000);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.print("The IP of this node is: ");
  Serial.println(WiFi.localIP());
}


void loop() {

  MACManagement();
  WiFiServer serverforclient(WiFi.localIP(), 25565);
  while (!serverforclient.hasClient()) { //this does not work with Socket or delay is too long
    delay(50);
    Serial.println("Waiting for client");
  }
  clientforcom = serverforclient.available();
  while (clientforcom.available() <= 0) {
    delay(50);
    Serial.println("Waiting for response");
  }
  String response = clientforcom.readString();
  if (response.startsWith("GetController")) {
    Serial.println("Sending String");
    clientforcom.println("Returned String");
  }
}


void MACManagement() {

  unsigned char number_client;
  struct station_info *stat_info;

  struct ip_addr *IPaddress;
  IPAddress address;
  int i = 0;

  number_client = wifi_softap_get_station_num();
  stat_info = wifi_softap_get_station_info();

  Serial.print(" Total Connected Clients are = ");
  Serial.println(number_client);

  while (stat_info != NULL) {

    IPaddress = &stat_info->ip;
    address = IPaddress->addr;

    Serial.print("client= ");

    Serial.print(i);
    Serial.print(" IP adress is = ");
    Serial.print((address));
    ConnectedIP[i] = address;
    Serial.print(" with MAC adress is = ");

    Serial.print(stat_info->bssid[0], HEX); Serial.print(" ");
    Serial.print(stat_info->bssid[1], HEX); Serial.print(" ");
    Serial.print(stat_info->bssid[2], HEX); Serial.print(" ");
    Serial.print(stat_info->bssid[3], HEX); Serial.print(" ");
    Serial.print(stat_info->bssid[4], HEX); Serial.print(" ");
    Serial.print(stat_info->bssid[5], HEX); Serial.print(" ");
    ConnectedMAC[i] = String(stat_info->bssid[0], HEX) + ":" + String(stat_info->bssid[1], HEX) + ":" + String(stat_info->bssid[2], HEX) + ":" + String(stat_info->bssid[3], HEX) + ":" + String(stat_info->bssid[4], HEX) + ":" + String(stat_info->bssid[5], HEX);
    stat_info = STAILQ_NEXT(stat_info, next);
    i++;
    Serial.println();
  }
}

这是我在C#中的网络访问代码:

void loadUI(){
            enterIPLabel.Visible=false;
            errorBox.Text="Connecting to Leader node";
            tcpcom=new TcpClient(leadernode.ToString(),25565);
            tcpcom.Connect(leadernode.ToString(),25565);
            while(!tcpcom.Connected);

            tcpcom.Client.Send(System.Text.Encoding.ASCII.GetBytes("GetController"));
            errorBox.Text="Awaiting Node Response";
            while(tcpcom.Available<=0); //hopefully this should stop this method from executing until the response is made
            //FILL IN CODE FOR RETURN STATEMENT
            tabController.Visible=true;
            NodeMap.Visible=true;
        }

但是,当我到达ESP8266代码的“ Waiting for Client”部分并运行C#程序时,请在L​​eadernode中输入IP地址。我从.NET收到一个错误,指出:

System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it

我不知道为什么ESP8266会拒绝连接,我设置了延迟,以至于无法启动Software Watch Dog,所以我不明白为什么它会中断程序。

0 个答案:

没有答案