Send recive data separated by comma

String read;
String write;
void setup() {
Serial.begin(9600);
}

void loop()
{
while(Serial.available()){
read= Serial.readString();
write= Serial.writeString();

}
}

how i can recive data?
example my data is :
a,b,c
a and b and c is number from 0 to 9999
a and b and c after recived put into variable:
sensor1=a
sensor2=b
sensor3=c

how i can send data?
example my data is :
a,b,c
a and b and c is number from 0 to 9999
a and b and c must sended by serial
sensor1=a
sensor2=b
sensor3=c

hi mohammad,

welcome to the arduino-forum.
For a small demo-code that shows the principle additional information is needed
at what baudrate is the data sended?
is there some kind of endmarker that is added after the number?

What device is sending the data?
another arduino?
a computer?

best thing would be if you could post a real example of the data

best regards
Stefan

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly ➜ please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. (also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac)

Would you mind to learn first how to receive (transmit) only one character from the InputBox (to the OutputBox) of the Serial Monitor (Fig-1) and then learn multi-characters (the string)?


Figure-1:

I can send and receive between Arduino and LabVIEW successful.
But I want send this form:
a,b,c
A and b and c is variable.
And my received data from LabVIEW is a,b,c format .
After received in Arduino I can put into sensor variable,but I can't separate sensor=a,b,c and put into sensor1-3.

consider

#define MAX_TOKS   10
char * toks [MAX_TOKS];
float  vals [MAX_TOKS];

int
tokenize (
    char *t )
{
    char s [80];
    int  i = 0;

    printf ("%s: %s\n", __func__, s);

    strcpy (s, t);

    for (toks [i] = strtok (s, ","); toks [i]; )  {
        vals [i] = atof (toks [i]);
        toks [++i] = strtok (NULL, ",");
    }

    return i;
}

// -----------------------------------------------------------------------------
const char t [] = "12.3,23,45.6,345,67";

void
setup (void)
{
    Serial.begin (9600);

    int nTok = tokenize ((char*) t);

    for (int n = 0; n < nTok; n++)
        Serial.println (vals [n]);
}

void
loop (void)
{
}

The comma-delimited format is often known as System Data Format, SDF. This is the format used in GPS NMEA, so GPS libraries offer a good view into how to parse SDF streams.


  if (strstr(nmea, "$GPRMC")) {
   // found RMC
    char *p = nmea;

    // get time
    p = strchr(p, ',')+1;
    float timef = atof(p);
    uint32_t time = timef;
    hour = time / 10000;
    minute = (time % 10000) / 100;
    seconds = (time % 100);

    milliseconds = fmod(timef, 1.0) * 1000;
    p = strchr(p, ',')+1;  // A/V?
    p = strchr(p, ',')+1;  // lat
    p = strchr(p, ',')+1;  // N/S?
    p = strchr(p, ',')+1;  // lon
    p = strchr(p, ',')+1;  // E/W?
    p = strchr(p, ',')+1;  // speed
    p = strchr(p, ',')+1;  // angle

    p = strchr(p, ',')+1;
    uint32_t fulldate = atof(p);
    day = fulldate / 10000;
    month = (fulldate % 10000) / 100;
    year = (fulldate % 100);
    // we dont parse the remaining, yet!
    return true;
  }

  return false;
}

Robin's Serial Input Basics - updated might be a good read. It does not use String objects but it can give you the idea.

Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code. This is described in How to get the best out of this forum.

the support to you could be much more specific if you would post the byte.seqeunce
byte for byte that your labVIEW-computer is sending

additionally there can be differencies in

  • odd or even parity,

  • 7 or 8 databits,

  • no stopbit, 1 stopbits two stopbits

  • no line-ending

  • cr as line-ending

  • nl as line-ending

  • cr and nl as line-ending

Is it possible to create an asynchronous frame without a Stop Bit? There is also the option for 1.5 stopbits.

Or parity OFF (disabled).

how do you guarantee that the start bit is the opposite value?

185-00

hence the "requirement" for a stop bit

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.