Serial monitor validations
nixpulvis opened this issue · comments
Since we are transferring data over serial to test code that will eventually be all in C on the Arduino, being able to validate that the serial message is good before using it will make playing with the data more consistent.
Possible syntax
SerialMonitor.new(serialport, baud, :start_stop_byte => "\n", :validation => /^\d$/)
Understanding Serial
Keep in mind the messages coming over the wire are currently being read as UTF-8 symbols by default. If the message is malformed we will get errors (those pesky UTF-8 exceptions). So throwing away all messages with bad UTF-8 encoding should fix that.
Handling a Bad Message
We have two option in the case we get a bad message.
- Throw away the message, wait for another message and then return that.
- Set some state variable to
:bad
or something, and return the bad message. - Both.
Option 1
This approach will always yells good data, using it will be super simple. No extra conditionals. However it will block the CPU while attempting to read another message. In the case messages are coming very frequently this is less of an issue.
Option 2
This will require a conditional around all logic using data from serial.
msg = @sm.gets
if msg.valid?
#...
end
While this will be added work for users of serial monitor, it will allow inspection of bad data, and doesn't hold the CPU for more then 1 message read. This also begs for the return type of SerialMonitor#gets
to be a SerialMonitor::Message
class. Allowing us to save the state to it and define methods like SerialMonitor::Message#valid?
Option 3
We could implement both, but would need to decide on a sensible API.
This code has been moved into nixpulvis/serial_communicator.