Arduino Forum

Using Arduino => Programming Questions => Topic started by: o0timbo0o on Dec 15, 2015, 06:31 pm

Title: Struggling with manipulating data from serial buffer
Post by: o0timbo0o on Dec 15, 2015, 06:31 pm
I am reading data from the serial buffer into a variable char bufferData

I would like to manipulate some of this data by scrapping off the 1st 11 chars and print the remaining data to the lcd.write(bufferData)

I can write all  the data stored in the variable to the LCD with no issues. Lack of knowledge is preventing me going any further.


Title: Re: Struggling with manipulating data from serial buffer
Post by: Delta_G on Dec 15, 2015, 06:35 pm
Lack of knowledge is preventing me going any further.
And the lack of seeing your code is stopping us from helping you.
Title: Re: Struggling with manipulating data from serial buffer
Post by: PaulS on Dec 15, 2015, 06:57 pm
If the data is in an array called bufferData, Serial.print(bufferData) will print the data starting at position 0.

Serial.print(&bufferData[10]) will print the data starting in position 11. Perhaps a clue will present itself.
Title: Re: Struggling with manipulating data from serial buffer
Post by: o0timbo0o on Dec 15, 2015, 07:01 pm
Sorry .. it's not the whole program just the snippet regarding the data in the buffer basically it's the last line of code  lcd.write(bufferData); .. I know i need to have something above this line to manipulate the data in the variable ..


Code: [Select]


char bufferData ;
void setup() {
  lcd.begin(16, 2);  // set up the LCD's number of columns and rows:
  mySerial.begin(9600);  // initialize the serial communications:
 }

void probe1() {
   mySerial.print("#01VC\r") ; Request probe temperature
     delay(100);
    if (mySerial.available()) {// when characters arrive over the serial port...
      delay(100);// wait a bit for the entire message to arrive
       lcd.setCursor(0,0);//Select Top left line of LCD
   while (mySerial.available() ) {// read all the available characters     
        float x = mySerial.parseFloat();// Take the buffer , strip the header text and just display the value (Data returns VC=1234.546)

        lcd.print(x,3); //Display 3 decimal places
        lcd.print((char)223);//Display the Celsius symbol
        lcd.print("C");

        mySerial.print("#01LB\r") ; // Request probe label
        delay(100);
 
 if (mySerial.available()) {// when characters arrive over the serial port...
        delay(100);// wait a bit for the entire message to arrive
        lcd.setCursor(0,1); // Select bottom left line of LCD
   while (mySerial.available() ) {// read all the available characters
                       bufferData=mySerial.read();// display each character to the LCD (Data Returns "LB=DD99/99-123456"-- only require "123456"

***SOMEHOW MANIPULATE THE VARIABLE HERE TO DISCARD 1ST 11 CHARS***
             
             lcd.write(bufferData);
 
       }
     }
   }
 }
}
void loop() {

 probe1();

 delay(100);
}
Title: Re: Struggling with manipulating data from serial buffer
Post by: PaulS on Dec 15, 2015, 07:24 pm
Quote
***SOMEHOW MANIPULATE THE VARIABLE HERE TO DISCARD 1ST 11 CHARS***
             
             lcd.write(bufferData);
You don't need to "manipulate the variable". You just need to define where to start printing:
Code: [Select]
   lcd.write(&bufferData[10]);
Title: Re: Struggling with manipulating data from serial buffer
Post by: o0timbo0o on Dec 15, 2015, 07:38 pm
You don't need to "manipulate the variable". You just need to define where to start printing:
Code: [Select]
   lcd.write(&bufferData[10]);
doesn't like it


exit status 1
invalid types 'char[int]' for array subscript
Title: Re: Struggling with manipulating data from serial buffer
Post by: PaulS on Dec 15, 2015, 07:40 pm
All of your code and the EXACT error message, or stop wasting our time.
Title: Re: Struggling with manipulating data from serial buffer
Post by: o0timbo0o on Dec 15, 2015, 07:54 pm
RTD_DISPLAY_WORK_IN_PROGRESS:46: error: invalid types 'char[int]' for array subscript

          lcd.write(&bufferData[10]);

                                  ^

exit status 1
invalid types 'char[int]' for array subscript

 
Code: [Select]

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(2, 3);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

char bufferData;

