WiFiClient.Read() returns 每发送一个字符数 1
WiFiClient.Read() returns for each character sent number 1
我正在尝试使用 TCP 向 ESP32 发送数据。该程序是用 arduino IDE 编写的。为了发送数据,我使用了一个名为 Packet Sender 的应用程序。该程序应将数据打印到串行并返回到 WiFiCLient。
每当我向 ESP32 发送数据时,它都会为发送的每个字节打印“1”。 “已接收数据”消息已正确发送。
我正在使用这个代码
#include <WiFi.h>
const uint ServerPort = 23;
WiFiServer Server(ServerPort);
WiFiClient RemoteClient;
const char *SSID = "SSID";
const char *WiFiPassword = "Password";
void ConnectToWifi()
{
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, WiFiPassword);
Serial.print("Connecting to "); Serial.println(SSID);
uint8_t i = 0;
while (WiFi.status() != WL_CONNECTED)
{
Serial.print('.');
delay(500);
if ((++i % 16) == 0)
{
Serial.println(F(" still trying to connect"));
}
}
Serial.print(F("Connected. My IP address is: "));
Serial.println(WiFi.localIP());
}
void CheckForConnections()
{
if (Server.hasClient())
{
Serial.println("Client spoted");
if (RemoteClient.connected())
{
Serial.println("Connection rejected");
Server.available().stop();
}
else
{
Serial.println("Connection accepted");
RemoteClient = Server.available();
}
}
}
void setup() {
Serial.begin(9600);
ConnectToWifi();
Server.begin();
}
void loop() {
// RemoteClient = Server.available();
CheckForConnections();
if(RemoteClient)
{
uint8_t ch;
bool newDataReceived = false;
while(ch = RemoteClient.read() != -1 ){
Serial.print(ch);
RemoteClient.write(ch);
newDataReceived = true;
}
if(newDataReceived){
Serial.println();
RemoteClient.print("Received data");
}
}
}
感谢您的回答,
雅库布
while (ch = RemoteClient.read() != -1 )
被编译器解释为:
ch = (RemoteClient.read() != -1)
右边部分的计算结果为 TRUE 或 FALSE,即。 1 或 0。1 表示 TRUE 就是您看到的打印内容。
所以,添加括号让它做你想做的事:
(ch = (RemoteClient.read()) != -1)
我正在尝试使用 TCP 向 ESP32 发送数据。该程序是用 arduino IDE 编写的。为了发送数据,我使用了一个名为 Packet Sender 的应用程序。该程序应将数据打印到串行并返回到 WiFiCLient。
每当我向 ESP32 发送数据时,它都会为发送的每个字节打印“1”。 “已接收数据”消息已正确发送。
我正在使用这个代码
#include <WiFi.h>
const uint ServerPort = 23;
WiFiServer Server(ServerPort);
WiFiClient RemoteClient;
const char *SSID = "SSID";
const char *WiFiPassword = "Password";
void ConnectToWifi()
{
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, WiFiPassword);
Serial.print("Connecting to "); Serial.println(SSID);
uint8_t i = 0;
while (WiFi.status() != WL_CONNECTED)
{
Serial.print('.');
delay(500);
if ((++i % 16) == 0)
{
Serial.println(F(" still trying to connect"));
}
}
Serial.print(F("Connected. My IP address is: "));
Serial.println(WiFi.localIP());
}
void CheckForConnections()
{
if (Server.hasClient())
{
Serial.println("Client spoted");
if (RemoteClient.connected())
{
Serial.println("Connection rejected");
Server.available().stop();
}
else
{
Serial.println("Connection accepted");
RemoteClient = Server.available();
}
}
}
void setup() {
Serial.begin(9600);
ConnectToWifi();
Server.begin();
}
void loop() {
// RemoteClient = Server.available();
CheckForConnections();
if(RemoteClient)
{
uint8_t ch;
bool newDataReceived = false;
while(ch = RemoteClient.read() != -1 ){
Serial.print(ch);
RemoteClient.write(ch);
newDataReceived = true;
}
if(newDataReceived){
Serial.println();
RemoteClient.print("Received data");
}
}
}
感谢您的回答, 雅库布
while (ch = RemoteClient.read() != -1 )
被编译器解释为:
ch = (RemoteClient.read() != -1)
右边部分的计算结果为 TRUE 或 FALSE,即。 1 或 0。1 表示 TRUE 就是您看到的打印内容。
所以,添加括号让它做你想做的事:
(ch = (RemoteClient.read()) != -1)