【发布时间】:2021-09-08 02:28:00
【问题描述】:
我正在尝试使用 UDP 和 TCP 协议通过 Wifi 将 Adafruit ESP32 Feather 中连续随机生成的像素颜色 (80x60) 发送到我的 C# 程序。我可以通过任何协议毫无问题地发送任何其他类型的数据,但是我创建的这个发送 4800 (80x60) 值的字符串会造成一些困难,尤其是在 UDP 中。
在 TCP 协议中,所有数据都由 ESP32 发送并由 C# 程序接收,尽管该协议非常慢,因此它们获得的视频每秒帧数很少。 在 UDP 的情况下,预计会更快(这是我的巨大希望),生成的数据并没有完全被 C# 程序接收,并且视频只有第一个像素(大约 410 - See Image)
两种协议在 C# 程序中的定义方式相同,只是 TCP 是 Stream 格式,而 UDP 是 Datagrams(无法定义 Stream):
public void initiate_connection(string esp32_ip, string esp32_port, int protocoltype)
{
string msg;
int port;
if (protocoltype == 1)
{
protype = ProtocolType.Udp;
socktype = SocketType.Dgram;
}
else if (protocoltype == 2)
{
protype = ProtocolType.Tcp;
socktype = SocketType.Stream;
}
try
{
s = new Socket(AddressFamily.InterNetwork, socktype, protype);
msg = esp32_ip;
IPAddress adafruit_remote_IP = IPAddress.Parse(msg);
port = int.Parse(esp32_port);
IPEndPoint IP_Endpoint_remote = new IPEndPoint(adafruit_remote_IP, port);
s.Connect(IP_Endpoint_remote);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
catch (ArgumentNullException se)
{
MessageBox.Show(se.Message);
}
catch (ObjectDisposedException se)
{
MessageBox.Show(se.Message);
}
catch (InvalidOperationException se)
{
MessageBox.Show(se.Message);
}
catch (System.Security.SecurityException se)
{
MessageBox.Show(se.Message);
}
}
private string send_command_adafruit(string msg)
{
byte[] sent_data;
byte[] received_data = new byte[10000]; //256
int number_of_received_bytes;
try
{
if (s.Connected)
{
sent_data = System.Text.ASCIIEncoding.ASCII.GetBytes(msg);
s.Send(sent_data);
number_of_received_bytes = s.Receive(received_data);
msg = System.Text.Encoding.ASCII.GetString(received_data, 0, number_of_received_bytes);
// s.Close();
return msg;
}
else
{
MessageBox.Show("Application Message: Socket NOT connect, verify your cables and conections.");
return "-1";
}
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
return "-1";
}
catch (ArgumentNullException se)
{
MessageBox.Show(se.Message);
return "-1";
}
catch (ObjectDisposedException se)
{
MessageBox.Show(se.Message);
return "-1";
}
catch (System.Security.SecurityException se)
{
MessageBox.Show(se.Message);
return "-1";
}
}
另一方面,我在 ESP32 中的代码如下:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include <WiFiUdp.h>
#include <Udp.h>
const char* ssid = "SensorIR_test";
const char* password = "123456789";
WiFiServer server(80);
WiFiUDP Udp;
const int udpPort = 80; // local port to listen on
int reply; // a int to send back
String str_reply, inputs;
String str_reply_UDP[60];
int speedx;
String parameter[6];
int i, j;
int subS_l;
int index_of_S;
void setup() {
Serial.begin(250000);
delay(10);
Serial.print("Setting AP (Access Point)…");
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.begin();
Udp.begin(udpPort);
}
void loop() {
WiFiClient client = server.available(); // Listen for incoming clients
int packetSize = Udp.parsePacket();
inputs = "";
i=1;
j=1;
if (client) {
// If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
client.setNoDelay(1);
//break;
int c=client.read();
// else {
// teste=teste+1;
for (int y=0; y<60; y++) {
for (int k=0; k<80; k++) {
//speedx=random(0,255);
str_reply+=random(0,255);
str_reply+=" ";
}
client.print(str_reply);
Serial.print(str_reply);
client.print(" ");
Serial.print(" ");
}
// delay(1000);
}
}
// Close the connection
client.stop();
Serial.println("TCP communication disconnected.");
Serial.println("");
}
else if (packetSize) {
// teste=teste+1;
for (int y=1; y<61; y++) {
str_reply_UDP[y]="";
}
int d = Udp.read();
// Serial.println(d);
if (d==59) { //ASCII=";"
Serial.println("New UDP Client."); // print a message out in the serial port
}
else if (d==33) { //ASCII="!"
for (int y=0; y<60; y++) {
for (int k=0; k<80; k++) {
str_reply_UDP[y]+=random(0,255);
str_reply_UDP[y]+=" ";
}
}
// str_reply+=" ";
}
// Serial.println(str_reply);
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
for (int u=0; u<61; u++) {
Serial.println(str_reply_UDP[u]);
Udp.println(str_reply_UDP[u]);
}
//////Udp.println(str_reply);
Udp.endPacket();
}
}
有人知道我应该怎么做吗?
【问题讨论】: