port_handler_mac.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2017 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
17 /* Author: Ryu Woon Jung (Leon) */
18 
19 #if defined(__APPLE__)
20 
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <termios.h>
26 #include <time.h>
27 #include <sys/time.h>
28 #include <sys/ioctl.h>
29 
30 #ifdef __MACH__
31 #include <mach/clock.h>
32 #include <mach/mach.h>
33 #endif
34 
35 #include "port_handler_mac.h"
36 
37 #define LATENCY_TIMER 16 // msec (USB latency timer)
38  // You should adjust the latency timer value.
39  // When you are going to use sync / bulk read, the latency timer should be loosen.
40  // the lower latency timer value, the faster communication speed.
41 
42  // Note:
43  // You can either change its value by following:
44  // http://www.ftdichip.com/Support/Documents/TechnicalNotes/TN_105%20Adding%20Support%20for%20New%20FTDI%20Devices%20to%20Mac%20Driver.pdf
45 
46 using namespace dynamixel;
47 
48 PortHandlerMac::PortHandlerMac(const char *port_name)
49  : socket_fd_(-1),
50  baudrate_(DEFAULT_BAUDRATE_),
51  packet_start_time_(0.0),
52  packet_timeout_(0.0),
53  tx_time_per_byte(0.0)
54 {
55  is_using_ = false;
56  setPortName(port_name);
57 }
58 
60 {
61  return setBaudRate(baudrate_);
62 }
63 
65 {
66  if(socket_fd_ != -1)
67  close(socket_fd_);
68  socket_fd_ = -1;
69 }
70 
72 {
73  tcflush(socket_fd_, TCIFLUSH);
74 }
75 
76 void PortHandlerMac::setPortName(const char *port_name)
77 {
78  strcpy(port_name_, port_name);
79 }
80 
82 {
83  return port_name_;
84 }
85 
86 // TODO: baud number ??
87 bool PortHandlerMac::setBaudRate(const int baudrate)
88 {
89  int baud = getCFlagBaud(baudrate);
90 
91  closePort();
92 
93  if(baud <= 0) // custom baudrate
94  {
95  setupPort(B38400);
96  baudrate_ = baudrate;
97  return setCustomBaudrate(baudrate);
98  }
99  else
100  {
101  baudrate_ = baudrate;
102  return setupPort(baud);
103  }
104 }
105 
107 {
108  return baudrate_;
109 }
110 
112 {
113  int bytes_available;
114  ioctl(socket_fd_, FIONREAD, &bytes_available);
115  return bytes_available;
116 }
117 
118 int PortHandlerMac::readPort(uint8_t *packet, int length)
119 {
120  return read(socket_fd_, packet, length);
121 }
122 
123 int PortHandlerMac::writePort(uint8_t *packet, int length)
124 {
125  return write(socket_fd_, packet, length);
126 }
127 
128 void PortHandlerMac::setPacketTimeout(uint16_t packet_length)
129 {
131  packet_timeout_ = (tx_time_per_byte * (double)packet_length) + (LATENCY_TIMER * 2.0) + 2.0;
132 }
133 
134 void PortHandlerMac::setPacketTimeout(double msec)
135 {
137  packet_timeout_ = msec;
138 }
139 
141 {
143  {
144  packet_timeout_ = 0;
145  return true;
146  }
147  return false;
148 }
149 
151 {
152  struct timespec tv;
153 #ifdef __MACH__ // OS X does not have clock_gettime, so here uses clock_get_time
154  clock_serv_t cclock;
155  mach_timespec_t mts;
156  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
157  clock_get_time(cclock, &mts);
158  mach_port_deallocate(mach_task_self(), cclock);
159  tv.tv_sec = mts.tv_sec;
160  tv.tv_nsec = mts.tv_nsec;
161 #else
162  clock_gettime(CLOCK_REALTIME, &tv);
163 #endif
164  return ((double)tv.tv_sec * 1000.0 + (double)tv.tv_nsec * 0.001 * 0.001);
165 }
166 
168 {
169  double time;
170 
172  if(time < 0.0)
174 
175  return time;
176 }
177 
178 bool PortHandlerMac::setupPort(int cflag_baud)
179 {
180  struct termios newtio;
181 
182  socket_fd_ = open(port_name_, O_RDWR|O_NOCTTY|O_NONBLOCK);
183  if(socket_fd_ < 0)
184  {
185  printf("[PortHandlerMac::SetupPort] Error opening serial port!\n");
186  return false;
187  }
188 
189  bzero(&newtio, sizeof(newtio)); // clear struct for new port settings
190 
191  newtio.c_cflag = CS8 | CLOCAL | CREAD;
192  newtio.c_iflag = IGNPAR;
193  newtio.c_oflag = 0;
194  newtio.c_lflag = 0;
195  newtio.c_cc[VTIME] = 0;
196  newtio.c_cc[VMIN] = 0;
197  cfsetispeed(&newtio, cflag_baud);
198  cfsetospeed(&newtio, cflag_baud);
199 
200  // clean the buffer and activate the settings for the port
201  tcflush(socket_fd_, TCIFLUSH);
202  tcsetattr(socket_fd_, TCSANOW, &newtio);
203 
204  tx_time_per_byte = (1000.0 / (double)baudrate_) * 10.0;
205  return true;
206 }
207 
208 bool PortHandlerMac::setCustomBaudrate(int speed)
209 {
210  printf("[PortHandlerMac::SetCustomBaudrate] Not supported on Mac!\n");
211  return false;
212 }
213 
214 int PortHandlerMac::getCFlagBaud(int baudrate)
215 {
216  switch(baudrate)
217  {
218  case 9600:
219  return B9600;
220  case 19200:
221  return B19200;
222  case 38400:
223  return B38400;
224  case 57600:
225  return B57600;
226  case 115200:
227  return B115200;
228  case 230400:
229  return B230400;
230  // Mac OS doesn't support over B230400
231  // case 460800:
232  // return B460800;
233  // case 500000:
234  // return B500000;
235  // case 576000:
236  // return B576000;
237  // case 921600:
238  // return B921600;
239  // case 1000000:
240  // return B1000000;
241  // case 1152000:
242  // return B1152000;
243  // case 1500000:
244  // return B1500000;
245  // case 2000000:
246  // return B2000000;
247  // case 2500000:
248  // return B2500000;
249  // case 3000000:
250  // return B3000000;
251  // case 3500000:
252  // return B3500000;
253  // case 4000000:
254  // return B4000000;
255  default:
256  return -1;
257  }
258 }
259 
260 #endif
bool setCustomBaudrate(int speed)
bool setBaudRate(const int baudrate)
The function that sets baudrate into the port handler The function sets baudrate into the port handl...
void setPortName(const char *port_name)
The function that sets port name into the port handler The function sets port name into the port han...
bool setBaudRate(const int baudrate)
The function that sets baudrate into the port handler The function sets baudrate into the port handl...
bool openPort()
The function that opens the port The function calls PortHandlerMac::setBaudRate() to open the port...
char * getPortName()
The function that returns port name set into the port handler The function returns current port name...
PortHandlerMac(const char *port_name)
The function that initializes instance of PortHandler and gets port_name The function initializes in...
bool setupPort(const int cflag_baud)
void closePort()
The function that closes the port The function closes the port.
int readPort(uint8_t *packet, int length)
The function that reads bytes from the port buffer The function gets bytes from the port buffer...
bool isPacketTimeout()
The function that checks whether packet timeout is occurred The function checks whether current time...
void setPacketTimeout(uint16_t packet_length)
The function that sets and starts stopwatch for watching packet timeout The function sets the stopwa...
int getCFlagBaud(const int baudrate)
int writePort(uint8_t *packet, int length)
The function that writes bytes on the port buffer The function writes bytes on the port buffer...
void closePort()
The function that closes the port The function closes the port.
bool setCustomBaudrate(int speed)
int getBytesAvailable()
The function that checks how much bytes are able to be read from the port buffer The function checks...
void setPortName(const char *port_name)
The function that sets port name into the port handler The function sets port name into the port han...
void clearPort()
The function that clears the port The function clears the port.
int getCFlagBaud(const int baudrate)
int getBaudRate()
The function that returns current baudrate set into the port handler The function returns current ba...
bool is_using_
shows whether the port is in use
Definition: port_handler.h:67
bool setupPort(const int cflag_baud)


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