I need to syntax check incomming queries/commands on the UART and i would like some idears on how to do it. The syntax of the commands are as follow:
Syntax required for a query is: @<device address><query>?;FF
Syntax required for a command is: @<device address><command>!<parameter>;FF
Examples: Query current baud rate: @253BR?;FF Change baud rate to 19200: @253BR!19200;FF where: @ <attention character> 253 <device address> BR? <query> (for query syntax) BR!19200 <command>!<parameter> (for command syntax) ;FF <terminator>
Please note the termination is ;FF and not CR or LF.
Is it possible to use the scanf() function for this task? Something like this perhaps:
scanf("@%3s%2s?;FF", adr, command);
Is ";FF" supposed to be a 3-character string (a semicolon and two 'F' characters), or just 2 bytes (a semicolon and a 0xFF byte) ?
Yes i need a ";FF" 3 char string as termination and not the usual CR.
The following code illustrates the scanf function will ignore the matching chars in the end of the format string:
printf("\nEnter a number: "); while (1) {
if (scanf("%d;FF",&n)) break;
scanf("%s", &tmp); //Clear input stream }
printf("You wrote %d", n);
Output: Enter a number: 3;FFYou wrote 3 Enter a number: 4 (CR) You wrote 4 Enter a number:
In the first line scanf returns correctly as ";FF" is entered. But in the second line i press CR after the number 4, and scanf ignores the ";FF" in the format string.