Problem regarding serial read sets of string

Please help me I need to read serial data from the serial port of unknown Size and need to store the data and retrieve it

what baud rate is the serial data being received?
the critical thing is can you receive data and store it without loosing information
after receiving you could store data locally on an SD card, send it over a serial link to a PC or transmit over wireless to a PC or the internet
have a look at

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

There is a big difference between an unknown size where the max can easily be stored within the small SRAM of an Arduino and where the max size is much larger than that - as might be the case with the data for a web page. My examples assume that the data can fit in the Arduino memory.

...R

@horance @Robin thank you so much for the response.
baud rate is 1200
The thing is I get data from a microcontroller through a serial interface to Arduino. The data sent by the microcontroller is a multi-line String so I need to store the data and use some delimiter to separate the desired data from that pool of strings and according to that data I need to control my entire system.

I'm familiar with the basic serial communication. The above is my objective. Could you suggest some code or a method to do it?

Thank you in advance.

1200 baud is about 100 characters/second so you should have no problems with overruns
I assume you are using \n or \r as the end of line on incomming strings
do you know how long the strings are ?
I would tend to double buffer, i.e. have two byte arrays of the maximum length be filling one while processing the other
the alternative is to use a ring buffer

beninben:
so I need to store the data and use some delimiter to separate the desired data from that pool of strings and according to that data I need to control my entire system.

It would be much easier to help if you post an example of the data that is arriving at your Arduino.

I'm not sure if you are using "delimiter" in the sense of end-marker (end of message) or in the sense of separator between parts of a message. Both concepts are covered in my examples.

...R

To AirCon*

Solar -> Ip_Volt: 0.000000 Op_Volt : 0.000000 Ip_Curr : 0.000000 Op_Curr : 0.000000

Grid -> Ip_Volt: 215.000000 Op_Volt : 51.099998 Ip_Curr : 0.200000 Op_Curr : 0.500000

Battery -> Volt : 53.964153 Curr : 0.733508 Char_Power : 37.878307 Dis_Power : 0.000000 Batt_SOC : 90.500000

Time: 20:40:46
Date: 18-10-16

Time: 20:40:46

Time: 20:40:51
Date: 18-10-16

Time: 20:40:51

SMC_stat:DPAU_stat = 0:1

Time: 20:40:56
Date: 18-10-16

Time: 20:40:56

This is the data I'm receiving
The thing is i need the values such as ip_volt: op_volt and store it in an array and then according to that my system should run. I was planning to use some keywords such stringtok as in "c"(i dont know that would work here in arduino). Hope this information helps you to help me

you could use strtok() to tokenise strings such as
Solar -> Ip_Volt: 0.000000 Op_Volt : 0.000000 Ip_Curr : 0.000000 Op_Curr : 0.000000
using space as the delimeter then look for tokens such as Ip_Volt: and the next toen is the number you require
e.g.

   char str[] = "Solar -> Ip_Volt: 0.000000 Op_Volt : 0.000000 Ip_Curr : 0.000000 Op_Curr : 0.000000  ";
   char *token = strtok(str, " ");
   /* walk through other tokens */
   while( token != NULL )
   {
      printf( " %s\n", token );
      token = strtok(NULL, " ");
   }

gives

 Solar
 ->
 Ip_Volt:
 0.000000
 Op_Volt
 :
 0.000000
 Ip_Curr
 :
 0.000000
 Op_Curr
 :
 0.000000

Yeah, thanks for the code!! but the problem is I receive the entire thing through the serial port so I cannot directly initialize the char array since I don't know the exact size of what I'm going to receive.

So this is the plan on my mind tell me if i'm wrong.
I'll receive the data through serial port and store it in some hash table or ring buffer as you have suggested and then by using the stringtok() and tokenize and store the numbers in an integer and use in my system.

if my plan is wrong please suggest me a plan to do it.

you could use a ring buffer but there may be a simpler approach

  1. as the characters arrive extract text delimeted by spaces into a char array or a string
  2. if the extracted text is Ip_Volt: or Op_Volt : the next text to arrive is a number
    otherwise go back to 1

beninben:
Yeah, thanks for the code!! but the problem is I receive the entire thing through the serial port

It looks like the data has lots of linefeed characters so just read it one line at a time. The Arduino will be well able to parse one line before all the data for the next line arrives and data is lost.

Try the second example in Serial Input Basics and see what output it produces. I reckon it will receive the lines one by one.

Then you can parse the line as suggested by @horace - his concept is pretty much the same as the parse example in my tutorial.

...R

I would do it without storing any of the string. I would write it as a state machine. When you receive an "S" you are probably reading the first part of "Solar". If the very next character is an "o" then you definitely are. Remember the Arduino may start at any point in the message so you might be reading the "S" of "SMC" or "SOC".

Then when you've recognised "Solar" you can look for the "Ip_Volt: " (including the trailing space). When you've recognised that, then you can start feeding the digits into a number. If you get anything else which can't be part of the Solar line, such as a carriage-return or the "G" in "Grid" then you know you've finished that line and you're no longer in the "Solar" state.

Remember that it could get unplugged and re-plugged at any time so your code should allow for any possible combination of input, such as "SolaTim...". (The big gap in time between "a" and "T" is not usually measured by this kind of serial parser.) Usually the reaction to that sort of thing is to go back to the start state which is waiting for the "S" and you pick up the next line which comes in 'clean'.

I won't answer technical private messages, Ben. Ask your question again here and maybe someone else can also benefit from the answer.

@morgan could you please send me a sample code?

noiasca:
it's NOT working code, but just a hint of how it could be done. But morgans idea is for shure far better!

It seems to me not much different from the WORKING code in the second example in Serial Input Basics

And I don't see how it relates to @MorganS's comments in Reply #11 - with which I don't happen to agree :slight_smile: (But there is room in the world for different ways of doing things)

...R

noiasca:
The nice thing about #11 (for me) is to not waste RAM for text buffering 111 byte or more.

Have you some better use for the RAM?

...R