COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
Serial Port functions

Functions

 Cosmos::I2C::I2C (string bus, uint8_t address, double delay=2e-4)
 
 Cosmos::I2C::~I2C ()
 
int32_t Cosmos::I2C::send (string data)
 
int32_t Cosmos::I2C::send (uint8_t *data, size_t len)
 
int32_t Cosmos::I2C::send (vector< uint8_t > data)
 
int32_t Cosmos::I2C::receive (string &data)
 
int32_t Cosmos::I2C::receive (uint8_t *data, size_t len)
 
int32_t Cosmos::I2C::receive (vector< uint8_t > &data)
 
int32_t Cosmos::I2C::get_error ()
 
int32_t Cosmos::I2C::get_fh ()
 
 Cosmos::Serial::Serial (string dname, size_t dbaud=9600, size_t dbits=8, size_t dparity=0, size_t dstop=1)
 
int32_t Cosmos::Serial::open_device ()
 
int32_t Cosmos::Serial::close_device ()
 
 Cosmos::Serial::~Serial ()
 
bool Cosmos::Serial::get_open ()
 
int32_t Cosmos::Serial::get_error ()
 
int32_t Cosmos::Serial::set_restoreonclose (bool argument)
 
int32_t Cosmos::Serial::set_params (size_t dbaud, size_t dbits, size_t dparity, size_t dstop)
 
int32_t Cosmos::Serial::set_flowcontrol (bool rtscts, bool xonxoff)
 
int32_t Cosmos::Serial::set_dtr (bool state)
 
int32_t Cosmos::Serial::set_rts (bool state)
 
bool Cosmos::Serial::get_cts ()
 
int32_t Cosmos::Serial::set_timeout (int, double timeout)
 
int32_t Cosmos::Serial::set_timeout (double timeout)
 
int32_t Cosmos::Serial::SendByte (uint8_t byte)
 
int32_t Cosmos::Serial::SendBuffer (uint8_t *buffer, int size)
 
int32_t Cosmos::Serial::put_char (uint8_t c)
 
int32_t Cosmos::Serial::put_string (string data)
 
int32_t Cosmos::Serial::put_data (vector< uint8_t > data)
 
int32_t Cosmos::Serial::put_data (const uint8_t *data, size_t size)
 
int32_t Cosmos::Serial::put_slip (vector< uint8_t > data)
 
int32_t Cosmos::Serial::put_nmea (vector< uint8_t > data)
 
int32_t Cosmos::Serial::drain ()
 
int32_t Cosmos::Serial::get_char (uint8_t &buffer)
 
int32_t Cosmos::Serial::ReceiveByte (uint8_t &buf)
 
int32_t Cosmos::Serial::ReceiveBuffer (uint8_t *buf, int size)
 
int32_t Cosmos::Serial::get_char ()
 
int32_t Cosmos::Serial::poll_char ()
 
int32_t Cosmos::Serial::get_string (string &data, size_t size=SIZE_MAX)
 
int32_t Cosmos::Serial::get_string (string &data, char endc=0)
 
int32_t Cosmos::Serial::get_data (vector< uint8_t > &data, size_t size=SIZE_MAX)
 
int32_t Cosmos::Serial::get_data (uint8_t *data, size_t size)
 
int32_t Cosmos::Serial::get_xmodem (vector< uint8_t > &data, size_t size)
 Read Xmodem frame. More...
 
int32_t Cosmos::Serial::get_slip (vector< uint8_t > &data, size_t size=SIZE_MAX)
 Read SLIP frame. More...
 
int32_t Cosmos::Serial::get_nmea (vector< uint8_t > &data, size_t size)
 Read NMEA response. More...
 

Detailed Description

Function Documentation

Cosmos::I2C::I2C ( string  bus,
uint8_t  address,
double  delay = 2e-4 
)

Create i2c port instance. Create a i2c port object to be used for reading and writing to a physical port.

