Python Library Serial Interface
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

__init__.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. serial_interface (Serial Interface Module)
  6. ==========================================
  7. **Author:**
  8. * Dirk Alders <sudo-dirk@mount-mockery.de>
  9. **Description:**
  10. This Module supports a common interface for a serial port
  11. **Submodules:**
  12. * :mod:`mmod.module.sub1`
  13. * :class:`mmod.module.sub2`
  14. * :func:`mmod.module.sub2`
  15. **Unittest:**
  16. See also the :download:`unittest <../../serial_interface/_testresults_/unittest.pdf>` documentation.
  17. """
  18. __DEPENDENCIES__ = ['stringtools']
  19. import stringtools
  20. import logging
  21. import serial
  22. import time
  23. logger_name = 'SERIAL_INTERFACE'
  24. logger = logging.getLogger(logger_name)
  25. class serial_interface(object):
  26. """
  27. Class to send and receive data via a serial port.
  28. .. note:: All parameters are given to the standard :py:class:`serial.Serial` library
  29. """
  30. LOG_PREFIX = 'SER:'
  31. def __init__(self, *args, **kwargs):
  32. self._args = args
  33. self._kwargs = kwargs
  34. self.com = None
  35. self.open()
  36. def open(self):
  37. """
  38. This opens the serial port.
  39. :returns: True if the port has been successfully opened
  40. :rtype: bool
  41. """
  42. if self.com is not None:
  43. self.close()
  44. try:
  45. self.com = serial.Serial(*self._args, **self._kwargs)
  46. except:
  47. self.com = None
  48. logger.warning('%s Unable to open comport (%s)', self.LOG_PREFIX, self._args[0])
  49. return False
  50. else:
  51. logger.debug('%s Opening comport (%s)', self.LOG_PREFIX, self._args[0])
  52. return True
  53. def close(self):
  54. """
  55. This closes the serial port.
  56. """
  57. if self.com is not None:
  58. logger.debug('%s Closing comport (%s)', self.LOG_PREFIX, self._args[0])
  59. self.com.close()
  60. self.com = None
  61. def bytes_in_queue(self):
  62. """
  63. This returns the number of available bytes for receiption.
  64. :returns: Number of bytes in queue
  65. :rtype: int
  66. """
  67. if self.com is not None:
  68. return self.com.inWaiting()
  69. else:
  70. return 0
  71. def receive(self, max_length=None):
  72. """
  73. This receives data from the serial interface.
  74. :param int length: An optinal number of bytes to be received. None will return all available bytes.
  75. :returns: The received data
  76. :rtype: str
  77. """
  78. length = self.bytes_in_queue()
  79. if max_length is not None:
  80. length = min(length, max_length)
  81. if self.com is not None and length > 0:
  82. data = self.com.read(length)
  83. self._handle_debug('RX <- ', data)
  84. return data
  85. def send(self, data):
  86. """
  87. This sends data via the serial interface.
  88. :param str data: The data to be send
  89. """
  90. if self.com is not None:
  91. self._handle_debug('TX -> ', data)
  92. self.com.write(data)
  93. def _handle_debug(self, msg, data):
  94. logger.debug('%s ' + msg + stringtools.hexlify(data), self.LOG_PREFIX)
  95. def __del__(self):
  96. self.close()