You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
voidparseData() { // split the data into its partschar * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the stringstrcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
floatFromPC = atof(strtokIndx); // convert this part to a float
}
runtime-generated string parsers
the commands are declared at runtime, in the arduino setup() function
any sufficiently complicated C or Fortran program
contains an ad hoc informally-specified bug-ridden slow implementation
of half of Common Lisp.
trees are encoded as lists of strings
http://www.ulisp.com/ - uLisp® is a version of the Lisp programming language specifically designed to run on microcontrollers with a limited amount of RAM
https://github.com/ffleurey/ThingMLArduinoDemo - ThingML is a high-level language based on components, asynchronous messaging and finate state machines. Despite a high level of abstraction, ThingML includes a code generator which is suitable for Arduino as well as other flavors of 8 bits microcontrollers.
Firmata [protocol] is based on the midi message format in that
commands bytes are 8 bits and data bytes are 7 bits.
For example the midi Channel Pressure (Command: 0xD0) message is 2 bytes long,
in Firmata the Command 0xD0 is used to enable reporting for a digital port (collection of 8 pins).
Both the midi and Firmata versions are 2 bytes long
ALL output methods, i.e. the Serial.print and Serial.write methods,
will stop the rest of you sketch from running once the Serial Tx buffer fills up.
The size of the Tx buffer varies between different Arduino boards.
Uno/Meg2560 has a 63 byte Tx buffer. The NanoBLE has none.
So if you want the rest of your sketch to keep running
while you output results or debugging messages,
you should not use any of the Serial print/write methods in your loop() code.
This tutorial will show you how to use the BufferedOuput class
to avoid blocking, add extra buffering.
automatic compiletime-generated string parsers
http://swig.org/ - SWIG is typically used to parse C/C++ interfaces and generate the 'glue code' required for the above target languages to call into the C/C++ code.
problem: swig can not generate purely text-based interfaces, aka "shell code"
related: string parsing on web servers
in the context of web servers, this problem is known as "routing"
the web client requests a "route" like /somedir/index.php?q=somequery
and the web server will lookup the request handler
The text was updated successfully, but these errors were encountered:
milahu
changed the title
string parsing on arduino (deserves an extra section)
string parsing on arduino: switch char, strcmp, compiletime parser generators, state machines
Oct 28, 2021
motivation: why string parsing?
in some cases, we want a custom text-based protocol,
which is easy for both humans and machines
as human, i do not want to type
instead, i want to say
challenges on arduino:
sample code can be run on a normal computer (no arduino)
single character parsers
as seen in many discussions on "arduino remote control via serial port"
as seen in
simple string parsers
use
strcmp
orstrncmp
to compare stringsas seen in
parsing variable strings
commands are constant strings, but we also want to parse variables
sample input:
https://forum.arduino.cc/t/how-to-parse-multiple-variables-in-one-string/582456 - strtok, atoi, atof
https://forum.arduino.cc/t/serial-input-basics-updated/382007/3
runtime-generated string parsers
the commands are declared at runtime, in the arduino
setup()
functionas seen in
concept:
the lookup from command name to handler function can be realized in different ways:
compiletime-generated string parsers
a "lexer" reads an input string and generates a list of tokens
a "parser" reads a list of tokens and generates a structure / syntax tree
with compiletime-generated string parsers,
commands and handlers are declared at compile time,
so the runtime can be optimized for lower memory usage
in most cases, this is a micro-optimization, aka "a waste of time" (lets do it anyway!)
as seen in
tree parsers
aka: lisp, scheme, ...
trees are encoded as lists of strings
note: in the context of lisp interpreters,
"embedded" usually means "a lisp interpreter, embedded into a non-lisp programming language"
sample input:
related
ThingML plugins
thingML allows us to write plugins to parse (and serialize) custom text-based protocols
https://heads-project.github.io/methodology/heads_methodology/thingml_plugins.html
document parser versus stream parser
finite state machines
FSM are usually
FSM can be used for string lexing and parsing,
but FSM can also be used to implement full arduino programs (manage state, handle events)
tag.1=value1 tag.2="value with spaces" a_different_tag=stuff tag
fish = "haddock"
crabs = 12
SET CHANNEL NAME='CHN 1'
GET CHANNEL NAME
G-code parser
tgolla/GCodeParser - a hand-written parser (not generated from grammar) → category: compiletime-generated string parsers
what is G-code?
full access to arduino via serial port
useful in development, not in production
in production, we want to give only limited access to the microcontroller
https://github.com/monsonite/SIMPL - category: single character parsers
serial-port-json-server
expose the arduino's serial port on a web server, to allow acces from a web browser
https://github.com/chilipeppr/serial-port-json-server
Locoduino/Commanders
string parser. obscure. what is the actual protocol?
https://github.com/Locoduino/Commanders/blob/master/examples/SerialCommander/SerialCommander.ino
binary protocols
firmata binary protocol
https://github.com/firmata/protocol
https://github.com/firmata/arduino
string libraries
non-blocking serial communication
https://arduino.stackexchange.com/a/22516/80923 Nick Gammon - reading serial without blocking
https://www.forward.com.au/pfod/ArduinoProgramming/Serial_IO/index.html
automatic compiletime-generated string parsers
http://swig.org/ - SWIG is typically used to parse C/C++ interfaces and generate the 'glue code' required for the above target languages to call into the C/C++ code.
problem: swig can not generate purely text-based interfaces, aka "shell code"
related: string parsing on web servers
in the context of web servers, this problem is known as "routing"
the web client requests a "route" like
/somedir/index.php?q=somequery
and the web server will lookup the request handler
https://github.com/julienschmidt/httprouter
The text was updated successfully, but these errors were encountered: