uart_interface.py
Go to the documentation of this file.
1 #
2 # Copyright 2019-2020 TDK Corporation
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 #
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials provided
14 # with the distribution.
15 #
16 # 3. Neither the name of the copyright holder nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 import serial
34 from serial import SerialException
35 from serial.tools import list_ports
36 
37 
38 class UartInterface():
39  @staticmethod
40  def setup(vid_pid=None, com_port='', baudrate=921600):
41 
42  if com_port:
43  return UartInterface(port=com_port, baud=baudrate)
44  else:
45  devices = UartInterface.enum_device(vid_pid)
46 
47  if not devices:
48  raise RuntimeError("No UART device found with {}".format(vid_pid))
49  else:
50  for port in devices:
51  try:
52  return UartInterface(port=port[0], baud=baudrate)
53  except SerialException:
54  continue
55 
56  raise RuntimeError("Cound not connect to any device with {}".format(vid_pid))
57 
58  def __init__(self, port, baud=1000000, rtscts=True):
59  self.mode_blocking = True
60  self.blocking_timeout = 2.0
61  self.__ser = serial.Serial(port, baud, rtscts=rtscts)
62  self.__ser.reset_input_buffer()
63  self.__ser.reset_output_buffer()
64 
65  def __del__(self):
66  try:
67  self.close()
68  except:
69  pass
70 
71  def write(self, message):
72  try:
73  return self.__ser.write(message)
74  except Exception:
75  raise RuntimeError('Error writing to device')
76 
77  def read(self, number_of_bytes):
78  try:
79  return bytearray(self.__ser.read(number_of_bytes))
80  except Exception:
81  raise RuntimeError('Error while reading from device')
82 
83 
85  if self.mode_blocking:
86  # self.__ser.setTimeout(0)
87  self.__ser.timeout = 0
88  self.mode_blocking = False
89 
90  def set_mode_blocking(self):
91  if not self.mode_blocking:
92  # self.__ser.setTimeout(self.blocking_timeout)
93  self.__ser.timeout = self.blocking_timeout
94  self.mode_blocking = True
95 
96  def get_timeout(self):
97  return self.__ser.timeout
98 
99  def set_timeout(self, new_timeout):
100  prev_timeout = self.__ser.timeout
101  self.__ser.timeout = new_timeout
102  return prev_timeout
103 
104  def close(self):
105  self.clear_buffer()
106  self.__ser.close()
107 
108  def clear_buffer(self):
109  self.__ser.reset_input_buffer()
110  self.__ser.reset_output_buffer()
111 
112  @staticmethod
113  def enum_device(vid_pid):
114  return list_ports.grep(vid_pid)
def setup(vid_pid=None, com_port='', baudrate=921600)
def read(self, number_of_bytes)
def __init__(self, port, baud=1000000, rtscts=True)


tdk_robokit
Author(s): TDK-OpenSource
autogenerated on Sat Jan 11 2020 03:18:39