port_handler.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 
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
dynamixel_sdk.port_handler.PortHandler.getCurrentTime
def getCurrentTime(self)
Definition: port_handler.py:100
dynamixel_sdk.port_handler.PortHandler.clearPort
def clearPort(self)
Definition: port_handler.py:50
dynamixel_sdk.port_handler.PortHandler.isPacketTimeout
def isPacketTimeout(self)
Definition: port_handler.py:93
dynamixel_sdk.port_handler.PortHandler.ser
ser
Definition: port_handler.py:41
dynamixel_sdk.port_handler.PortHandler.setBaudRate
def setBaudRate(self, baudrate)
Definition: port_handler.py:59
dynamixel_sdk.port_handler.PortHandler.closePort
def closePort(self)
Definition: port_handler.py:46
dynamixel_sdk.port_handler.PortHandler.getCFlagBaud
def getCFlagBaud(self, baudrate)
Definition: port_handler.py:131
dynamixel_sdk.port_handler.PortHandler.baudrate
baudrate
Definition: port_handler.py:34
dynamixel_sdk.port_handler.PortHandler.is_using
is_using
Definition: port_handler.py:39
dynamixel_sdk.port_handler.PortHandler.getPortName
def getPortName(self)
Definition: port_handler.py:56
dynamixel_sdk.port_handler.PortHandler.readPort
def readPort(self, length)
Definition: port_handler.py:76
dynamixel_sdk.port_handler.PortHandler.getBytesAvailable
def getBytesAvailable(self)
Definition: port_handler.py:73
dynamixel_sdk.port_handler.PortHandler.setPacketTimeout
def setPacketTimeout(self, packet_length)
Definition: port_handler.py:85
dynamixel_sdk.port_handler.PortHandler.writePort
def writePort(self, packet)
Definition: port_handler.py:82
dynamixel_sdk.port_handler.PortHandler.tx_time_per_byte
tx_time_per_byte
Definition: port_handler.py:37
dynamixel_sdk.port_handler.PortHandler.__init__
def __init__(self, port_name)
Definition: port_handler.py:32
dynamixel_sdk.port_handler.PortHandler.getBaudRate
def getBaudRate(self)
Definition: port_handler.py:70
dynamixel_sdk.port_handler.PortHandler.is_open
is_open
Definition: port_handler.py:33
dynamixel_sdk.port_handler.PortHandler.setupPort
def setupPort(self, cflag_baud)
Definition: port_handler.py:110
dynamixel_sdk.port_handler.PortHandler.getTimeSinceStart
def getTimeSinceStart(self)
Definition: port_handler.py:103
dynamixel_sdk.port_handler.PortHandler.setPortName
def setPortName(self, port_name)
Definition: port_handler.py:53
dynamixel_sdk.port_handler.PortHandler.openPort
def openPort(self)
Definition: port_handler.py:43
dynamixel_sdk.port_handler.PortHandler
Definition: port_handler.py:31
dynamixel_sdk.port_handler.PortHandler.packet_timeout
packet_timeout
Definition: port_handler.py:36
dynamixel_sdk.port_handler.PortHandler.port_name
port_name
Definition: port_handler.py:40
dynamixel_sdk.port_handler.PortHandler.packet_start_time
packet_start_time
Definition: port_handler.py:35
dynamixel_sdk.port_handler.PortHandler.setPacketTimeoutMillis
def setPacketTimeoutMillis(self, msec)
Definition: port_handler.py:89


dynamixel_sdk
Author(s): Gilbert , Zerom , Darby Lim , Leon
autogenerated on Wed Mar 2 2022 00:13:50