25 #if defined(__linux__)
34 #include <sys/ioctl.h>
35 #include <linux/serial.h>
39 #define LATENCY_TIMER 16
65 baudrate_(DEFAULT_BAUDRATE_),
66 packet_start_time_(0.0),
71 setPortName(port_name);
74 bool PortHandlerLinux::openPort()
88 tcflush(socket_fd_, TCIFLUSH);
93 strcpy(port_name_, port_name);
104 int baud = getCFlagBaud(baudrate);
111 baudrate_ = baudrate;
112 return setCustomBaudrate(baudrate);
116 baudrate_ = baudrate;
117 return setupPort(baud);
129 ioctl(socket_fd_, FIONREAD, &bytes_available);
130 return bytes_available;
135 return read(socket_fd_, packet, length);
140 return write(socket_fd_, packet, length);
145 packet_start_time_ = getCurrentTime();
146 packet_timeout_ = (tx_time_per_byte * (double)packet_length) + (LATENCY_TIMER * 2.0) + 2.0;
151 packet_start_time_ = getCurrentTime();
152 packet_timeout_ = msec;
157 if(getTimeSinceStart() > packet_timeout_)
165 double PortHandlerLinux::getCurrentTime()
168 clock_gettime(CLOCK_REALTIME, &tv);
169 return ((
double)tv.tv_sec * 1000.0 + (
double)tv.tv_nsec * 0.001 * 0.001);
172 double PortHandlerLinux::getTimeSinceStart()
176 time = getCurrentTime() - packet_start_time_;
178 packet_start_time_ = getCurrentTime();
183 bool PortHandlerLinux::setupPort(
int cflag_baud)
185 struct termios newtio;
187 socket_fd_ = open(port_name_, O_RDWR|O_NOCTTY|O_NONBLOCK);
190 printf(
"[PortHandlerLinux::SetupPort] Error opening serial port!\n");
194 bzero(&newtio,
sizeof(newtio));
196 newtio.c_cflag = cflag_baud | CS8 | CLOCAL | CREAD;
197 newtio.c_iflag = IGNPAR;
200 newtio.c_cc[VTIME] = 0;
201 newtio.c_cc[VMIN] = 0;
204 tcflush(socket_fd_, TCIFLUSH);
205 tcsetattr(socket_fd_, TCSANOW, &newtio);
207 tx_time_per_byte = (1000.0 / (double)baudrate_) * 10.0;
211 bool PortHandlerLinux::setCustomBaudrate(
int speed)
214 struct serial_struct ss;
215 if(ioctl(socket_fd_, TIOCGSERIAL, &ss) != 0)
217 printf(
"[PortHandlerLinux::SetCustomBaudrate] TIOCGSERIAL failed!\n");
221 ss.flags = (ss.flags & ~ASYNC_SPD_MASK) | ASYNC_SPD_CUST;
222 ss.custom_divisor = (ss.baud_base + (speed / 2)) / speed;
223 int closest_speed = ss.baud_base / ss.custom_divisor;
225 if(closest_speed < speed * 98 / 100 || closest_speed > speed * 102 / 100)
227 printf(
"[PortHandlerLinux::SetCustomBaudrate] Cannot set speed to %d, closest is %d \n", speed, closest_speed);
231 if(ioctl(socket_fd_, TIOCSSERIAL, &ss) < 0)
233 printf(
"[PortHandlerLinux::SetCustomBaudrate] TIOCSSERIAL failed!\n");
237 tx_time_per_byte = (1000.0 / (double)speed) * 10.0;
241 int PortHandlerLinux::getCFlagBaud(
int baudrate)
void clearPort()
The function that clears the port @description The function clears the port.
char * getPortName()
The function that returns port name set into the port handler @description The function returns curre...
bool setBaudRate(const int baudrate)
The function that sets baudrate into the port handler @description The function sets baudrate into th...
bool isPacketTimeout()
The function that checks whether packet timeout is occurred @description The function checks whether ...
void setPacketTimeout(uint16_t packet_length)
The function that sets and starts stopwatch for watching packet timeout @description The function set...
int getBytesAvailable()
The function that checks how much bytes are able to be read from the port buffer @description The fun...
void setPortName(const char *port_name)
The function that sets port name into the port handler @description The function sets port name into ...
int getBaudRate()
The function that returns current baudrate set into the port handler @description The function return...
void closePort()
The function that closes the port @description The function closes the port.
int readPort(uint8_t *packet, int length)
The function that reads bytes from the port buffer @description The function gets bytes from the port...
PortHandlerLinux(const char *port_name)
The function that initializes instance of PortHandler and gets port_name @description The function in...
int writePort(uint8_t *packet, int length)
The function that writes bytes on the port buffer @description The function writes bytes on the port ...