C#从Arduino WI-FI接收数据

时间:2017-10-22 06:17:31

标签: c# arduino wifi arduino-uno networkstream

我有一个C#程序从Arduino传感器接收数据(gps,roll,pitch ......)。使用串行连接(COM / USB)时,一切正常。我将WIFI模块连接到Arduino并使用套接字发送/接收。我可以知道如何通过无线网络从Arduino连续读取/接收数据吗? (我知道发送部分但不知道接收部分)。我需要帮助将接收部分(serialport)转换为WIFI NetworkStream。

这是我的C#winform代码的一部分

int HostPort = 4998;
string Host = "192.168.0.2";
System.Net.Sockets.TcpClient tcpClient = new  System.Net.Sockets.TcpClient();
private void button2_Click(object sender, EventArgs e) {
  byte[] M1bytesToSend = new byte[1] { 0xFF };
  NetworkStream networkStream = tcpClient.GetStream();
  networkStream.Write(M1bytesToSend, 0, M1bytesToSend.Length);
}
private void Form1_Load(object sender, EventArgs e) {
  tcpClient.Connect(Host, HostPort);
}

serialport1的接收部分C#:

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) {
  Thread.Sleep(50);
  int bytes = serialPort1.BytesToRead;
  string data = serialPort1.ReadExisting();
  Char delimiter = '*';
  String[] substrings = data.Split(delimiter);
  if ((bytesRx > 25) & (bytesRx < 40)) {
    //receive 2nd part Arduino code and do something
  }
  if ((bytesRx > 15) & (bytesRx < 25)) {
    //receive 1st part arduino code and do something
  }
}

这是我的Arduino代码,它将不断向Winform发送数据。

Serial.print("*"); // 1st part
ypr[1]=(ypr[1] * 180/M_PI)+90;
Serial.print(ypr[1]);
Serial.print("*");
ypr[2]=(ypr[2] * 180/M_PI)+90;
Serial.print(ypr[2]);
Serial.print("*");
Serial.print(ypr[0] * 180/M_PI);//yaw
Serial.print("*");
delay(50);
Serial.print("*"); //second part
Serial.print(headingDegrees);
Serial.print("*");
Serial.print(b,6);
Serial.print("*");
Serial.print(c,6);
Serial.print("*");

0 个答案:

没有答案