I am trying another bit of OOP with a class for reading and parsing serial input strings, such as GPS output.
I have non-OOP code to capture serial strings, save them, and parse them. I am trying to move this to a Class so I can have multiple types of strings to capture, with different start characters or different end characters, or different delimiter characters, but one set of methods to handle them all. Someone may have already done this, but I am trying to use this as a learning by doing exercise.
I want to be able to have multiple instances of the class where the char array size is one of the variables. I have not found a way to set a char array while also setting the array size in a class. For example, I want one instance of Messages, say Messages GPS to have an dataBufferSize of 80 characters and another, say, Messages IMU to have a dataBufferSize of 18 characters, as shown in the code below. If I could do that, than the getString method could use the class member m_dataBuffer. Instead, I have a local dataBuffer and am considering a poor workaround of copying the resultant string back to the class member m_dataBuffer. I hate the idea of having multiple copies of the data using up RAM, even if temporarily. If I try to strcpy the local dataBuffer to the class member m_dataBuffer, I get mismatches of char and char* and complaints about non-static and const in all the variations I have tried. But that would be one workaround.
Really, what I want to do is is just have the getString function write directly to the respective m_databuffer, for example when I call GPS.getString, the captured string is written to GPS.m_dataBuffer and then I can do other things with that string (m_dataBuffer) with other Class methods.
Classes with char arrays continue to elude me... Help to get me off center is appreciated.
[/
/*
Demonstration of serial input string handler using Classes to
allow for capture and parsing of different formats or serial messages
*/
class Messages {
private:
int m_dataBufferSize;
char m_startChar;
char m_endChar;
char m_delimiters[];
public:
char m_dataBuffer;
private:
bool m_storeString;
public:
// single delimiter instance
Messages(int DBS, char SC, char EC, char DL1)
{
m_dataBufferSize = DBS;
m_startChar = SC;
m_endChar = EC;
m_delimiters[0] = DL1;
}
// two delimter instance
Messages(int DBS, char SC, char EC, char DL1, char DL2)
{
m_dataBufferSize = DBS;
m_startChar = SC;
m_endChar = EC;
m_delimiters[0] = DL1;
m_delimiters[1] = DL2;
}
bool getSerialString() {
static byte dataBufferIndex = 0;
char dataBuffer[m_dataBufferSize + 1]; //HOW CAN I USE THE CLASS MEMBER m_dataBuffer HERE INSTEAD OF A LOCAL dataBuffer?
while (Serial.available() > 0) {
char incomingbyte = Serial.read();
if (incomingbyte == m_startChar) {
dataBufferIndex = 0; //Initialize our dataBufferIndex variable
m_storeString = true;
}
if (m_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 == m_dataBufferSize) {
//Oops, our index is pointing to an array element outside our buffer.
dataBufferIndex = 0;
break;
}
if (incomingbyte == m_endChar) {
dataBuffer[dataBufferIndex] = 0; //null terminate the C string
//m_storeString = false; could be declared here but not really necessary since it is set false again next time the function is called
//Our data string is complete. return true
Serial.print("In getSerialString: ");
Serial.println(dataBuffer);
// ??? HOW TO COPY THE LOCAL dataBuffer to m_dataBuffer???
//strcpy(m_dataBuffer, dataBuffer); //ERROR invalid conversion from 'char' to 'char*' [-fpermissive]
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 outputString() {
Serial.print("In outputString: ");
Serial.println(m_dataBuffer);
}
};
//Invoke GPS and IMU instances
//Messages.InstanceName(int DBS, char SC, char EC, char DL1,(DL2) )
Messages GPS{80, '
, '\r', ','};
Messages IMU{18, '!', '\r', ':', ','};
void setup() {
Serial.begin(115200);
Serial.println("Serial port activated");
}
void loop() {
if (GPS.getSerialString()) {
Serial.println("got string");
GPS.outputString(); // DOES NOT HAVE THE CAPTURED STRING
while (true) {};
}
}
code]