Go Down

Topic: Read string data from ethernet client (Read 1 time) previous topic - next topic

Ervin

Hi everyone.

I have an TCP/IP Ethernet client written in C- Sharp, and i have an Arduino TCP/IP Ethernet Server.
I have this small function, for returning data from client( in String format ).
When i sent data to Server for example: AAAA , the Serial monitor display this: AAAÿ , the last character of String will be this ÿ.( dosen't matter the length of String )
I don't know what is wrong in my code.

Code: [Select]

void loop()
{
  EthernetClient client = server.available();
.
.

.
call my function
Serial.println( convertClientCommandToString( client ) );
.
.
.
}//end loop
String convertClientCommandToString( EthernetClient client ) {
 
  String clientCommand;
  clientCommand= "";
 
  if (client.available() > 0)
  {
    int h = client.available()+1; // length of the command ????
 
    for (int i = 0; i < h; i++)
    {
      clientCommand+= (char)client.read();
    }
   
    return clientCommand;
  }
  else
  {
    return "#";//no commands // command length == 1 ???
  }
}

billroy

The code reads more characters than are available because you tell it to:
Code: [Select]

   int h = client.available()+1; // length of the command ????


That Y-umlaut character is what you see when you call Serial.read() but its buffer is empty.

-br


Ervin

Thanks your reply.
I erased the ' +1 ' from end of line. But now when i send 4 character to Server i got just 3,( i attached a picture )
Code: [Select]

    int h = client.available();

SurferTim

Maybe all the characters have not arrived yet. I would use a terminating character to align the data, like a newline character, then read until you encounter it. Instead of sending the equivalent of client.print("AAAA"), use client.print("AAAA\n").

Ervin

I got the problem, before i called the convertClientCommandToString(), i had called a char c =  client.read() command, and it read the first character.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy