sorry for the translation google
Good morning. sorry for the translation google
I just tested a tutorial to make a function "boolean getSerialString()"
the tutorial is here: JHaskell's Blog: Serial Comm Fundamentals on Arduino
I really struggled to understand.
here the code:
#define DATABUFFERSIZE 35
char dataBuffer[DATABUFFERSIZE+1]; //Add 1 for NULL terminator
byte dataBufferIndex = 0;
void setup(){
Serial.begin(57600);
Serial2.begin(57600);
Serial.print("wait data on com2");
}
//******************************************//
boolean getSerialString(){
char startChar = '
in first the "boolean getSerialString()" is well after void setup ()?
in second in the void loop() to use getSerialString the data are in dataBuffer[] ?
; // or '!', or whatever your start character is
char endChar = '\x1a';// or your end charracter
boolean storeString = false; //This will be our flag to put the data in our buffer
static byte dataBufferIndex = 0;
while(Serial2.available()>0)
{
char incomingbyte = Serial2.read();
Serial.print(incomingbyte);// tests to see if data is ok
if(incomingbyte==startChar)
{
dataBufferIndex = 0; //Initialize our dataBufferIndex variable
storeString = true;
}
if(storeString)
{
//Let's check our index here, and abort if we're outside our buffer size
//We use our define here so our buffer size can be easily modified
if(dataBufferIndex==DATABUFFERSIZE)
{
//Oops, our index is pointing to an array element outside our buffer.
dataBufferIndex = 0;
break;
}
if(incomingbyte==endChar)
{
dataBuffer[dataBufferIndex] = 0; //null terminate the C string
//Our data string is complete. return true
return true;
}
else{
dataBuffer[dataBufferIndex++] = incomingbyte;
dataBuffer[dataBufferIndex] = 0; //null terminate the C string
}}
else{
}}
//We've read in all the available Serial data, and don't have a valid string yet, so return false
return false;
}
//************************/////
void loop(){
if(getSerialString()){
//String available for parsing. Parse it here
for (int i = 0; i <= 35; i++) // select here index
{
Serial.print(dataBuffer[i]);}
in first the "boolean getSerialString()" is well after void setup ()?
in second in the void loop() to use getSerialString the data are in dataBuffer[] ?