Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arduino IDE 1.8.1 WiFi library multiple byte TCP read function read(*databuf, datasize) gives wrong values #30

Open
Sudeshna19 opened this issue Jun 23, 2017 · 0 comments

Comments

@Sudeshna19
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant