Go Down

Topic: Struggling with manipulating data from serial buffer (Read 1 time) previous topic - next topic

GoForSmoke

Running an Arduino RS232 shield V2 which is required because talking to some old industrial temperature probes.

The code works with what I have , it's returning all the data i'm requesting but i'm limited to a 16x2 LCD and the probe serial number is returned with the model number and header information. I simply want to remove all but the last 6 characters.
If i send #01LB ( It's asking for the probe on address 1 for it's info . It returns "LB=DD99/99-123456" which fills the whole of second line on the LCD display .  I don't need any of the header "LB=" or the model number "DD99/99-" . All I want to display on the LCD is "123456".

Is there anything else you are saving that array for? Because if not then read all the chars but only print the ones you want **one at a time** and you don't need to save chars. It is also no big deal to read and evaluate numbers or match words with char by char stream input without text buffering or using the string.h library at all. I've done it since the 80's. You need less RAM and it's inherently quicker when dealing with serial data. My way you finish evaluation just after the final char arrives (if not sooner like a no-match non-numeric condition may evaluate on the 1st char) and the other way you start evaluation of the buffered text with numerous string.h commands only after the final char arrives.
Using a state machine makes it easier, the 2nd Nick Gammon tutorial addressed below covers an intro state machine that can read code+value combinations as his example code. It's a step to match chars in C strings instead of 1 code letter match but not a giant leap, just keep count of chars matched.

1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

zoomkat

Is there an end of data character at the end of the character string being sent? What you want to do is probably really simple using Strings.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

zoomkat

Quote
If i send #01LB ( It's asking for the probe on address 1 for it's info . It returns "LB=DD99/99-123456" which fills the whole of second line on the LCD display .  I don't need any of the header "LB=" or the model number "DD99/99-" . All I want to display on the LCD is "123456".
A simple example of capturing the characters after the - using some old code.

Code: [Select]

//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send
// LB=DD99/99-123456

String readString, newString;

void setup() {
  Serial.begin(9600);
  Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String
          int pos = readString.indexOf('-');
      newString = readString.substring(pos+1);
      Serial.print("newString is: ");
      Serial.println(newString);
      //int val = newString.toInt();
      //Serial.print("heart rate is: ");
      //Serial.println(val);
      readString=""; //clears variable for new input   
      newString=""; //clears variable for new input
  }
}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

-dev

Don't use String. In spite of numerous rational discussions, a few holdouts continue to think it's OK to recommend. 

Character arrays, aka C strings, are a good way to start, and will have long-term benefits. Even in the short term, it will save about 1600 bytes of program space.

Cheers,
/dev
Really, I used to be /dev.  :(

GoForSmoke

Don't use String. In spite of numerous rational discussions, a few holdouts continue to think it's OK to recommend. 

Character arrays, aka C strings, are a good way to start, and will have long-term benefits. Even in the short term, it will save about 1600 bytes of program space.

Cheers,
/dev
A bunch of us tell people that all time. They are wastey of RAM and cycles, they copy themselves just to add chars and shotgun the heap in the process. None of that is a real problem on a PC but using them on AVR's because you use them in PC code is like putting a bathtub on a bicycle because you have one in your house. All it shows is a deliberate disconnect of the hardware differences.

The same people are real big on transportable code. LOL, I want to transfer the code to a 168P (1K RAM) after being profligate with RAM due to Bad Habits Learned From Trivial Example Code, what do I do then besides either learn to code more tightly or just give up because of "hardware limits".

See what you can squeeze out of less RAM and your sketch may run on the smaller device instead of ratcheting up bigger all the time to run on wastey code.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

o0timbo0o

Thank you for all your assistance , the cd.write(&bufferData[11]) works  great after reading in the serial data into a char array

Go Up