void setup() {

  lcd.begin(16, 2);  // set up the LCD's number of columns and rows:
  mySerial.begin(9600);  // initialize the serial communications:

}
void probe1() {


  mySerial.print("#01VC\r") ;// Read probe temperature
  delay(100);

  if (mySerial.available()) {// when characters arrive over the serial port...
    delay(100);// wait a bit for the entire message to arrive
    //lcd.clear();// clear the screen
    lcd.setCursor(0, 0);

    while (mySerial.available() ) {// read all the available characters
      //  lcd.write(mySerial.read());
      //  bufferData=mySerial.read();// display each character to the LCD
      //    lcd.write(bufferData);

      float x = mySerial.parseFloat();// Take the buffer , strip the header text and just display the value (Data returns VC=1234.546)

      lcd.print(x, 3);
      lcd.print((char)223);
      lcd.print("C");

      mySerial.print("#01LB\r") ;//read Probe identifier
      delay(100);

      if (mySerial.available()) {// when characters arrive over the serial port...
        delay(100);// wait a bit for the entire message to arrive
        lcd.setCursor(0, 1);

        while (mySerial.available() ) {// read all the available characters
          lcd.write(mySerial.read());

          //  lcd.write(&bufferData[10]);
          bufferData = mySerial.read(); // display each character to the LCD
                                        // display each character to the LCD (Data Returns "LB=DD99/99-123456"-- only require "123456"
          lcd.write(bufferData);

        }
      }
    }
  }
}


void loop() {

  probe1();

  delay(100);




}


Title: Re: Struggling with manipulating data from serial buffer
Post by: GoForSmoke on Dec 15, 2015, 07:59 pm
OTOH you can count the number of chars you Serial.read(), do nothing with the first 11 and then print the rest. And then usually the code needs to do more.

But really, you don't need to buffer anything to do what you described or a whole lot more.
Title: Re: Struggling with manipulating data from serial buffer
Post by: PaulS on Dec 15, 2015, 08:00 pm
Code: [Select]
char bufferData;
A buffer is a place to hold more than one piece of data. You implied that you had a string with more than 11 characters in it. Since that was not even close to true, there is simply no way to do what you want with the codr that you have.
Title: Re: Struggling with manipulating data from serial buffer
Post by: Delta_G on Dec 15, 2015, 08:35 pm
Is it time to go here?

http://snippets-r-us.com/ (http://snippets-r-us.com/)
Title: Re: Struggling with manipulating data from serial buffer
Post by: PaulS on Dec 15, 2015, 08:55 pm
Is it time to go here?

http://snippets-r-us.com/ (http://snippets-r-us.com/)
No. Complete code is in reply #7. Code that doesn't do what OP says it does, but complete anyway.

Code: [Select]
SoftwareSerial mySerial = SoftwareSerial(2, 3);
OP: Do you really have a mySerial connected to the Arduino? If not, why are you calling it mySerial?

If you get a cat, do you call it myDog?
Title: Re: Struggling with manipulating data from serial buffer
Post by: o0timbo0o on Dec 15, 2015, 09:20 pm
No. Complete code is in reply #7. Code that doesn't do what OP says it does, but complete anyway.

Code: [Select]
SoftwareSerial mySerial = SoftwareSerial(2, 3);
OP: Do you really have a mySerial connected to the Arduino? If not, why are you calling it mySerial?

If you get a cat, do you call it myDog?
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".





Title: Re: Struggling with manipulating data from serial buffer
Post by: Robin2 on Dec 15, 2015, 10:05 pm
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".
Have a look at the parse example in Serial Input Basics (http://forum.arduino.cc/index.php?topic=288234.0). You could easily adapt it to split the data on the '-' char and copy the bit you are interested in to another char array.

...R
Title: Re: Struggling with manipulating data from serial buffer
Post by: Delta_G on Dec 15, 2015, 10:12 pm
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".






So it is not a matter of removing anything.  You just want to display the last 6.  If you put all of it in a char array then you can use the method @ PaulS posted earlier.
Title: Re: Struggling with manipulating data from serial buffer
Post by: GoForSmoke on Dec 15, 2015, 10:28 pm
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.

Title: Re: Struggling with manipulating data from serial buffer
Post by: zoomkat on Dec 16, 2015, 04:09 am
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.
Title: Re: Struggling with manipulating data from serial buffer
Post by: zoomkat on Dec 16, 2015, 04:26 am
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
  }
}

Title: Re: Struggling with manipulating data from serial buffer
Post by: -dev on Dec 16, 2015, 05:07 am
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
Title: Re: Struggling with manipulating data from serial buffer
Post by: GoForSmoke on Dec 16, 2015, 08:24 am
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.
Title: Re: Struggling with manipulating data from serial buffer
Post by: o0timbo0o on Dec 21, 2015, 04:04 pm
Thank you for all your assistance , the cd.write(&bufferData[11]) works  great after reading in the serial data into a char array