Reading data via serial communication

Hi all, i need some help on reading some data from serial monitor and storing them on some variables, (just some numbers)

for example lets suppose we have variables like this

int input[10];

i want to be able to store data that came from serial monitor to the specific variable for example when i send S05150E (on serial monitor) then the atmega must store the number 150 to variable input[5]

so on S05150E S will mean that we are STARTing to send data 05 will mean that we choose to store data on input[5] variable 150 will mean that we chose number 150 to store ( on variable input[5]) E will mean that we inform chip that we are ENDING transmiting

maybe there is a better idea on how to storing data to variables but i dont know any better (if anyone else knows can inform me).

can anyone help me on how can i do that? do i need to cycle all time with many " while" and "if" on each character that coming from data? is there any better and easier way? please give me a syntax

thanks in advance sorry for my bad english

so on S05150E S will mean that we are STARTing to send data 05 will mean that we choose to store data on input[5] variable 150 will mean that we chose number 150 to store ( on variable input[5]) E will mean that we inform chip that we are ENDING transmiting

This is a protocol. A specific definition of the data stream, and a set of instructions for parsing and using the data. Nicely done.

maybe there is a better idea on how to storing data to variables

The first step in defining a method is to define a protocol. You've done that.

can anyone help me on how can i do that?

The next step is to implement that protocol on both the sender and the receiver.

As you mentioned, you'll need a while loop on the Arduino that will read each character that is in the serial buffer. Within that while look, you'll need to test each character to see if it is the start character ('S'), or the end character ('E').

When it is the start character, initialize an array to hold the data (inData[0] = '\0';) and an index into that array (index = 0;). Set a flag (started = true;) that indicates that the start character has been received.

When it is the end character, set a flag (ended = true;) indicating that the end of the packet has been received, and break out of the while loop.

Otherwise, add the character to the array and increment the index, if started and not ended.

After the while loop, add a block to be executed if started and ended. In that block, you'll parse the array (which should contain 05150, for instance) and do something with the parsed data.

Search the forum for "started && ended" for an example of some code.

Something simple to tinker with. The below code captures serial port input into strings. What is sent previous to a "," occurring in the sent string is captured into the readString variable. Then the readString variable can be evaluated for various data contents if desired.

// zoomkat 10-29-10 simple delimited ',' string parce 
// from serial port input (via serial monitor)
// and print result out serial port
// CR/LF could also be a delimiter
// for IDE 0019 and later

String readString;

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

void loop() {

        //expect a string like wer,qwe rty,123 456,hyre kjhg,
        //or like hello world,who are you?,bye!,
        while (Serial.available()) {
        delay(10);  //small delay to allow input buffer to fill
          
        char c = Serial.read();  //gets one byte from serial buffer
        if (c == ',') {break;}  //breaks out of capture loop to print readstring
        readString += c; } //makes the string readString  
            
      if (readString.length() >0) {
      Serial.println(readString); //prints string to serial port out
      
      readString=""; //clears variable for new input
      }
   }

Now try to imagine that post with consistent and logical indentation, and without the line speed-dependent 'delay' (what was that about?)

...without the line speed-dependent 'delay' (what was that about?)

It fixed a problem I was having when sending longer strings for parce testing. Without the delay the string would often be captured as two strings. Adding the slight delay made sure all the string was in the buffer prior to starting to remove the string from the buffer. There are other more complicated ways to do the job, but just the one line of code eliminated my problem.

but just the one line of code eliminated my problem.

It didn't eliminate the problem for all baud rates - only your specific baud rate.

That's why end-of-packet markers are necessary.

It didn't eliminate the problem for all baud rates - only your specific baud rate. That's why end-of-packet markers are necessary.

Reading comprehension issue. I said it fixed my problem, not other problems. Packet markers may fix issues, or may not. The person posting requested a way to capture a strting from the serial port. I'm the only respondant that actually posted some working code that might be helpful. Those that can, do. Those that can't, ... :)

Just for the point of discussion, below is simple code that looks for a delimiter. It also has my 10ms delay and I know it isn't going to work in all situations. My challange for you is to produce simple working "end-of-packet marker" code that can be posted for the new coders to give them something to start with. Tag,you are it! :)

// zoomkat 10-29-10 simple delimited ',' string parce 
// from serial port input (via serial monitor)
// and print result out serial port
// CR/LF could also be a delimiter
// for IDE 0019 and later

String readString;

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

void loop() {

        //expect a string like wer,qwe rty,123 456,hyre kjhg,
        //or like hello world,who are you?,bye!,
        while (Serial.available()) {
        delay(10);  //small delay to allow input buffer to fill
          
        char c = Serial.read();  //gets one byte from serial buffer
        if (c == ',') {break;}  //breaks out of capture loop to print readstring
        readString += c; } //makes the string readString  
            
      if (readString.length() >0) {
      Serial.println(readString); //prints string to serial port out
      
      readString=""; //clears variable for new input
      }
   }

Those that can, do. Those that can't, ...

I just get tired of typing the same code over and over, so I suggested that the OP do a little searching. Using the string I suggested leads to code that works at any baud rate, with no delays, along with an explanation of what the code does, and what needs to be changed to use different start and end packet markers.

What the thoughtful, conscientous programmer would be asking is "why does a ten (ish) character-period sticking-plaster delay make my sketch work, and what can I do to eliminate it, and improve my understanding and remove a waste of processor resource?" Just my tuppence.

I just get tired of typing the same code over and over, so I suggested that the OP do a little searching. Using the string I suggested leads to code that works at any baud rate, with no delays, along with an explanation of what the code does, and what needs to be changed to use different start and end packet markers.

Well, no links were provided, no working code was posted, and no string of code was posted that is the magic bullitt for start/end markers, so that approach appears to be ineffective. Eventually I will include start/end markers in my test code and make it available when needed. As the forum is difficult to search for code and the arduino web pages are lacking in some basic info, I just copy/paste what I have to the discussion. Not very difficult as the code is available when I open the arduino IDE.

What the thoughtful, conscientous programmer would be asking is “why does a ten (ish) character-period sticking-plaster delay make my sketch work, and what can I do to eliminate it, and improve my understanding and remove a waste of processor resource?”
Just my tuppence.

“It’s easy to sit there and say you’d like to have more money. And I guess that’s what I like about it. It’s easy. Just sitting there, rocking back and forth, wanting that money.” - Deep Thoughts by Jack Handy :slight_smile: