Archive for December 21st, 2015

Chiplotle: Better RTS-CTS Handshake Hackage

With hardware handshaking in full effect, the Chiplotle routine that sends data to the HP 7475A plotter doesn’t need to sleep, because the Linux serial handlers take care of that under the hood. Rather than simply comment that statement out, as I did before, it’s better to test the configuration and only sleep when needed:

The routine that extracts values from ~/.chiplotle/ is already included (well, imported) in the distribution’s file, so all we need is a test for (the lack of) hardware handshaking:

   def _write_string_to_port(self, data):
      ''' Write data to serial port. data is expected to be a string.'''
      #assert type(data) is str
      if not isinstance(data, basestring):
         raise TypeError('string expected.')
      data = self._filter_unrecognized_commands(data)
      data = self._slice_string_to_buffer_size(data)
      for chunk in data:
         if not get_config_value('rtscts'):
             self._sleep_while_buffer_full( )

The wisdom of reading a file inside the innermost loop of the serial data output routine may be debatable, but:

  • The output is 9600 b/s serial data
  • The expected result is that we’re about to wait
  • Plenty of smart folks have improved file I/O, so the read is probably a cache hit

For all I know, it doesn’t actually read a file, but consults an in-memory data structure. Works well enough for me, anyhow.

The configuration file I’ve been using all along looks like this (minus most of the comments):

# -*- coding: utf-8 -*-
serial_port_to_plotter_map = {'/dev/ttyUSB0' : 'HP7475A'}

## Serial connection parameters.
## Set your plotter to match these values, or vice versa..
baudrate = 9600
bytesize = 8
parity = 'N'
stopbits = 1
timeout = 1
xonxoff = 0
rtscts = 1

## Maximum wait time for response from plotter.
## Every time the plotter is queried, Chiplotle will wait for
## a maximum of `maximum_response_wait_time` seconds.
maximum_response_wait_time = 4

## Set to True if you want information (such as warnings)
## displayed on the console. Set to False if you don't.
verbose = True

That’s much prettier…