Reading for serial to array

Hi

I have a problem about reading for serial to array, i need when i sent "A:1;B:2;C:3;D:4" from my pc to Arduino ,I need separate from "A" to "D" and ";" to A=1,B=2,C=3,D=4 int or sometime i sent "B:2" to B=2 only or another.

This my code but it's not work =(

char string[32];
char Data[10];
int A,B,C,D;

int availableBytes = Serial.available();
for(int i=0; i<availableBytes; i++)
{
    string[i] = Serial.read();
            if(string[i] == 'A')
            {
                Data[0] = string[i+1] ;
               
            }    
           if(string[i] == 'B')
            {
                Data[1] = string[i+1] ;
               
            } 
           if(string[i] == 'C')
            {
                Data[2] = string[i+1] ;
               
            } 
           if(string[i] == 'D')
            {
                Data[3] = string[i+1] ;
               
            } } 
A=atoi(Data[0]);
B=atoi(Data[1]);
C=atoi(Data[2]);
D=atoi(Data[3]);

Someone can you help me.
Thanks so much. :blush:

That isn't all of your code, but one problem I see is that "availableBytes" will almost always only ever have the value 1 or 0.

Depending on the scope of your variables, ( which is why it is useful to post all code), you could have other issues.

this is my code but it's not work;

//setting................

char string[30],sum[10]="";

//void loop..............

if (client.available() > 0)
{

int availableBytes = client.available();
for(int i=0; i<availableBytes; i++)
{

string = client.read();

  • if(StartA)*

  • { *

  • sum[0]=string[i+1];*

  • tmp2=atoi(sum); *

  • client.println(sum);*

  • // j++;*

  • } *

  • if(StartB)*

  • { *

  • sum[0]=string[i+1];*

  • tmp2=atoi(sum); *

  • } *

_ if(string == 'A')_
* {*
* StartA=true;*
* }*
_ if(string == 'B')
* {
StartB=true;
}
if(string == 'C')
{
StartC=true;
}
if(string == 'D')
{
StartD=true;
}*_

_ if(string == ';')
* {
//Stop=true;*_

* StartA=false;*
* StartB=false;*
* StartC=false;*
* StartD=false; *
* // j=0; *
* } [/quote]*

this is my code but it's not work;

Yes, it does work. It just doesn't do what you expect. Primarily because your expectations are wrong.

First, your topic implies that you are reading from the serial port. The client instance is NOT connected to the serial port.

Second, you have a most unrealistic expectation of how quickly data arrives from the server for the client to read. It arrives relatively slowly. You need to read and store all the data that arrives from the server before you even think about parsing the data.

Third, you seem to think that atoi expects a char array as input. It does not. It expects a string. A string is a char array that is NULL terminated. None of your char arrays are NULL terminated, so you don't have any strings, so you can't use any str functions.

There are plenty of examples that show how to read and store client data, keeping the array NULL terminated. You need to find, and follow, one of them.

When you have all the client data, which won't happen until both client.connected() is false and client.available() is 0, in a NULL terminated char array, then you can worry about parsing it.