serial_mock API¶
MockSerial Class¶
-
class
serial_mock.
MockSerial
(stream, logfile=None, **kwargs)[source]¶ MockSerial(stream:string) instanciates a new MockStreamTunnel, stream should point to the comm port to listen on. in general this class should not be directly invoked but should be subclassed, you can find some examples in the examples folder, or in the cli.py file
Parameters: - stream – a path to a pipe (ie “/dev/ttyS99”,”COM11”), a stream like object, or “DEBUG”
- data_prefix – the separator between getters/setters and the data_attribute they reference
>>> from serial_mock.decorators import serial_query >>> from serial_mock.mock import MockSerial >>> class SimpleSerial(MockSerial): ... simple_queries = { ... "get -name":"hello my name is bob", ... "get -next":["123","456","789"], ... "get -id":12 ... } ... data={"x":6} ... @serial_query("trigger command") ... def do_something(self,requiredArg,optionalArg="0"): ... return "RESULT: %r %r"%(requiredArg,optionalArg) ... >>> mock = SimpleSerial("DEBUG") >>> mock.process_cmd("trigger command 1") "RESULT: '1' '0'" >>> mock.process_cmd("trigger command 1 2") "RESULT: '1' '2'" >>> mock.process_cmd("get -name") 'hello my name is bob' >>> mock.process_cmd("get -next") '123' >>> mock.process_cmd("get -next") '456' >>> mock.process_cmd("get -next") '789' >>> mock.process_cmd("get -next") '123' >>> mock.process_cmd("get -id") '12' >>> mock.process_cmd("get -x") '6' >>> mock.process_cmd('set -x 10') 'OK' >>> mock.process_cmd("get -x") '10'
-
data
= {}¶ any keys defined in data will automatically have getters or setters created for them
-
data_prefix
= '-'¶ the prefix to use with data auto generated routes
-
baudrate
= 9600¶ the baudrate we should operate at
-
prompt
= '>'¶ the prompt to display to the user
-
delimiter
= '\r'¶ user_terminal defines the character(or characters, or regexp, or list) of items that indicate our user has finished a command
-
endline
= '\r'¶ endline defines the character to output after our response but before our prompt
-
simple_queries
= {}¶ any key:value pair in simple queries is exposed as a simple query response … and the query string must be an exact match value can be a string/unicode/bytes value or it can be a list or array, if a list or array is passed in then the responses will be cycled any other value type will be coerced to str
-
logfile
= None¶
-
process_cmd
(cmd)[source]¶ looks up a command to see if its registered. and returns the result if it is otherwise returns an error string in general this command should not be invoked directly (but it can be…)
>>> from serial_mock.mock import MockSerial >>> inst = MockSerial("DEBUG") >>> inst.process_cmd("a") "ERROR 'a' Not Found"
Parameters: cmd – the command to process Returns: a string (the result of the command)
DummySerial Class¶
-
class
serial_mock.
DummySerial
(MockSerialClass)[source]¶ DummySerial provides a serial.Serial interface into a MockSerial instance. you can use this as a dropin replacement to serial.Serial, for anything that accepts serial.Serial as an argument
>>> from serial_mock.mock import DummySerial,MockSerial >>> from serial_mock.decorators import serial_query >>> class MyInterface(MockSerial): ... @serial_query("trigger command") ... def do_something(self,requiredArg,optionalArg="0"): ... return "RESULT: %r %r"%(requiredArg,optionalArg) ... >>> ser = DummySerial(MyInterface) >>> ser.write("trigger command 5\r") 18L >>> ser.read(ser.inWaiting()) "RESULT: '5' '0'\r>" >>> ser.write("trigger command 1 2\r") 20L >>> ser.read(ser.inWaiting()) "RESULT: '1' '2'\r>"
-
is_open
= True¶
-
open
()[source]¶ Open port with current settings. This may throw a SerialException if the port cannot be opened.
-
in_waiting
¶
-
EmittingSerial Class¶
-
class
serial_mock.
EmittingSerial
(stream, logfile=None, **kwargs)[source]¶ EmmitingSerial(stream:string) provides a reference class on an interface that periodically emits a “heartbeat” type message
Parameters: - stream – a path to a pipe (ie “/dev/ttyS99”,”COM11”), a stream like object, or “DEBUG”
- data_prefix – the separator between getters/setters and the data_attribute they reference
-
emit
= 'EMIT MSG'¶
-
delay
= (5, 35)¶
-
interval
= (15, 35)¶
debug output¶
these modules make use of pythons logging module, you can set the verbosity with logging.getLogger("serial_mock").setLevel(logging.DEBUG)
Indices and tables