port_handler.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 ################################################################################
5 # Copyright 2017 ROBOTIS CO., LTD.
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 ################################################################################
19 
20 # Author: Ryu Woon Jung (Leon)
21 
22 import time
23 import serial
24 import sys
25 import platform
26 
27 LATENCY_TIMER = 16
28 DEFAULT_BAUDRATE = 1000000
29 
30 
31 class PortHandler(object):
32  def __init__(self, port_name):
33  self.is_open = False
34  self.baudrate = DEFAULT_BAUDRATE
35  self.packet_start_time = 0.0
36  self.packet_timeout = 0.0
37  self.tx_time_per_byte = 0.0
38 
39  self.is_using = False
40  self.port_name = port_name
41  self.ser = None
42 
43  def openPort(self):
44  return self.setBaudRate(self.baudrate)
45 
46  def closePort(self):
47  self.ser.close()
48  self.is_open = False
49 
50  def clearPort(self):
51  self.ser.flush()
52 
53  def setPortName(self, port_name):
54  self.port_name = port_name
55 
56  def getPortName(self):
57  return self.port_name
58 
59  def setBaudRate(self, baudrate):
60  baud = self.getCFlagBaud(baudrate)
61 
62  if baud <= 0:
63  # self.setupPort(38400)
64  # self.baudrate = baudrate
65  return False # TODO: setCustomBaudrate(baudrate)
66  else:
67  self.baudrate = baudrate
68  return self.setupPort(baud)
69 
70  def getBaudRate(self):
71  return self.baudrate
72 
73  def getBytesAvailable(self):
74  return self.ser.in_waiting
75 
76  def readPort(self, length):
77  if (sys.version_info > (3, 0)):
78  return self.ser.read(length)
79  else:
80  return [ord(ch) for ch in self.ser.read(length)]
81 
82  def writePort(self, packet):
83  return self.ser.write(packet)
84 
85  def setPacketTimeout(self, packet_length):
86  self.packet_start_time = self.getCurrentTime()
87  self.packet_timeout = (self.tx_time_per_byte * packet_length) + (LATENCY_TIMER * 2.0) + 2.0
88 
89  def setPacketTimeoutMillis(self, msec):
90  self.packet_start_time = self.getCurrentTime()
91  self.packet_timeout = msec
92 
93  def isPacketTimeout(self):
94  if self.getTimeSinceStart() > self.packet_timeout:
95  self.packet_timeout = 0
96  return True
97 
98  return False
99 
100  def getCurrentTime(self):
101  return round(time.time() * 1000000000) / 1000000.0
102 
103  def getTimeSinceStart(self):
104  time_since = self.getCurrentTime() - self.packet_start_time
105  if time_since < 0.0:
106  self.packet_start_time = self.getCurrentTime()
107 
108  return time_since
109 
110  def setupPort(self, cflag_baud):
111  if self.is_open:
112  self.closePort()
113 
114  self.ser = serial.Serial(
115  port=self.port_name,
116  baudrate=self.baudrate,
117  # parity = serial.PARITY_ODD,
118  # stopbits = serial.STOPBITS_TWO,
119  bytesize=serial.EIGHTBITS,
120  timeout=0
121  )
122 
123  self.is_open = True
124 
125  self.ser.reset_input_buffer()
126 
127  self.tx_time_per_byte = (1000.0 / self.baudrate) * 10.0
128 
129  return True
130 
131  def getCFlagBaud(self, baudrate):
132  if platform.system() == 'Darwin':
133  if baudrate in [9600, 19200, 38400, 57600, 115200]:
134  return baudrate
135  else:
136  return -1
137  else:
138  if baudrate in [9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000,
139  2000000, 2500000, 3000000, 3500000, 4000000]:
140  return baudrate
141  else:
142  return -1
def setPacketTimeout(self, packet_length)
Definition: port_handler.py:85


dynamixel_sdk
Author(s): Gilbert , Zerom , Darby Lim , Leon
autogenerated on Fri Apr 16 2021 02:25:55