RE: scanf() and syntax checking
scanf() can match literals in the input as well as extract fields
The biggest drawback to scanf() is the lack of detail about what
went wrong. This may or may not be a problem for you; mainly it
limits your ability to provide useful feedback or debug
For example, executing
actual = scanf("@%uBR!%u;\xFF", &devAddr, &baudRate);
will match your baud rate command. If, however, even one character
is out of place, all you'll know is "actual", the number of bytes
that were actually matched from the input (not the format string).
will return "2" (the 'X' stops interpretation of the integer
beginning with the '2' and doesn't match a 'B'), but so will
You won't be able to generate error messages like "2X3 is not a
valid device address" or such without more control over the
Similarly, the format string you propose will work fine, in some
sense, but if the actual input isn't the exact right field width,
scanf() will happily produce strings anyway:
dev = "2X3" command = "BR"
dev = "2CR" command = "!1"
and you'll need to add another layer of scanning and checking.
If your input is mostly reliable, say from another program rather
than a human, then this might not be such an issue.
If you break up the parsing so that you match the input line
piece-by-piece, then you probably don't need the complexity of
scanf(), but instead just strcmp() or strtok().
You can get a surprising bit of mileage out of scanf(), but it
ultimately falls short for serious parsing tasks. For small jobs
where performance doesn't matter, you could manage.