Hi, thanks for the suggestions...
I came up with this ...
I'm sure i made some stupid mistakes with pointers, but it seems to work ( for the moment
)
#include <LiquidCrystal.h>
#include <stdio.h>
#include <string.h>
#define MAX_LINE 32
char line[MAX_LINE + 1];
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
struct mdata{
char rpm[8];
char gear[4];
} rfactor;
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop()
{
if (lineAvailable(MAX_LINE,line))
{
Analyze_String(line);
Serial.print(" Found RPM: ");
Serial.println(rfactor.rpm);
lcd.setCursor(0,1);
lcd.print(rfactor.rpm);
Serial.print(" Found GEAR: ");
Serial.println(rfactor.gear);
lcd.setCursor(0,0);
lcd.print(rfactor.gear);
}
//do something other than waiting for serial transfer
}
boolean lineAvailable(int max_line,char *line)
{
int c;
static int line_idx = 0;
static boolean eol = false;
static boolean start = false;
static boolean mstop = false;
if (max_line <= 0) // handle bad values for max_line
{
eol = true;
if (max_line == 0)
line[0] = '\0';
}
else // valid max_line
{
if (Serial.available() > 0)
{
c = Serial.read();
if (c != -1 ) // got a char -- should always be true
{
if ( c == '!' ) start = true;
if ( c == '#' ) mstop = true;
if (c == '\r' || c == '\n')
eol = true;
else
if ( start )
line[line_idx++] = c;
if ( mstop )
eol = true;
if (line_idx >= max_line)
eol = true;
line[line_idx] = '\0'; // always terminate line, even if unfinished
}
if (eol)
{
line_idx = 0; // reset for next line
eol = false; // get ready for another line
start = false;
mstop = false;
return true;
}
else
return false;
}
}
}
int Analyze_String(char line[]){
const char *ptr = line;
char field [ 32 ];
int n;
while ( sscanf(ptr, "%31[^;]%n", field, &n) == 1 )
{
switch(field[0]) {
case 'R':
strcpy(rfactor.rpm, field);
break;
case 'G':
strcpy(rfactor.gear, field);
break;
}
// delay(10);
ptr += n; /* advance the pointer by the number of characters read */
if ( ptr != ';' )
{
break; / didn't find an expected delimiter, done? /
}
++ptr; / skip the delimiter */
}
return 0;
}
String example:
!;R 17500;G 2;S 151;L 2;P 10;#