I have Mega 2560 connected to WiFi Shield (configured as server). I am using IDE 1.8.1. In my code I want to receive say 2 bytes. The client sends 1 byte at a time. Say client sends b first and then A ideally server should read bA after the second byte sent from client. However, I notice the second value getting corrupted. I am pasting the code below:
#include <SPI.h>
#include<WiFi.h>
int configureSuccess = WL_IDLE_STATUS;
char ssid[] = "xyz";
char pass[] = "*******";
WiFiServer server(20000);
static uint8_t alreadyConnected = 0;
void setup() {
Serial.begin(9600);
Serial1.begin(28800);
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while ( configureSuccess != WL_CONNECTED) {
configureSuccess = WiFi.begin(ssid, pass);
// delay 10000
unsigned long startMillis = millis();
while(millis()- startMillis < 10000)
{
};
}
server.begin();
for(int trialcount=0;trialcount<5;trialcount++)
{
if(Serial)
{
break;
}else
{
unsigned long startMillis = millis();
while(millis()- startMillis < 1000)
{
};
}
}
if (configureSuccess==WL_CONNECTED)
{
IPAddress ip = WiFi.localIP();
Serial.print("<<<IP address: ");
Serial.print(ip);
Serial.println(">>>");
}
else
{
Serial.println("<<< IP address :Failed to configure. >>>");
}
}
void loop() {
// wait for a new client:
int avlBytes = 0;
int st = 0;
int data2[2];
Serial1.println("loop");
WiFiClient client1 = server.available();
if (client1){
if(!alreadyConnected){
client1.flush();
alreadyConnected++;
Serial1.println("Flushing");
}
avlBytes = client1.available();
if(avlBytes >= 2){
st = client1.read(data2,2);
//data2[0] = client1.read();
//data2[1] = client1.read();
Serial1.println("data_s");
Serial1.println(data2[0]);
Serial1.println(data2[1]);
Serial1.println("rx_status");
Serial1.println(st);
}
else{
Serial1.println("rcvd");
Serial1.println(avlBytes);
}
}
else {
alreadyConnected = 0;
Serial1.println("No client");
}
unsigned long startMillis = millis();
while(millis()- startMillis < 1000)
{
};
}
With client1.read(data2, 2) if client send b first then rcvd 1 is printed. If client then sends A then data_s 98 2 is printed and rcvd is still 1. Next time if I send c then data_s 65 2 and rcvd 1.
However, if I use two back-to-back read() (data2[0] = client1.read(); data2[1] = client1.read();) the Serial1 print would be data_s 98 65. Seems to be an issue in ServerDrv::getDataBuf. The method ServerDrv::getData works fine.
I have Mega 2560 connected to WiFi Shield (configured as server). I am using IDE 1.8.1. In my code I want to receive say 2 bytes. The client sends 1 byte at a time. Say client sends b first and then A ideally server should read bA after the second byte sent from client. However, I notice the second value getting corrupted. I am pasting the code below:
With client1.read(data2, 2) if client send b first then rcvd 1 is printed. If client then sends A then data_s 98 2 is printed and rcvd is still 1. Next time if I send c then data_s 65 2 and rcvd 1.
However, if I use two back-to-back read() (data2[0] = client1.read(); data2[1] = client1.read();) the Serial1 print would be data_s 98 65. Seems to be an issue in ServerDrv::getDataBuf. The method ServerDrv::getData works fine.