local.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import serial
  2. import time
  3. from alphasign.interfaces import base
  4. class Serial(base.BaseInterface):
  5. def __init__(self, device="/dev/ttyS0"):
  6. self.device = device
  7. self.debug = True
  8. def connect(self):
  9. """Establish connection to the device.
  10. Args:
  11. device: character device (default: /dev/ttyS0)
  12. """
  13. # TODO(ms): these settings can probably be tweaked and still support most of
  14. # the devices.
  15. self._conn = serial.Serial(port=self.device,
  16. baudrate=4800,
  17. parity=serial.PARITY_EVEN,
  18. stopbits=serial.STOPBITS_TWO,
  19. timeout=1,
  20. xonxoff=0,
  21. rtscts=0)
  22. def disconnect(self):
  23. if self._conn:
  24. self._conn.close()
  25. def write(self, packet):
  26. if not self._conn:
  27. return
  28. if self.debug:
  29. print "Writing packet: %s" % repr(packet)
  30. self._conn.write(str(packet))
  31. class DebugInterface(base.BaseInterface):
  32. def __init__(self):
  33. self.debug = True
  34. def connect(self):
  35. pass
  36. def disconnect(self):
  37. pass
  38. def write(self, packet):
  39. if self.debug:
  40. print "Writing packet: %s" % repr(packet)