Parameters
busName of physical I2C device.
addressAddress of physical I2C port.
delayTime in seconds to wait for reading after writing.
52  {
53  handle.bus = bus;
54 #if !defined(COSMOS_WIN_OS)
55  handle.fh = open(handle.bus.c_str(), O_RDWR|O_NONBLOCK);
56 #endif
57 
58  if (handle.fh < 0)
59  {
60  error = - errno;
61  handle.fh = -1;
62  return;
63  }
64  // only works for linux for now
65  // TODO: expand to mac and windows
66 #if defined(COSMOS_LINUX_OS)
67  if (ioctl(handle.fh, I2C_FUNCS, &handle.funcs) < 0)
68  {
69  error = - errno;
70  close(handle.fh);
71  handle.fh = -1;
72  return;
73  }
74 
75  handle.address = address;
76  handle.delay = delay;
77 
78  if (ioctl(handle.fh, I2C_SLAVE, handle.address) < 0)
79  {
80  error = - errno;
81  handle.connected = false;
82  close(handle.fh);
83  handle.fh = -1;
84  return;
85  }
86 
87  if ((error = i2c_smbus_read_byte(handle.fh)) < 0)
88  {
89  error = - errno;
90  handle.connected = false;
91  close(handle.fh);
92  handle.fh = -1;
93  return;
94  }
95 #endif
96  handle.connected = true;
97  return;
98  }
uint8_t address
Definition: i2c.h:75
int32_t error
Definition: i2c.h:82
string bus
Definition: i2c.h:74
struct Cosmos::I2C::@29 handle
double delay
Definition: i2c.h:79
Cosmos::I2C::~I2C ( )
101  {
102  if (handle.fh >= 0)
103  {
104  close(handle.fh);
105  }
106  }
struct Cosmos::I2C::@29 handle
int32_t Cosmos::I2C::send ( string  data)
124  {
125  //uint8_t * c = data.c_str();
126  uint8_t * buff = new uint8_t[data.size() + 1];
127  memset(buff, 0, sizeof(data.size())); // reset buffer
128  std::copy(data.begin(), data.end(), buff);
129  error = this->send(buff, data.size());
130 
131  return error;
132  }
int32_t send(string data)
Definition: i2c.cpp:123
int32_t error
Definition: i2c.h:82
int32_t Cosmos::I2C::send ( uint8_t *  data,
size_t  len 
)
135  {
136 
137  error = ::write(handle.fh, data, len);
138 
139  if (error < 0)
140  {
141  error = -errno;
142  }
143 
144  return error;
145  }
int32_t error
Definition: i2c.h:82
struct Cosmos::I2C::@29 handle
int32_t Cosmos::I2C::send ( vector< uint8_t >  data)
148  {
149 
150  error = ::write(handle.fh, data.data(), data.size());
151 
152  if (error < 0)
153  {
154  error = -errno;
155  }
156 
157  return error;
158  }
int32_t error
Definition: i2c.h:82
struct Cosmos::I2C::@29 handle
int32_t Cosmos::I2C::receive ( string &  data)
162  {
163  // work in progress
164  // uint8_t buff[0];
165  // int32_t count = 0;
166  // int32_t rcvd = 0;
167 
168  // do {
169  // rcvd = this->receive(buff,1);
170  // printf("%02x - %c\n", buff[0], buff[0]);
171  // count ++;
172  // }while (buff[0] != 0x00); //end tranmission with null byte
173 
174  return 0;
175  }
int32_t Cosmos::I2C::receive ( uint8_t *  data,
size_t  len 
)
178  {
179  size_t count = 0;
180 
181  COSMOS_SLEEP(handle.delay);
182 
183  if (len)
184  {
185  ElapsedTime et;
186  do
187  {
188  int32_t rcvd = 0;
189  if (data == nullptr)
190  {
191  vector <uint8_t> tbuf;
192  tbuf.resize(len - count);
193  rcvd = ::read(handle.fh, tbuf.data(), len - count);
194  }
195  else
196  {
197  rcvd = ::read(handle.fh, data, len - count);
198  }
199 
200  if (rcvd < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
201  {
202  error = -errno;
203  return error;
204  }
205  else if (rcvd <= 0)
206  {
207  if (et.split() > len * .0001)
208  {
209  error = count;
210  return error;
211  }
212  }
213  else
214  {
215  // et.reset();
216  count += rcvd;
217  }
218  } while(count < len);
219  }
220  return count;
221  }
int count
Definition: rw_test.cpp:36
ElapsedTime et
Definition: agent_cpu_device_test.cpp:51
int32_t error
Definition: i2c.h:82
Definition: elapsedtime.h:62
struct Cosmos::I2C::@29 handle
double split()
ElapsedTime::split, gets the current elapsed time since the start()
Definition: elapsedtime.cpp:234
int32_t Cosmos::I2C::receive ( vector< uint8_t > &  data)
224  {
225  uint8_t tbuf[256];
226  data.clear();
227 
228  COSMOS_SLEEP(handle.delay);
229 
230  ElapsedTime et;
231  do
232  {
233  int32_t rcvd = 0;
234  rcvd = ::read(handle.fh, tbuf, 256);
235 
236  if (rcvd < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
237  {
238  error = -errno;
239  return error;
240  }
241  else if (rcvd > 0)
242  {
243  for (int32_t i=0; i<rcvd; ++i)
244  {
245  data.push_back(tbuf[i]);
246  }
247  et.reset();
248  }
249  } while(et.split() <= .0001 * (data.size()+1));
250 
251  return data.size();
252  }
int i
Definition: rw_test.cpp:37
ElapsedTime et
Definition: agent_cpu_device_test.cpp:51
int32_t error
Definition: i2c.h:82
Definition: elapsedtime.h:62
struct Cosmos::I2C::@29 handle
double split()
ElapsedTime::split, gets the current elapsed time since the start()
Definition: elapsedtime.cpp:234
void reset()
ElapsedTime::reset.
Definition: elapsedtime.cpp:278
int32_t Cosmos::I2C::get_error ( )
255  {
256  return error;
257  }
int32_t error
Definition: i2c.h:82
int32_t Cosmos::I2C::get_fh ( )
260  {
261  return handle.fh;
262  }
struct Cosmos::I2C::@29 handle
Cosmos::Serial::Serial ( string  dname,
size_t  dbaud = 9600,
size_t  dbits = 8,
size_t  dparity = 0,
size_t  dstop = 1 
)

Create serial port instance. Create a serial port object to be used for reading and writing to a physical serial port.

Parameters
dnameName of physical serial port.
dbaudBaud rate. Will be rounded to nearest of 75, 110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200.
dbitsNumber of data bits.
dparity0 = even, 1 = odd.
dstopNumber of stop bits.
50  {
51  name = dname;
52  baud = dbaud;
53  bits = dbits;
54  parity = dparity;
55  stop = dstop;
56 
57  error = open_device();
58  }
string name
Definition: serialclass.h:106
size_t parity
Definition: serialclass.h:109
size_t baud
Definition: serialclass.h:107
size_t bits
Definition: serialclass.h:108
int32_t open_device()
Definition: serialclass.cpp:60
int32_t error
Definition: serialclass.h:89
size_t stop
Definition: serialclass.h:110
int32_t Cosmos::Serial::open_device ( )
61  {
62  if (get_open())
63  {
65  }
66 
67 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS)
68  fd=open(name.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
69  if (fd == -1)
70  {
71  error = -errno;
72  return error;
73  }
74  fcntl(fd, F_SETFL, FNDELAY);
75 #endif
76 
77 #if defined(COSMOS_MAC_OS)
78  fd=open(name.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
79  if (fd == -1)
80  {
81  error = -errno;
82  return error;
83  }
84 #endif
85 
86 #if defined(COSMOS_WIN_OS)
87  // using TCHAR we can directly send COM1 to the string
88  // rather than \\\\.\\COM1
89  TCHAR *port=new TCHAR[name.size()+1];
90  port[name.size()]=0;
91  std::copy(name.begin(),name.end(),port);
92 
93  handle = CreateFileA(port,
94  GENERIC_READ|GENERIC_WRITE,
95  0, // must be opened with exclusive-access
96  NULL, // default security attributes
97  OPEN_EXISTING, // must use OPEN_EXISTING
98  0, // not overlapped I/O, no threads
99  NULL); // hTemplate must be NULL for comm devices
100 
101  if (handle == INVALID_HANDLE_VALUE) {
102  cout << "CreateFile failed with error " << GetLastError() << endl;
103  //cout << "error opening serial port" << endl;
104  } else {
105  // sucess opening serial port
106  fd = _open_osfhandle((intptr_t)handle, _O_RDONLY); // flag = 0
107  // fd=open(name.c_str(), O_RDWR | O_NOCTTY);
108  if (fd == -1)
109  {
110  error = -WSAGetLastError();
111  return error;
112  }
113  }
114 
115 #endif
116 
117 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
118 
119  tcgetattr(fd,&(oldtio));
120 #endif
121 
123  return error;
124  }
static string port
Definition: add_radio.cpp:16
string name
Definition: serialclass.h:106
int32_t set_params(size_t dbaud, size_t dbits, size_t dparity, size_t dstop)
Definition: serialclass.cpp:181
size_t parity
Definition: serialclass.h:109
size_t baud
Definition: serialclass.h:107
int fd
Definition: serialclass.h:88
size_t bits
Definition: serialclass.h:108
bool get_open()
Definition: serialclass.cpp:158
HANDLE handle
Definition: serialclass.h:103
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
size_t stop
Definition: serialclass.h:110
int32_t Cosmos::Serial::close_device ( )
127  {
128  if (fd >= 0)
129  {
130 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
131  tcflush(fd,TCOFLUSH);
132  tcflush(fd,TCIFLUSH);
133 #endif
134 
135  if (restoreonclose)
136  {
137  /* then we restore old settings */
138 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
139  tcsetattr(fd,TCSANOW,&(oldtio));
140 #else
141  SetCommState(handle, &(dcb));
142 #endif
143  }
144 
145  /* and close the file */
146  close(fd);
147  fd = -1;
148  }
149  error = 0;
150  return error;
151  }
bool restoreonclose
Definition: serialclass.h:96
int fd
Definition: serialclass.h:88
DCB dcb
Definition: serialclass.h:102
HANDLE handle
Definition: serialclass.h:103
int32_t error
Definition: serialclass.h:89
Cosmos::Serial::~Serial ( )
154  {
155  close_device();
156  }
int32_t close_device()
Definition: serialclass.cpp:126
bool Cosmos::Serial::get_open ( )
159  {
160  if (fd >= 0)
161  {
162  return true;
163  }
164  else
165  {
166  return false;
167  }
168  }
int fd
Definition: serialclass.h:88
int32_t Cosmos::Serial::get_error ( )
171  {
172  return error;
173  }
int32_t error
Definition: serialclass.h:89
int32_t Cosmos::Serial::set_restoreonclose ( bool  argument)
176  {
177  restoreonclose = argument;
178  return 0;
179  }
bool restoreonclose
Definition: serialclass.h:96
int32_t Cosmos::Serial::set_params ( size_t  dbaud,
size_t  dbits,
size_t  dparity,
size_t  dstop 
)
182  {
183  baud = dbaud;
184 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
185  tcflag_t trate;
186  tcflag_t tbits;
187  tcflag_t tstop;
188  tcflag_t tparity;
189 #endif
190 
191  if (fd < 0)
192  {
193  return SERIAL_ERROR_OPEN;
194  }
195 
196  // Set baud rate
197 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
198  uint32_t baud_index;
199  bool baud_speed_index = 0;
200  if (dbaud > (38400+57600)/2)
201  {
202  baud_speed_index = 1;
203  baud_index = static_cast<uint32_t>((log10f(dbaud) - 4.7604F) / .118F + .5F);
204  }
205  else
206  {
207  baud_index = static_cast<uint32_t>((log10f(dbaud) - 1.699F) / .2025F + .5F);
208  }
209 
210  bool baud_adjust = false;
211  do
212  {
213  baud_adjust = false;
214  int32_t baud_diff = std::abs(static_cast<int32_t>(dbaud) - static_cast<int32_t>(baud_speed[baud_speed_index][baud_index]));
215  if (baud_index > 0)
216  {
217  int32_t new_baud_diff = std::abs(static_cast<int32_t>(dbaud) - static_cast<int32_t>(baud_speed[baud_speed_index][baud_index-1]));
218  if (new_baud_diff < baud_diff)
219  {
220  --baud_index;
221  baud_diff = new_baud_diff;
222  baud_adjust = true;
223  }
224  }
225  if (baud_index < baud_speed[baud_speed_index].size()-1)
226  {
227  int32_t new_baud_diff = std::abs(static_cast<int32_t>(dbaud) - static_cast<int32_t>(baud_speed[baud_speed_index][baud_index+1]));
228  if (new_baud_diff < baud_diff)
229  {
230  ++baud_index;
231  baud_diff = new_baud_diff;
232  baud_adjust = true;
233  }
234  }
235  } while (baud_adjust);
236  baud = baud_speed[baud_speed_index][baud_index];
237  set_timeout(20. / baud);
238 
239  if (baud_speed_index)
240  {
241  trate = B57600 + baud_index;
242  }
243  else
244  {
245  trate = baud_index;
246  }
247 
248  /* databits */
249  switch (dbits) {
250  case 7:
251  tbits=CS7;
252  break;
253  case 8:
254  tbits=CS8;
255  break;
256  default:
257  tbits=CS8;
258  }
259 
260  /* parity, */
261  switch (dparity) {
262  case 0:
263  tparity=0;
264  break;
265  case 1: /* odd */
266  tparity=PARENB|PARODD;
267  break;
268  case 2:
269  tparity=PARENB;
270  break;
271  default:
272  tparity=0;
273  }
274 
275  /* and stop bits */
276  switch (dstop) {
277  case 1:
278  tstop=0;
279  break;
280  case 2:
281  tstop=CSTOPB;
282  break;
283  default:
284  tstop=0;
285  }
286 
287  /* now we setup the values in port's termios */
288  tio.c_cflag=trate|tbits|tparity|tstop|CLOCAL|CREAD;
289  tio.c_iflag=IGNPAR;
290  tio.c_oflag=0;
291  tio.c_lflag=0;
292  tio.c_cc[VMIN] = 1;
293  tio.c_cc[VTIME] = 0;
294 
295  /* we flush the port */
296  tcflush(fd,TCOFLUSH);
297  tcflush(fd,TCIFLUSH);
298 
299  /* we send new config to the port */
300  tcsetattr(fd,TCSANOW,&(tio));
301 #else
302  // windows
303 
304  // Initialize the DCB structure.
305  SecureZeroMemory(&dcb, sizeof(DCB));
306  dcb.DCBlength = sizeof(DCB);
307 
308  // Build on the current configuration by first retrieving all current
309  // settings.
310  BOOL fSuccess = GetCommState(handle, &dcb);
311  if (!fSuccess)
312  {
313  // Handle the error.
314  cout << "GetCommState failed with error " << GetLastError() << endl;
315  //return (2);
316  }
317 
318  //PrintCommState(dcb); // Output to console
319 
320  printf("\nBaudRate = %d, ByteSize = %d, Parity = %d, StopBits = %d\n",
321  dcb.BaudRate,
322  dcb.ByteSize,
323  dcb.Parity,
324  dcb.StopBits);
325 
326  // Fill in DCB values and set the com state:
327  // Example: 115,200 bps, 8 data bits, no parity, and 1 stop bit.
328 
329  dcb.fBinary = true; // Windows does not support nonbinary mode transfers, so this member must be TRUE.
330  dcb.BaudRate = dbaud; // baud rate
331  dcb.ByteSize = dbits; // number of data bits must be between 5 to 8
332  dcb.Parity = dparity; // If this member is TRUE, parity checking is performed and errors are reported.
333  if (dstop == 1) {
334  dcb.StopBits = ONESTOPBIT; // 0 = 1 stop bit, see https://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx
335  }
336  if (dstop == 1.5) {
337  dcb.StopBits = ONE5STOPBITS; // 1 = 1.5 stop bits, see https://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx
338  }
339  if (dstop == 2) {
340  dcb.StopBits = TWOSTOPBITS; // 2 = 2 stop bits, see https://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx
341  }
342 
343  fSuccess = SetCommState(handle, &dcb);
344 
345  if (!fSuccess)
346  {
347  // Handle the error.
348  cout << "SetCommState failed with error " << GetLastError();
349  return (3);
350  }
351 
352 
353  // COMMTIMEOUTS Cptimeouts;
354 
355  // Cptimeouts.ReadIntervalTimeout = MAXDWORD;
356  // Cptimeouts.ReadTotalTimeoutMultiplier = 0;
357  // Cptimeouts.ReadTotalTimeoutConstant = 0;
358  // Cptimeouts.WriteTotalTimeoutMultiplier = 0;
359  // Cptimeouts.WriteTotalTimeoutConstant = 0;
360 
361  // if(!SetCommTimeouts(handle, &Cptimeouts))
362  // {
363  // //printf("unable to set comport time-out settings\n");
364  // //CloseHandle(Cport[comport_number]);
365  // //return(1);
366  // }
367 
368 
369  // Get the comm config again.
370  fSuccess = GetCommState(handle, &dcb);
371 
372  if (!fSuccess)
373  {
374  // Handle the error.
375  cout << "GetCommState failed with error " << GetLastError();
376  // return (2);
377  }
378 
379  cout << "Serial port sucessfully configured" << endl;
380 
381 #endif // windows
382 
383  return 0;
384  }
vector< uint32_t > baud_speed[2]
Definition: serialclass.h:90
size_t baud
Definition: serialclass.h:107
int fd
Definition: serialclass.h:88
int32_t set_timeout(int, double timeout)
Definition: serialclass.cpp:515
DCB dcb
Definition: serialclass.h:102
HANDLE handle
Definition: serialclass.h:103
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t Cosmos::Serial::set_flowcontrol ( bool  rtscts,
bool  xonxoff 
)
387  {
388  if (fd < 0)
389  {
391  return (error);
392  }
393 
394 #if defined(COSMOS_WIN_OS)
395  GetCommState(handle, &(dcb));
396  if (rtscts)
397  {
398  dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
399  dcb.fOutxCtsFlow = TRUE;
400 
401  }
402  else
403  {
404  dcb.fRtsControl = RTS_CONTROL_DISABLE;
405  dcb.fOutxCtsFlow = FALSE;
406  }
407 #else
408  /* We setup rts/cts (hardware) flow control */
409  if (rtscts)
410  {
411  tio.c_cflag |= CRTSCTS;
412  } else
413  {
414  tio.c_cflag &= ~CRTSCTS;
415  }
416 #endif
417 
418 #if defined(COSMOS_WIN_OS)
419  if (xonxoff)
420  {
421  dcb.fInX = dcb.fOutX = TRUE;
422  }
423  else
424  {
425  dcb.fInX = dcb.fOutX = FALSE;
426 
427  }
428  SetCommState(handle, &(dcb));
429 #else
430  /* We setup xon/xoff (soft) flow control */
431  if (xonxoff)
432  {
433  tio.c_iflag |= (IXON|IXOFF);
434  }
435  else
436  {
437  tio.c_iflag &= ~(IXON|IXOFF);
438  }
439 
440  tcsetattr(fd,TCSANOW,&(tio));
441 #endif
442 
443  error = 0;
444  return error;
445  }
#define FALSE
Definition: jpleph.cpp:69
int fd
Definition: serialclass.h:88
#define TRUE
Definition: jpleph.cpp:68
DCB dcb
Definition: serialclass.h:102
HANDLE handle
Definition: serialclass.h:103
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
int32_t Cosmos::Serial::set_dtr ( bool  state)
448  {
449 #if defined(COSMOS_WIN_OS)
450  GetCommState(handle, &(dcb));
451  if (state)
452  {
453  dcb.fDtrControl = DTR_CONTROL_ENABLE;
454  }
455  else
456  {
457  dcb.fDtrControl = DTR_CONTROL_DISABLE;
458  }
459 #elif defined(COSMOS_LINUX_OS)
460  int flag = TIOCM_DTR;
461 
462  if (state)
463  {
464  ioctl(fd, TIOCMBIS, &flag);
465  }
466  else
467  {
468  ioctl(fd, TIOCMBIC, &flag);
469  }
470 #endif
471  return 0;
472  }
int fd
Definition: serialclass.h:88
DCB dcb
Definition: serialclass.h:102
HANDLE handle
Definition: serialclass.h:103
int32_t Cosmos::Serial::set_rts ( bool  state)
475  {
476 #if defined(COSMOS_WIN_OS)
477  GetCommState(handle, &(dcb));
478  if (state)
479  {
480  dcb.fDtrControl = RTS_CONTROL_ENABLE;
481  }
482  else
483  {
484  dcb.fDtrControl = RTS_CONTROL_DISABLE;
485  }
486 #elif defined(COSMOS_LINUX_OS)
487  int flag = TIOCM_RTS;
488 
489  if (state)
490  {
491  ioctl(fd, TIOCMBIS, &flag);
492  }
493  else
494  {
495  ioctl(fd, TIOCMBIC, &flag);
496  }
497 #endif
498  return 0;
499  }
int fd
Definition: serialclass.h:88
DCB dcb
Definition: serialclass.h:102
HANDLE handle
Definition: serialclass.h:103
bool Cosmos::Serial::get_cts ( )
502  {
503 #if defined(COSMOS_WIN_OS)
504 
505 #elif defined(COSMOS_LINUX_OS)
506  int s;
507  ioctl(fd, TIOCMGET, &s);
508  return (s & TIOCM_DSR) != 0;
509 #endif
510  }
int fd
Definition: serialclass.h:88
int32_t Cosmos::Serial::set_timeout ( int  ,
double  timeout 
)
517  {
518  return set_timeout(timeout);
519  }
int32_t set_timeout(int, double timeout)
Definition: serialclass.cpp:515
int32_t Cosmos::Serial::set_timeout ( double  timeout)
522  {
523  if (fd < 0)
524  {
525  return SERIAL_ERROR_OPEN;
526  }
527 
528 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
529  // tio.c_cc[VMIN]=minchar;
530  // tio.c_cc[VTIME]=(int)(timeout*10.+.4);
531 
532  // tcsetattr(fd,TCSANOW,&(tio));
533  ictimeout = timeout;
534 #else // windows
535  _COMMTIMEOUTS timeouts;
536  timeouts.ReadIntervalTimeout = timeout * 1000.;
537  SetCommTimeouts(handle, &timeouts);
538 #endif
539 
540  error=0;
541  return 0;
542  }
int fd
Definition: serialclass.h:88
HANDLE handle
Definition: serialclass.h:103
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
double ictimeout
Definition: serialclass.h:95
int32_t Cosmos::Serial::SendByte ( uint8_t  byte)
548  {
549  if (fd < 0)
550  {
552  return (error);
553  }
554 
555 #ifdef COSMOS_WIN_OS
556  int n=0;
557  // write to port
558  WriteFile(handle, &byte, 1, (LPDWORD)((void *)&n), NULL);
559 
560  if(n<0)
561  {
562  return(-errno);
563  }
564 
565 #else
566  if (write(fd, &byte, 1) < 0)
567  {
568  return (-errno);
569  }
570 #endif
571 
572  // if all goes well, 0 = sucess
573  return 0;
574  }
int fd
Definition: serialclass.h:88
HANDLE handle
Definition: serialclass.h:103
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
int32_t Cosmos::Serial::SendBuffer ( uint8_t *  buffer,
int  size 
)
579  {
580  int32_t n=0;
581 
582  if (fd < 0)
583  {
585  return (error);
586  }
587 
588 #ifdef COSMOS_WIN_OS
589  // write to port
590  WriteFile(handle, buffer, size, (LPDWORD)((void *)&n), NULL);
591 
592  if(n<0)
593  {
594  return(-errno);
595  }
596 
597 #else
598  if ((n = write(fd, &buffer, size)) < 0)
599  {
600  return (-errno);
601  }
602 #endif
603 
604  // if all goes well, return the number of bytes sent
605  return n;
606  }
static char buffer[255]
Definition: propagator_simple.cpp:60
int fd
Definition: serialclass.h:88
HANDLE handle
Definition: serialclass.h:103
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
int32_t Cosmos::Serial::put_char ( uint8_t  c)
610  {
611  if (fd < 0)
612  {
614  return (error);
615  }
616 
617  int result;
618 
619 #ifdef COSMOS_WIN_OS
620  int n=0;
621  WriteFile(handle, &c, 1, (LPDWORD)((void *)&n), NULL);
622  if(n<0)
623  {
624  error = -errno;
625  }
626 
627 #else
628  ElapsedTime et;
629  do
630  {
631  fd_set set;
632  FD_ZERO(&set);
633  FD_SET(fd, &set);
634  timeval timeout;
635  double rtimeout = ictimeout - et.split();
636  if (rtimeout >= 0.)
637  {
638  timeout.tv_sec = static_cast<int32_t>(rtimeout);
639  timeout.tv_usec = static_cast<int32_t>(1000000. * (rtimeout - timeout.tv_sec));
640  int rv = select(fd+1, nullptr, &set, nullptr, &timeout);
641  if (rv == -1)
642  {
643  error = -errno;
644  }
645  else if (rv == 0)
646  {
648  }
649  else
650  {
651  result = write(fd, &c, 1);
652  if (result > 0)
653  {
654  error = result;
655  break;
656  }
657  else
658  {
659  if (result < 0)
660  {
661  error = -errno;
662  }
663  else
664  {
666  }
667  }
668  }
669  }
670  } while (et.split() < ictimeout);
671 #endif
672 
673  // These sleeps are necessary to keep from overrunning the serial output buffer
674  // Windows seem to have trouble with anything shorter than 1/100 second (we should explore this)
675  // Unix is set to sleep just a little longer than it would take to send a character (could be shorter?)
676 #ifdef COSMOS_WIN_OS
677  COSMOS_SLEEP(0.010);
678 #else
679 // COSMOS_SLEEP(10. / baud - et.split());
680 #endif
681  //
682 
683  return error;
684  }
#define SERIAL_ERROR_TIMEOUT
Definition: cosmos-errno.h:171
ElapsedTime et
Definition: agent_cpu_device_test.cpp:51
int fd
Definition: serialclass.h:88
#define SERIAL_ERROR_BUFFER_SIZE_EXCEEDED
Definition: cosmos-errno.h:175
Definition: elapsedtime.h:62
HANDLE handle
Definition: serialclass.h:103
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
double split()
ElapsedTime::split, gets the current elapsed time since the start()
Definition: elapsedtime.cpp:234
int32_t error
Definition: serialclass.h:89
double ictimeout
Definition: serialclass.h:95
int32_t Cosmos::Serial::put_string ( string  data)
687  {
688  if (fd < 0)
689  {
691  return (error);
692  }
693 
694  int nbytes = write(fd, data.data(), data.size());
695  if (nbytes < 0)
696  {
697  error = -errno;
698  return error;
699  }
700  else
701  {
702  error = 0;
703  return nbytes;
704  }
705  }
int fd
Definition: serialclass.h:88
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
int32_t Cosmos::Serial::put_data ( vector< uint8_t >  data)
708  {
709  return put_data(data.data(), data.size());
710 // if (fd < 0)
711 // {
712 // error = SERIAL_ERROR_OPEN;
713 // return (error);
714 // }
715 
716 // int nbytes = write(fd, data.data(), data.size());
717 // if (nbytes < 0)
718 // {
719 // error = -errno;
720 // return error;
721 // }
722 // else
723 // {
724 // error = 0;
725 // return nbytes;
726 // }
727  }
int32_t put_data(vector< uint8_t > data)
Definition: serialclass.cpp:707
int32_t Cosmos::Serial::put_data ( const uint8_t *  data,
size_t  size 
)
730  {
731  if (fd < 0)
732  {
734  return (error);
735  }
736 
737 // ElapsedTime et;
738 // int32_t tbytes = 0;
739 // error = 0;
740 // double timeout = ictimeout * size;
741 // if (timeout > 5.)
742 // {
743 // timeout = 5.;
744 // }
745 // do
746 // {
747 // int nbytes = write(fd, &data[tbytes], size);
748  int nbytes = write(fd, data, size);
749  if (nbytes < 0)
750  {
751  error = -errno;
752  return error;
753  }
754  else {
755  error = 0;
756  return nbytes;
757  }
758 // tbytes += nbytes;
759 // } while (tbytes < size && et.split() < timeout);
760 // return tbytes;
761  }
int fd
Definition: serialclass.h:88
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
int32_t Cosmos::Serial::put_slip ( vector< uint8_t >  data)
764  {
765  if (fd < 0)
766  {
768  return (error);
769  }
770 
771  size_t i = 0;
773  if (error < 0)
774  {
775  return error;
776  }
777  for (size_t j=0; j<data.size(); j++)
778  {
779  switch (data[j])
780  {
781  case SLIP_FEND:
783  if (error < 0)
784  {
785  return error;
786  }
788  if (error < 0)
789  {
790  return error;
791  }
792  i+=2;
793  break;
794  case SLIP_FESC:
796  if (error < 0)
797  {
798  return error;
799  }
801  if (error < 0)
802  {
803  return error;
804  }
805  i+=2;
806  break;
807  default:
808  error = put_char(data[j]);
809  if (error < 0)
810  {
811  return error;
812  }
813  i++;
814  break;
815  }
816  }
818  if (error < 0)
819  {
820  return error;
821  }
822  return (i);
823  }
int i
Definition: rw_test.cpp:37
#define SLIP_FESC
SLIP Buffer Escape character.
Definition: sliplib.h:60
int32_t put_char(uint8_t c)
Definition: serialclass.cpp:609
int fd
Definition: serialclass.h:88
#define SLIP_TFEND
SLIP Buffer Escaped End character.
Definition: sliplib.h:64
#define SLIP_FEND
SLIP Buffer End character.
Definition: sliplib.h:56
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
#define SLIP_TFESC
SLIP Buffer Escaped Escape character.
Definition: sliplib.h:68
int32_t Cosmos::Serial::put_nmea ( vector< uint8_t >  data)
826  {
827  if (fd < 0)
828  {
830  return (error);
831  }
832 
833  uint8_t cs_in, digit1, digit2;
834  string message_sent; // for debugging
835 
836  cs_in = 0;
837 
838  // All commands start with a dollar sign, followed by a
839  // character command, a comma, command specific parameters,
840  // an asterisk, a checksum, and a newline character.
841  // An example for VN-100 command is shown below.
842  // $VNRRG,11*73
843 
844  // start command '$'
845  error = put_char('$');
846  message_sent = '$';
847 
848  // iterate through the buffer to send each charcter to serial port
849  size_t j;
850  for (j=0; j<data.size(); j++)
851  {
852  error = put_char(data[j]);
853  message_sent += data[j];
854  // check sum (xor?)
855  cs_in ^= (uint8_t)data[j];
856  }
857  // end of command '*'
858  error = put_char('*');
859  if (error < 0)
860  {
861  return error;
862  }
863  message_sent += '*';
864 
865  if (cs_in > 16)
866  {
867  digit1 = cs_in/16;
868  if (digit1 < 10)
869  {
870  error = put_char( '0'+digit1);
871  if (error < 0)
872  {
873  return error;
874  }
875  message_sent += '0'+digit1;
876  }
877  else
878  {
879  error = put_char( 'A'+digit1-10);
880  if (error < 0)
881  {
882  return error;
883  }
884  message_sent += 'A'+digit1-10;
885  }
886  }
887  else
888  {
889  error = put_char( '0');
890  if (error < 0)
891  {
892  return error;
893  }
894  message_sent += '0';
895  }
896 
897  ++j;
898  digit2 = cs_in%16;
899  if (digit2 <10 )
900  {
901  error = put_char( '0'+digit2);
902  if (error < 0)
903  {
904  return error;
905  }
906  message_sent += '0'+digit2;
907  }
908  else
909  {
910  error = put_char( 'A'+digit2-10);
911  if (error < 0)
912  {
913  return error;
914  }
915  message_sent += 'A'+digit2-10;
916  }
917  ++j;
918  error = put_char( '\n');
919  if (error < 0)
920  {
921  return error;
922  }
923  message_sent += "<CR><LF>";
924  return (j+3);
925  }
int32_t put_char(uint8_t c)
Definition: serialclass.cpp:609
int fd
Definition: serialclass.h:88
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
int32_t Cosmos::Serial::drain ( )
928  {
929  if (fd < 0)
930  {
932  return (error);
933  }
934 
935 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
936  // tcdrain(fd);
937  // tcflush(fd,TCOFLUSH);
938  // tcflush(fd,TCIFLUSH);
939  tcflush(fd, TCIOFLUSH);
940 #else // windows
941  PurgeComm(handle, PURGE_RXCLEAR|PURGE_TXCLEAR);
942 #endif
943  return 0;
944  }
int fd
Definition: serialclass.h:88
HANDLE handle
Definition: serialclass.h:103
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
int32_t Cosmos::Serial::get_char ( uint8_t &  buffer)
947  {
948  // if file descirptor return error (<0) then fail
949  if (fd < 0)
950  {
952  return (error);
953  }
954 
955  error = get_char();
956  if (error >= 0)
957  {
958  buffer = error;
959  }
960  return error;
961  }
int32_t get_char()
Definition: serialclass.cpp:987
static char buffer[255]
Definition: propagator_simple.cpp:60
int fd
Definition: serialclass.h:88
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
int32_t Cosmos::Serial::ReceiveByte ( uint8_t &  buf)
963  {
964  int n = 0;
965  buf = '\0'; // emtpy buffer
966  //n = ReceiveBuffer(&buf, size);
967 #ifdef COSMOS_WIN_OS
968  ReadFile(handle, &buf, 1, (LPDWORD)((void *)&n), NULL);
969 #endif
970 
971  return(n);
972  }
HANDLE handle
Definition: serialclass.h:103
char buf[128]
Definition: rw_test.cpp:40
int32_t Cosmos::Serial::ReceiveBuffer ( uint8_t *  buf,
int  size 
)
974  {
975  int n = 0;
976 
977  if(size>4096) size = 4096;
978 
979  //uint8_t *data_rx;
980 #ifdef COSMOS_WIN_OS
981  ReadFile(handle, buf, size, (LPDWORD)((void *)&n), NULL);
982 #endif
983 
984  return(n);
985  }
HANDLE handle
Definition: serialclass.h:103
char buf[128]
Definition: rw_test.cpp:40
int32_t Cosmos::Serial::get_char ( )
988  {
989  // if file descirptor return error (<0) then fail
990  if (fd < 0)
991  {
993  return (error);
994  }
995 
996  int result;
997  uint8_t c;
998 
999 #ifdef COSMOS_WIN_OS
1000  ElapsedTime et;
1001  do
1002  {
1003  int n=0;
1004  result = ReadFile(handle, &c, 1, (LPDWORD)((void *)&n), NULL);
1005  if (result < 0)
1006  {
1007  result = -WSAGetLastError();
1008  }
1009  else
1010  {
1011  result = SERIAL_ERROR_TIMEOUT;
1012  }
1013  if (result > 0)
1014  {
1015  error = c;
1016  break;
1017  }
1018  else
1019  {
1020  error = result;
1021  }
1022  COSMOS_SLEEP(ictimeout < 1. ? ictimeout/10. : .1);
1023  } while (error == SERIAL_ERROR_TIMEOUT && et.split() < ictimeout);
1024 #else
1025  ElapsedTime et;
1026  do
1027  {
1028  fd_set set;
1029  FD_ZERO(&set);
1030  FD_SET(fd, &set);
1031  timeval timeout;
1032  timeout.tv_sec = static_cast<int32_t>(ictimeout);
1033  timeout.tv_usec = static_cast<int32_t>(1000000. * (ictimeout - timeout.tv_sec));
1034  int rv = select(fd+1, &set, nullptr, nullptr, &timeout);
1035  if (rv == -1)
1036  {
1037  error = -errno;
1038  }
1039  else if (rv == 0)
1040  {
1042  }
1043  else
1044  {
1045  if (FD_ISSET(fd, &set))
1046  {
1047  result = read(fd, &c, 1);
1048  if (result > 0)
1049  {
1050  error = c;
1051  break;
1052  }
1053  else
1054  {
1055  if (result < 0)
1056  {
1057  error = -errno;
1058  }
1059  else
1060  {
1062  }
1063  }
1064  }
1065  }
1066  } while (et.split() < ictimeout);
1067 #endif
1068 
1069  return error;
1070  }
#define SERIAL_ERROR_TIMEOUT
Definition: cosmos-errno.h:171
#define SERIAL_ERROR_EOT
Definition: cosmos-errno.h:172
ElapsedTime et
Definition: agent_cpu_device_test.cpp:51
int fd
Definition: serialclass.h:88
Definition: elapsedtime.h:62
HANDLE handle
Definition: serialclass.h:103
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
double split()
ElapsedTime::split, gets the current elapsed time since the start()
Definition: elapsedtime.cpp:234
int32_t error
Definition: serialclass.h:89
double ictimeout
Definition: serialclass.h:95
int32_t Cosmos::Serial::poll_char ( )
1073  {
1074  // if file descirptor return error (<0) then fail
1075  if (fd < 0)
1076  {
1078  return (error);
1079  }
1080 
1081  int result;
1082  uint8_t c;
1083 
1084 #ifdef COSMOS_WIN_OS
1085  ElapsedTime et;
1086  do
1087  {
1088  int n=0;
1089  result = ReadFile(handle, &c, 1, (LPDWORD)((void *)&n), NULL);
1090  if (result < 0)
1091  {
1092  result = -WSAGetLastError();
1093  }
1094  else
1095  {
1096  result = SERIAL_ERROR_TIMEOUT;
1097  }
1098  if (result > 0)
1099  {
1100  error = c;
1101  break;
1102  }
1103  else
1104  {
1105  error = result;
1106  }
1107  COSMOS_SLEEP(ictimeout < 1. ? ictimeout/10. : .1);
1108  } while (error == SERIAL_ERROR_TIMEOUT && et.split() < ictimeout);
1109 #else
1110  ElapsedTime et;
1111  do
1112  {
1113  result = read(fd, &c, 1);
1114  if (result > 0)
1115  {
1116  error = c;
1117  break;
1118  }
1119  else
1120  {
1121  if (result < 0)
1122  {
1123  error = -errno;
1124  }
1125  }
1126  } while (et.split() < ictimeout);
1127 #endif
1128 
1129 // printf("{%.5f}", et.split());
1130 
1131  if (et.split() > ictimeout)
1132  {
1133  return SERIAL_ERROR_TIMEOUT;
1134  }
1135  else
1136  {
1137  return error;
1138  }
1139  }
#define SERIAL_ERROR_TIMEOUT
Definition: cosmos-errno.h:171
ElapsedTime et
Definition: agent_cpu_device_test.cpp:51
int fd
Definition: serialclass.h:88
Definition: elapsedtime.h:62
HANDLE handle
Definition: serialclass.h:103
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
double split()
ElapsedTime::split, gets the current elapsed time since the start()
Definition: elapsedtime.cpp:234
int32_t error
Definition: serialclass.h:89
double ictimeout
Definition: serialclass.h:95
int32_t Cosmos::Serial::get_string ( string &  data,
size_t  size = SIZE_MAX 
)
1142  {
1143  if (fd < 0)
1144  {
1146  return (error);
1147  }
1148 
1149  data.clear();
1150  for (uint16_t i=0; i<size; ++i)
1151  {
1152  if ((error=get_char()) < 0)
1153  {
1154  if (error == SERIAL_ERROR_TIMEOUT)
1155  {
1156  return(i);
1157  }
1158  else
1159  {
1160  return error;
1161  }
1162  }
1163  else
1164  {
1165  data.append(1, (char)error);
1166  }
1167  }
1168  return (static_cast<int32_t>(size));
1169  }
#define SERIAL_ERROR_TIMEOUT
Definition: cosmos-errno.h:171
int i
Definition: rw_test.cpp:37
int32_t get_char()
Definition: serialclass.cpp:987
int fd
Definition: serialclass.h:88
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
int32_t Cosmos::Serial::get_string ( string &  data,
char  endc = 0 
)
1172  {
1173  if (fd < 0)
1174  {
1176  return (error);
1177  }
1178 
1179  data.clear();
1180  do
1181  {
1182  if ((error=get_char()) < 0)
1183  {
1184  if (error == SERIAL_ERROR_TIMEOUT)
1185  {
1186  return(data.size());
1187  }
1188  else
1189  {
1190  return error;
1191  }
1192  }
1193  else
1194  {
1195  data.append(1, (char)error);
1196  }
1197  } while (error != endc);
1198 
1199  return (static_cast<int32_t>(data.size()));
1200  }
#define SERIAL_ERROR_TIMEOUT
Definition: cosmos-errno.h:171
int32_t get_char()
Definition: serialclass.cpp:987
int fd
Definition: serialclass.h:88
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
int32_t Cosmos::Serial::get_data ( vector< uint8_t > &  data,
size_t  size = SIZE_MAX 
)
1203  {
1204  if (fd < 0)
1205  {
1207  return (error);
1208  }
1209 
1210  for (uint16_t i=0; i<size; ++i)
1211  {
1212  if ((error=get_char()) < 0)
1213  {
1214  if (error == SERIAL_ERROR_TIMEOUT)
1215  {
1216  return(i);
1217  }
1218  else
1219  {
1220  return error;
1221  }
1222  }
1223  else
1224  {
1225  data.push_back(static_cast<uint8_t>(error));
1226  }
1227  }
1228  return (static_cast<int32_t>(size));
1229  }
#define SERIAL_ERROR_TIMEOUT
Definition: cosmos-errno.h:171
int i
Definition: rw_test.cpp:37
int32_t get_char()
Definition: serialclass.cpp:987
int fd
Definition: serialclass.h:88
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
int32_t Cosmos::Serial::get_data ( uint8_t *  data,
size_t  size 
)
1232  {
1233  if (fd < 0)
1234  {
1236  return (error);
1237  }
1238 
1239  for (uint16_t i=0; i<size; ++i)
1240  {
1241  if ((error=get_char()) < 0)
1242  {
1243  if (error == SERIAL_ERROR_TIMEOUT)
1244  {
1245  return(i);
1246  }
1247  else
1248  {
1249  return error;
1250  }
1251  }
1252  else
1253  {
1254  data[i] = static_cast<uint8_t>(error);
1255  }
1256  }
1257  return (static_cast<int32_t>(size));
1258  }
#define SERIAL_ERROR_TIMEOUT
Definition: cosmos-errno.h:171
int i
Definition: rw_test.cpp:37
int32_t get_char()
Definition: serialclass.cpp:987
int fd
Definition: serialclass.h:88
#define SERIAL_ERROR_OPEN
Definition: cosmos-errno.h:177
int32_t error
Definition: serialclass.h:89
int32_t Cosmos::Serial::get_xmodem ( vector< uint8_t > &  data,
size_t  size 
)

Read Xmodem frame.

Read one Xmodem block (frame) of data, removing control characters and calculating checksum. Supplied buffer is assumed to be at least 128 bytes.

Parameters
dataByte array to store incoming data.
sizeSize , in bytes, of byte array.
Returns
Packet number or negative error.
1269  {
1270  int32_t ch;
1271 
1272  ch = get_char();
1273  if (ch != XMODEM_SOH)
1274  {
1275  if (ch == XMODEM_EOT)
1276  {
1277  return SERIAL_ERROR_EOT;
1278  }
1279  else
1280  {
1281  return SERIAL_ERROR_READ;
1282  }
1283  }
1284  uint8_t blocknum = static_cast<uint8_t>(get_char());
1285  ch = get_char();
1286  if (255-blocknum != ch)
1287  {
1288  return SERIAL_ERROR_READ;
1289  }
1290 
1291  uint16_t i = 0;
1292  uint8_t csum = 0;
1293  do
1294  {
1295  ch = get_char();
1296  if (ch < 0)
1297  {
1298  return (ch);
1299  }
1300  data.push_back(static_cast<uint8_t>(ch));
1301  ++i;
1302  csum += ch;
1303  } while (i<128);
1304 
1305  ch = get_char();
1306  if (ch != csum)
1307  {
1308  return SERIAL_ERROR_CHECKSUM;
1309  }
1310 
1311  return blocknum;
1312  }
int i
Definition: rw_test.cpp:37
#define SERIAL_ERROR_EOT
Definition: cosmos-errno.h:172
int32_t get_char()
Definition: serialclass.cpp:987
#define XMODEM_EOT
Definition: cssl_lib.h:25
#define SERIAL_ERROR_CHECKSUM
Definition: cosmos-errno.h:169
#define XMODEM_SOH
Definition: cssl_lib.h:24
#define SERIAL_ERROR_READ
Definition: cosmos-errno.h:170
int32_t Cosmos::Serial::get_slip ( vector< uint8_t > &  data,
size_t  size = SIZE_MAX 
)

Read SLIP frame.

Read an entire frame of SLIP encoded data from the serial port. Special SLIP characters are removed on the fly. Will stop early if supplied buffer size is exceeded.

Parameters
dataByte array to store incoming data.
sizeSize , in bytes, of byte array.
Returns
Number of bytes read, up to maximum.
1323  {
1324  int32_t ch;
1325 // uint16_t i;
1326 
1327  data.clear();
1328  do
1329  {
1330  ch = get_char();
1331  if (ch < 0)
1332  {
1333  if (ch == SERIAL_ERROR_TIMEOUT)
1334  {
1335  return (SERIAL_ERROR_SLIPIN);
1336  }
1337  else
1338  {
1339  return (ch);
1340  }
1341  }
1342  } while (ch != SLIP_FEND);
1343 
1344 // i = 0;
1345  do
1346  {
1347  ch = get_char();
1348  if (ch < 0)
1349  {
1350  if (ch == SERIAL_ERROR_TIMEOUT)
1351  {
1352  return (SERIAL_ERROR_SLIPOUT);
1353  }
1354  else
1355  {
1356  return (ch);
1357  }
1358  }
1359 // if (i < size)
1360  if (data.size() < size)
1361  {
1362  switch (ch)
1363  {
1364  case SLIP_FESC:
1365  ch = get_char();
1366  switch (ch)
1367  {
1368  case SLIP_TFEND:
1369  data.push_back(SLIP_FEND);
1370  break;
1371  case SLIP_TFESC:
1372  data.push_back(SLIP_FESC);
1373  break;
1374  }
1375 // ++i;
1376  break;
1377  case SLIP_FEND:
1378  break;
1379  default:
1380  data.push_back(static_cast<uint8_t>(ch));
1381 // ++i;
1382  break;
1383  }
1384  }
1385  } while (ch != SLIP_FEND);
1386 
1387 // return (i);
1388  return data.size();
1389  }
#define SERIAL_ERROR_TIMEOUT
Definition: cosmos-errno.h:171
#define SERIAL_ERROR_SLIPIN
Definition: cosmos-errno.h:173
#define SLIP_FESC
SLIP Buffer Escape character.
Definition: sliplib.h:60
int32_t get_char()
Definition: serialclass.cpp:987
#define SERIAL_ERROR_SLIPOUT
Definition: cosmos-errno.h:174
#define SLIP_TFEND
SLIP Buffer Escaped End character.
Definition: sliplib.h:64
#define SLIP_FEND
SLIP Buffer End character.
Definition: sliplib.h:56
#define SLIP_TFESC
SLIP Buffer Escaped Escape character.
Definition: sliplib.h:68
int32_t Cosmos::Serial::get_nmea ( vector< uint8_t > &  data,
size_t  size 
)

Read NMEA response.

Read an entire NMEA response from the serial port. The leading $ and trailing * and checksum are removed, and only the payload of the response is returned. Will stop early if supplied buffer size is exceeded.

Parameters
dataByte array to store incoming data.
sizeSize , in bytes, of byte array.
Returns
Number of bytes read, up to maximum.
1401  {
1402  int32_t ch;
1403  uint16_t i;
1404  uint8_t cs_in, cs_out;
1405  string input;
1406 
1407  do
1408  {
1409  ch = get_char();
1410  input += static_cast<char>(ch);
1411  if (ch < 0) return (ch);
1412  } while (ch != '$');
1413 
1414  i = 0;
1415  cs_in = 0;
1416  do
1417  {
1418  ch = get_char();
1419  input += static_cast<char>(ch);
1420  if (ch < 0) return (ch);
1421  if (i < size)
1422  {
1423  switch (ch)
1424  {
1425  case '*':
1426  break;
1427  default:
1428  cs_in ^= ch;
1429  data.push_back(static_cast<uint8_t>(ch));
1430  ++i;
1431  break;
1432  }
1433  }
1434  } while (ch != '*');
1435  ch = get_char();
1436  input += static_cast<char>(ch);
1437  if (ch < 0) return (ch);
1438  if (ch > '9')
1439  {
1440  if (ch > 'F')
1441  {
1442  cs_out = (static_cast<uint8_t>(ch) - 'a' + 10) * 16;
1443  }
1444  else
1445  {
1446  cs_out = (static_cast<uint8_t>(ch) - 'A' + 10) * 16;
1447  }
1448  }
1449  else
1450  {
1451  cs_out = (static_cast<uint8_t>(ch) - '0') * 16;
1452  }
1453  ch = get_char();
1454  input += static_cast<char>(ch);
1455  if (ch < 0) return (ch);
1456  if (ch > '9')
1457  {
1458  if (ch > 'F')
1459  {
1460  cs_out += (ch - 'a' + 10);
1461  }
1462  else
1463  {
1464  cs_out += (ch - 'A' + 10);
1465  }
1466  }
1467  else
1468  {
1469  cs_out += (ch - '0');
1470  }
1471  if (cs_in != cs_out)
1472  return (SERIAL_ERROR_CHECKSUM);
1473  ch = get_char();
1474  input += static_cast<char>(ch);
1475 
1476  return (i);
1477  }
int i
Definition: rw_test.cpp:37
int32_t get_char()
Definition: serialclass.cpp:987
#define SERIAL_ERROR_CHECKSUM
Definition: cosmos-errno.h:169