gpb01 / SerialCmd

A Wiring/Arduino library to tokenize and parse commands received over a phisical/software serial port or buffer.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Suggested enhancement

achrn opened this issue · comments

commented

Thanks for SerialCmd. This is not an 'issue', but a suggested enhancement. Itshould probably be a 'pull request', but I don't really do / understand GitHub, sorry.

I'm using SerialCmd, but have other traffic on the serial port that is not amendable to processing with SerialCmd. Accordingly, it's useful to make anything SerialCmd doesn't recognise available to the rest of my sketch. Therefore, I copy the received command into a second buffer immediately after it's recognised as an input line and just before trying to strtok_r it. It's an easy code change, but it does add more RAM consumption, so if you were to add it, it probably needs to be a conditional option. This can be done as follows:

In SerialCmd.h add to the 'configuration' block
#define SERIALCMD_PUBBUFFER 1 // If set to 1 double buffer read lines to a public

Also in SerialCmd.h add at the end of the public: section:
#if ( SERIALCMD_PUBBUFFER )
char lastLine[SERIALCMD_MAXBUFFER + 1];
#endif

In SerialCmd.cpp add into SerialCmd::ReadSer() immediately after the the 'if ( SerialCmd_InChar == SerialCmd_Term ) {'
if ( SERIALCMD_PUBBUFFER )
strcpy(lastLine,SerialCmd_Buffer); // copy to public buffer
#endif

This all results in creation of a new public char SerialCmd.lastLine[] which contains whatever the last received serial line was, whether SerialCmd recognised it as a line or not, so then (for example) Demo_SerialCmd.ino can do something like:
ret = mySerCmd.ReadSer();
if ( ret == 0 ) {
mySerCmd.Print ( ( char * ) "ERROR: Urecognized command: " );
mySerCmd.Print (mySerCmd.lastLine);
mySerCmd.Print ( ( char * ) "\r\n");
}

Hi achrn,
thank you very much for your suggestion ... it sounds like a good idea!

I will implement this in a future release.

Guglielmo