ser.cc
Go to the documentation of this file.
1 /* Copyright 2017 UFACTORY Inc. All Rights Reserved.
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Author: Jimy Zhang <jimy92@163.com>
6  ============================================================================*/
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 
13 #ifdef _WIN32
14 #include <windows.h>
15 #include <sys/timeb.h>
16 #else
17 #include <sys/shm.h>
18 #include <unistd.h>
19 #include <termios.h>
20 #include <unistd.h>
21 #endif
22 
23 #include "xarm/core/port/ser.h"
24 #include "xarm/core/common/crc16.h"
25 
27  unsigned char ch;
28  int ret;
29  while (state_ == 0) {
30  ret = read_char(&ch);
31 
32  if (ret >= 0) {
33  parse_put(&ch, 1);
34  continue;
35  }
36  //usleep(1000);
37 #ifdef _WIN32
38  Sleep(1); // 1 ms
39 #else
40  usleep(1000); // 1000us
41 #endif
42  }
43  delete rx_que_;
44 }
45 
46 static void recv_proc_(void *arg) {
47  SerialPort *my_this = (SerialPort *)arg;
48 
49  my_this->recv_proc();
50 }
51 
52 SerialPort::SerialPort(const char *port, int baud, int que_num,
53  int que_maxlen) {
54  que_num_ = que_num;
55  que_maxlen_ = que_maxlen;
57 
58  int ret = init_serial(port, baud);
59  if (ret == -1)
60  {
61  state_ = -1;
62  }
63  else
64  {
65  state_ = 0;
66  }
67 
68  if (state_ == -1) {
69  delete rx_que_;
70  return;
71  }
72 
73  UXBUS_PROT_FROMID_ = 0x55;
74  UXBUS_PROT_TOID_ = 0xAA;
75  flush();
76  thread_id_ = std::thread(recv_proc_, this);
77  thread_id_.detach();
78 }
79 
81  state_ = -1;
82  close_port();
83 }
84 
85 int SerialPort::is_ok(void) { return state_; }
86 
87 void SerialPort::flush(void) {
88  rx_que_->flush();
89  rx_data_idx_ = 0;
91 }
92 
93 int SerialPort::read_char(unsigned char *ch) {
94  //return (read(fp_, ch, 1) == 1) ? 0 : -1;
95  try {
96  ser.read();
97  return 0;
98  }
99  catch (...) {
100  return -1;
101  }
102 
103 }
104 
105 int SerialPort::read_frame(unsigned char *data) {
106  if (state_ != 0) { return -1; }
107 
108  if (rx_que_->size() == 0) { return -1; }
109 
110  rx_que_->pop(data);
111  return 0;
112 }
113 
114 int SerialPort::write_char(unsigned char ch) {
115  //return ((write(fp_, &ch, 1) == 1) ? 0 : -1);
116  try {
117  ser.write(std::to_string(ch));
118  return 0;
119  }
120  catch (...) {
121  return -1;
122  }
123 }
124 
125 int SerialPort::write_frame(unsigned char *data, int len) {
126  //if (write(fp_, data, len) != len) { return -1; }
127  //return 0;
128  try {
129 
130  std::string str_data = (char *)data;
131  int size = ser.write(str_data);
132  if (size != len) { return -1; }
133  return 0;
134  }
135  catch (...) {
136  return -1;
137  }
138 
139 }
140 
142  state_ = -1;
143  //close(fp_);
144  try {
145  ser.close();
146  } catch(...) {}
147 }
148 
149 void SerialPort::parse_put(unsigned char *data, int len) {
150  unsigned char ch;
151 
152  for (int i = 0; i < len; i++) {
153  ch = data[i];
154  // printf("---state = %d, ch = %x\n", rx_state_, ch);
155  switch (rx_state_) {
156  case UXBUS_START_FROMID:
157  if (UXBUS_PROT_FROMID_ == ch) {
158  rx_buf_[0] = ch;
160  }
161  break;
162 
163  case UXBUS_START_TOOID:
164  if (UXBUS_PROT_TOID_ == ch) {
165  rx_buf_[1] = ch;
167  }
168  else {
170  }
171  break;
172 
173  case UXBUS_STATE_LENGTH:
174  if (0 < ch && ch < (que_maxlen_ - 5)) {
175  rx_buf_[2] = ch;
176  rx_length_ = ch;
177  rx_data_idx_ = 3;
179  }
180  else {
182  }
183  break;
184 
185  case UXBUS_STATE_DATA:
186  if (rx_data_idx_ < rx_length_ + 3) {
187  rx_buf_[rx_data_idx_++] = ch;
188  if (rx_data_idx_ == rx_length_ + 3) {
190  }
191  }
192  else {
194  }
195  break;
196 
197  case UXBUS_STATE_CRC1:
198  rx_buf_[rx_length_ + 3] = ch;
200  break;
201 
202  case UXBUS_STATE_CRC2:
203  int crc, crc_r;
204  rx_buf_[rx_length_ + 4] = ch;
205  crc = modbus_crc(rx_buf_, rx_length_ + 3);
206  crc_r = (rx_buf_[rx_length_ + 4] << 8) + rx_buf_[rx_length_ + 3];
207  if (crc == crc_r) {
208  rx_que_->push(rx_buf_);
209  }
211  break;
212 
213  default:
215  break;
216  }
217  }
218 }
219 
220 int SerialPort::init_serial(const char *port, int baud) {
221  /* speed_t speed;
222  struct termios options;
223 
224  fp_ = open((const char *)port, O_RDWR | O_NOCTTY);
225  if (-1 == fp_) { return -1; }
226 
227  fcntl(fp_, F_SETFL, FNDELAY);
228  tcgetattr(fp_, &options);
229  bzero(&options, sizeof(options));
230 
231  switch (baud) {
232  case 110:
233  speed = B110;
234  break;
235  case 300:
236  speed = B300;
237  break;
238  case 600:
239  speed = B600;
240  break;
241  case 1200:
242  speed = B1200;
243  break;
244  case 2400:
245  speed = B2400;
246  break;
247  case 4800:
248  speed = B4800;
249  break;
250  case 9600:
251  speed = B9600;
252  break;
253  case 19200:
254  speed = B19200;
255  break;
256  case 38400:
257  speed = B38400;
258  break;
259  case 57600:
260  speed = B57600;
261  break;
262  case 115200:
263  speed = B115200;
264  break;
265  case 921600:
266  speed = B921600;
267  break;
268  }
269 
270  cfsetispeed(&options, speed);
271  cfsetospeed(&options, speed);
272 
273  options.c_oflag &= ~OPOST;
274  options.c_cc[VTIME] = 200;
275  options.c_cc[VMIN] = 10;
276  tcsetattr(fp_, TCSANOW, &options);
277  return 0;
278  */
279  try {
280  ser.setPort(port);
281  ser.setBaudrate(baud);
283  ser.setTimeout(timeout);
284  ser.open();
285  return 0;
286  }
287  catch (...) {
288  return -1;
289 
290  }
291 }
void close_port(void)
Definition: ser.cc:141
static void recv_proc_(void *arg)
Definition: ser.cc:46
void close()
Definition: serial.cc:87
int read_frame(unsigned char *data)
Definition: ser.cc:105
unsigned char UXBUS_PROT_FROMID_
Definition: ser.h:51
int write_char(unsigned char ch)
Definition: ser.cc:114
int write_frame(unsigned char *data, int len)
Definition: ser.cc:125
void setTimeout(Timeout &timeout)
Definition: serial.cc:282
char pop(void *data)
Definition: queue_memcpy.cc:41
void parse_put(unsigned char *data, int len)
Definition: ser.cc:149
void setBaudrate(uint32_t baudrate)
Definition: serial.cc:293
void flush(void)
Definition: ser.cc:87
unsigned char UXBUS_PROT_TOID_
Definition: ser.h:52
unsigned char rx_buf_[128]
Definition: ser.h:56
int rx_state_
Definition: ser.h:55
int read_char(unsigned char *ch)
Definition: ser.cc:93
int state_
Definition: ser.h:33
int rx_length_
Definition: ser.h:57
int modbus_crc(unsigned char *data, int len)
Definition: crc16.cc:59
QueueMemcpy * rx_que_
Definition: ser.h:36
SerialPort(const char *port, int baud, int que_num, int que_maxlen)
Definition: ser.cc:52
int rx_data_idx_
Definition: ser.h:54
~SerialPort(void)
Definition: ser.cc:80
static Timeout simpleTimeout(uint32_t timeout)
Definition: serial.h:112
void open()
Definition: serial.cc:81
int init_serial(const char *port, int baud)
Definition: ser.cc:220
long size(void)
Definition: queue_memcpy.cc:30
void setPort(const std::string &port)
Definition: serial.cc:265
serial::Serial ser
Definition: ser.h:21
int que_maxlen_
Definition: ser.h:28
size_t read(uint8_t *buffer, size_t size)
Definition: serial.cc:124
std::thread thread_id_
Definition: ser.h:34
char push(void *data)
Definition: queue_memcpy.cc:66
int que_num_
Definition: ser.h:29
int is_ok(void)
Definition: ser.cc:85
char flush(void)
Definition: queue_memcpy.cc:21
Definition: ser.h:17
void recv_proc(void)
Definition: ser.cc:26
size_t write(const uint8_t *data, size_t size)
Definition: serial.cc:252


xarm_api
Author(s):
autogenerated on Sat May 8 2021 02:51:23