bce669a225f05779bb293db4727cea80812c37e7
pxsi: parameter update over serial interface
Markus Zolliker (markus.zolliker@psi.ch)
helper library for micropython on RP2040 (e.g. Arduino Nano Connect or Paspberry Pico)
Code example
from pxsi import Pxsi
# declare variables visibile over serial
with Pxsi('MyBox'): # idn will be: pxsi,MyBox
pollinterval = 5.0 # important: add decimal point in case you want this to be a float
anint = 2
afloat = 0.0
astring = 'text'
# the given variables are now available in the main namespace
# create action functions for some of the variables
def change_afloat(value):
value = float(value) # check value is compatible with float
# write here what to do when t changes
return value # return the validated value
# the validity of <value> has to be checked here, and if any conversion happens
# the converted value has to be returned
# if no change function is present, the value is converted to the type of
# the initial value (e.g. float, int or str)
# here follows your real code
while True:
# handle IO, wait at most 0.5 secs
Pxsi.handle(0.5)
# read out sensors, do control logic here
# important: do not sleep here!
Serial protocol
Commands:
name # get value of variable name
name=value # change value (value must be a valid JSON value)
["name1", ["name2", value]] # multiple commands: query name1, change name2. must be a valid JSON array
{"name1": null, "name2": value} # alternative form - be aware that order is not guaranteed
The values must match the type of initial values
Special command for identification
*IDN?
returns:
pxsi,<idn>
For all other commands the reply is always a JSON object:
{"name1": value1, "name2", value2}
In case of an error, the result contains an item with the key "error" and a JSON array as value, listing the errors appeared.
Hints for Arduino Nano Connect
For a soft reset send '\x03\x04' (Ctrl-C Ctrl-D). The reply will be quite some text, with the last line:
pxsi:<idn>
Description
Languages
Python
100%