Pages: [1]   Go Down
Author Topic: Read string data from ethernet client  (Read 728 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 ???
  }
}
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 986
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code reads more characters than are available because you tell it to:
Code:
   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

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
    int h = client.available();


* arduino.png (537.59 KB, 1387x648 - viewed 40 times.)
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5367
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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").
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: