granola.serial_sniffer module
SerialSniffer is a class that can be used in place of pyserial Serial to track every write and read for pyserial.
All it does is capture every incoming write and read, writes it out to a csv, and then passes it along to
pyserial.
You can easily run SerialSniffer in your code for a one off to just capture some commands for mocking later
by replacing your pyserial Serial import with:
from granola import SerialSniffer as Serial
- class granola.serial_sniffer.SerialSniffer(*args, **kwargs)
Bases:
SerialThis class is meant to be injected in place of Serial in some code that communicates with a device. It will then write out any serial commands sent, and their responses to a CSV file, to be used by GRANOLA. This code assumes that the next write after any read will be the “response” unpaired reads and writes will be ignored.
- Args: (passed by subclassing SerialSniffer with the appropriate class variable overwritten)
outfile (str): Path to the file you want to write to delimiter : passed to csv.writer() quotechar : passed to csv.writer() write_terminator (str) : The character sequence used to indicate that a serial command is complete read_terminator (str) : The character sequence used to indicate that a serial response is complete
Todo
Change how this is done to the same way the Breakfast Cereal is done (init then __call__)
- delimiter = ','
- outfile = ''
- quotechar = '"'
- read(size=1, *args, **kwargs)
A wrapper for Serial.read, that also stores read content, and when a terminator is reached, records that read to the given csv inputs and outputs are the same as Serial.read
- read_terminator = b'\r>'
- reset_input_buffer()
A wrapper for serial.reset_input_buffer that also clears the current read buffer. Should only be used with pyserial versions >= 3.0
- reset_output_buffer()
A wrapper for serial.reset_output_buffer that also clears the current write buffer. Should only be used with pyserial versions >= 3.0
- write(data, *args, **kwargs)
A wrapper for Serial.write, that also stores written content, and when a terminator is reached, records that read to the given csv inputs and outputs are the same as Serial.read Note: to mock the behavior of the serial instruments I have access to, any charaters after the initial terminator are ignored for our purposes. They are still written to the serial port, of course.
- write_terminator = b'\r'