Commits

Ben Bass  committed 8d1497f

add serial_device module supporting serial line properties

  • Participants
  • Parent commits 072d325

Comments (0)

Files changed (4)

   python -m unittest discover
 * change - now resets the flow control and baudrate during device
   initialisation
+* new - SerialDevice class - supports properties for various serial lines
+  (CTS, DTS, RI as inputs; DTR, DSR as outputs)
 
 0.13
 ~~~~

File docs/pylibftdi.rst

     :undoc-members:
     :show-inheritance:
 
-:mod:`bitbang` Module
----------------------
-
-.. automodule:: pylibftdi.bitbang
-    :members:
-    :undoc-members:
-    :show-inheritance:
-
 :mod:`device` Module
 --------------------
 
     :undoc-members:
     :show-inheritance:
 
+
+:mod:`bitbang` Module
+---------------------
+
+.. automodule:: pylibftdi.bitbang
+    :members:
+    :undoc-members:
+    :show-inheritance:
+
+:mod:`serial_device` Module
+---------------------
+
+.. automodule:: pylibftdi.serial_device
+    :members:
+    :undoc-members:
+    :show-inheritance:
+
 :mod:`util` Module
 ------------------
 

File pylibftdi/__init__.py

            'ALL_OUTPUTS', 'ALL_INPUTS', 'BB_OUTPUT', 'BB_INPUT',
            'USB_VID_LIST', 'USB_PID_LIST']
 
-from pylibftdi import _base, driver, device, util, bitbang
+from pylibftdi import _base, driver, device, util, bitbang, serial_device
 
 # Bring them in to package scope so we can treat pylibftdi
 # as a module if we want.
 Bus = util.Bus
 Driver = driver.Driver
 Device = device.Device
+SerialDevice = serial_device.SerialDevice
 BitBangDevice = bitbang.BitBangDevice
 USB_VID_LIST = driver.USB_VID_LIST
 USB_PID_LIST = driver.USB_PID_LIST

File pylibftdi/serial_device.py

+"""
+pylibftdi - python wrapper for libftdi
+
+Copyright (c) 2010-2014 Ben Bass <benbass@codedstructure.net>
+See LICENSE file for details and (absence of) warranty
+
+pylibftdi: http://bitbucket.org/codedstructure/pylibftdi
+
+"""
+from pylibftdi.device import Device
+
+from ctypes import c_uint16, byref
+
+CTS_MASK = 1 << 4
+DSR_MASK = 1 << 5
+RI_MASK = 1 << 6
+
+
+class SerialDevice(Device):
+    """
+    simple subclass to support serial(rs232) lines
+
+    cts, dsr, ri - input
+    dtr, rts - output
+
+    Note: These lines are all active-low by default, though this can be
+    changed in the EEPROM settings. pylibftdi does not attempt to hide
+    these settings, and simply writes out the given values (i.e. '1'
+    will typically make an output line 'active' - and therefore low)
+    """
+
+    _dtr = None
+    _rts = None
+
+    @property
+    def dtr(self):
+        """
+        set (or get the previous set) state of the DTR line
+
+        :return: the state of the DTR line; None if not previously set
+        """
+        return self._dtr
+
+    @dtr.setter
+    def dtr(self, value):
+        value &= 1
+        if value != self._dtr:
+            self.ftdi_fn.ftdi_setdtr(value)
+
+        self._dtr = value
+
+    @property
+    def rts(self):
+        """
+        set (or get the previous set) state of the DTR line
+
+        :return: the state of the RTS line; None if not previously set
+        """
+        return self._rts
+
+    @rts.setter
+    def rts(self, value):
+        value &= 1
+        if value != self._rts:
+            self.ftdi_fn.ftdi_setrts(value)
+
+        self._rts = value
+
+    def _modem_status(self):
+        status = c_uint16()
+        self.ftdi_fn.ftdi_poll_modem_status(byref(status))
+        return status.value
+
+    @property
+    def cts(self):
+        """
+        get the state of CTS (1 = 'active')
+        """
+        return int(bool(self._modem_status() & CTS_MASK))
+
+    @property
+    def dsr(self):
+        """
+        get the state of DSR (1 = 'active')
+        """
+        return int(bool(self._modem_status() & DSR_MASK))
+
+    @property
+    def ri(self):
+        """
+        get the state of RI (1 = 'active')
+        """
+        return int(bool(self._modem_status() & RI_MASK))