// Serial read and "parse"
#define BUF_SIZE 66 // max 64 byte, delší rozdělí po 64 bytech - opakuje se readSerial
char newMessage[BUF_SIZE] = { "" };
bool newMessageAvailable = false;
uint8_t m_length; // length of new message
void readSerial()
//void readSerial(void)
{
static char *cp = newMessage;
while (Serial.available())
{
*cp = (char)Serial.read();
if ((*cp == '\n') || (cp - newMessage >= BUF_SIZE - 2)) // end of message character or full buffer
{
*cp = '\0'; // end the string
// restart the index for next filling spree and flag we have a message waiting
cp = newMessage;
newMessageAvailable = true;
}
else // move char pointer to next position
{
cp++;
m_length++;
}
}
}
/* pokud pošlu jen prázdý (odřádkování) pak další čtení přeskočí 1.byte ???
chtělo by to nějak doladit nebo zabránit přečtení prázdného odřádkování */
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (newMessageAvailable && m_length > 0)
{
Serial.print("Message length : ");
Serial.println(m_length);
Serial.println(newMessage);
// parsing ind.char
// if(newMessage[.]== ... etc)
//pro testovací účel jen zobrazím jednotliv0 byte:
for(uint8_t index = 0; index < m_length+1; index++)
{
Serial.print(newMessage[index]); // mohu pracovat s jednotlivými byte pole
newMessage[index] = ' '; // nebere "..."
// newMessage[index] = 0; // vymaže jen m_length bytů z array, další zůstanou staré, jen je příště nezobrazím
}
Serial.println();
// newMessage = "empty"; // to nejde, nějak bych měl umět vymazat celé array char[..] ?, zatím to mažu při zpracování
newMessageAvailable = false;
m_length = 0;
}
readSerial();
}