COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
Collaboration diagram for Smple Serial Library Functions:

Functions

const char * cssl_geterrormsg ()
 
int32_t cssl_geterror ()
 
int32_t cssl_start ()
 
int32_t cssl_stop ()
 
cssl_tcssl_open (const char *fname, int baud, int bits, int parity, int stop)
 
int32_t cssl_close (cssl_t *serial)
 
int32_t cssl_setup (cssl_t *serial, int baud, int bits, int parity, int stop)
 
int32_t cssl_setflowcontrol (cssl_t *serial, int rtscts, int xonxoff)
 
int32_t cssl_settimeout (cssl_t *serial, int, double timeout)
 Set read timeout. More...
 
int32_t cssl_putchar (cssl_t *serial, uint8_t c)
 
int32_t cssl_putstring (cssl_t *serial, char *str)
 
int32_t cssl_putdata (cssl_t *serial, uint8_t *data, uint32_t datalen)
 
int32_t cssl_putslip (cssl_t *serial, uint8_t *buf, size_t size)
 
int32_t cssl_putnmea (cssl_t *serial, uint8_t *buf, size_t size)
 
int32_t cssl_drain (cssl_t *serial)
 
int32_t cssl_getchar (cssl_t *serial)
 
int32_t cssl_getdata (cssl_t *serial, uint8_t *buffer, int size)
 
int32_t cssl_getxmodem (cssl_t *serial, uint8_t *buf)
 Read Xmodem frame. More...
 
int32_t cssl_getslip (cssl_t *serial, uint8_t *buf, uint16_t size)
 Read SLIP frame. More...
 
int32_t cssl_getnmea (cssl_t *serial, uint8_t *buf, uint16_t size)
 Read NMEA response. More...
 

Detailed Description

Function Documentation

const char * cssl_geterrormsg ( )
57 {
58  return cssl_errors[cssl_error];
59 }
static cssl_error_t cssl_error
Definition: cssl_lib.cpp:43
static const char * cssl_errors[]
Definition: cssl_lib.cpp:32
int32_t cssl_geterror ( )
64 {
65  return cssl_error;
66 }
static cssl_error_t cssl_error
Definition: cssl_lib.cpp:43
int32_t cssl_start ( )
74 {
75  if (cssl_started) {
76  return 0;
77  }
78 
79  /* OK, the cssl is started */
80  cssl_started=1;
82  return 0;
83 }
static int cssl_started
Definition: cssl_lib.cpp:26
static cssl_error_t cssl_error
Definition: cssl_lib.cpp:43
Definition: cssl_lib.h:56
int32_t cssl_stop ( )
87 {
88  /* if not started we do nothing */
89  if (!cssl_started)
90  return 0;
91 
92  /* we close all ports, and free the list */
93  while (head)
95 
96  /* And at least : */
97  cssl_started=0;
99  return 0;
100 }
static int cssl_started
Definition: cssl_lib.cpp:26
static cssl_t * head
Definition: cssl_lib.cpp:29
static cssl_error_t cssl_error
Definition: cssl_lib.cpp:43
int32_t cssl_close(cssl_t *serial)
Definition: cssl_lib.cpp:197
Definition: cssl_lib.h:56
cssl_t * cssl_open ( const char *  fname,
int  baud,
int  bits,
int  parity,
int  stop 
)
113 {
114  cssl_t *serial;
115 
116  if (!cssl_started) {
118  return NULL;
119  }
120 
121  /* create new cssl_t structure */
122  serial=(cssl_t*)calloc(1,sizeof(cssl_t));
123 
124  /* oops, no memory */
125  if (!serial) {
127  return 0;
128  }
129 
130  /* opening the file */
131  /* the read/write operations will be blocking */
132 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS)
133  serial->fd=open(fname, O_RDWR | O_NOCTTY);
134 #endif
135 
136 #if defined(COSMOS_MAC_OS)
137  serial->fd=open(fname, O_RDWR | O_NOCTTY | O_NDELAY);
138 #endif
139 
140 #if defined(COSMOS_WIN_OS)
141  serial->handle = CreateFileA(fname,
142  GENERIC_READ|GENERIC_WRITE,
143  0, /* no share */
144  NULL, /* no security */
145  OPEN_EXISTING,
146  0, /* no threads */ // FILE_ATTRIBUTE_NORMAL
147  NULL); /* no templates */
148 
149  if(serial->handle==INVALID_HANDLE_VALUE)
150  {
151  std::cout << "unable to open serial port" << std::endl;
152  exit(0);
153  }
154 
155  serial->fd = _open_osfhandle((intptr_t)serial->handle, 0);
156 #endif
157 
158  /* oops, cannot open */
159  if (serial->fd == -1) {
161  free(serial);
162  return NULL;
163  }
164 
165  /* we remember old termios */
166 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
167  tcgetattr(serial->fd,&(serial->oldtio));
168 #else // windows
169  // does not seem to be necessary
170  // GetCommState(serial->handle, &(serial->olddcb));
171 #endif
172 
173  /* now we set new values */
174  // TODO: what a bit ERROR, parity and bits is flipped
175  //cssl_setup(serial,baud,parity,bits,stop);
176 
177  // new! TODO: check if all functions that call cssl_open work
178  cssl_setup(serial,baud,bits,parity,stop);
179 
180  // Default to no flow control
181  cssl_setflowcontrol(serial, 0, 0);
182 
183  // Set minimal timeout so we don't give up too soon
184  cssl_settimeout(serial, 0, .1);
185 
186  /* we add the serial to our list */
187  serial->next=head;
188  head=serial;
189 
191 
192  return serial;
193 }
static int cssl_started
Definition: cssl_lib.cpp:26
HANDLE handle
Definition: cssl_lib.h:46
struct __cssl_t * next
Definition: cssl_lib.h:51
Definition: cssl_lib.h:62
int fd
Definition: cssl_lib.h:38
Definition: cssl_lib.h:58
static char fname[100]
Definition: geomag.cpp:89
static cssl_t * head
Definition: cssl_lib.cpp:29
static cssl_error_t cssl_error
Definition: cssl_lib.cpp:43
Definition: cssl_lib.h:61
int32_t cssl_setup(cssl_t *serial, int baud, int bits, int parity, int stop)
Definition: cssl_lib.cpp:257
int32_t cssl_setflowcontrol(cssl_t *serial, int rtscts, int xonxoff)
Definition: cssl_lib.cpp:389
Definition: cssl_lib.h:56
int32_t cssl_settimeout(cssl_t *serial, int, double timeout)
Set read timeout.
Definition: cssl_lib.cpp:469
cssl_t * serial
Definition: arduino_lib.cpp:63
Definition: cssl_lib.h:34
int32_t cssl_close ( cssl_t serial)
198 {
199  cssl_t *cur;
200 
201  if (!cssl_started) {
203  return (cssl_error);
204  }
205 
206  if (!serial) {
208  return (cssl_error);
209  }
210 
211  /* first we flush the port */
212 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
213  tcflush(serial->fd,TCOFLUSH);
214  tcflush(serial->fd,TCIFLUSH);
215 #endif
216 
217  /* then we restore old settings */
218 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
219  tcsetattr(serial->fd,TCSANOW,&(serial->oldtio));
220 #else
221  SetCommState(serial->handle, &(serial->dcb));
222 #endif
223 
224  /* and close the file */
225  close(serial->fd);
226 
227  /* now we can remove the serial from the list */
228 
229  if (head==serial) {
230  head=serial->next;
231  free(serial);
233  return 0;
234  }
235 
236  for (cur=head;cur;cur=cur->next) {
237  if (cur->next==serial) {
238  cur->next=serial->next;
239  free(serial);
241  return 0;
242  }
243  }
244 
245  /* we should never reach there,
246  it means, that serial was not found in the list */
248  return (cssl_error);
249 }
static int cssl_started
Definition: cssl_lib.cpp:26
struct _DCB dcb
Definition: cssl_lib.h:44
HANDLE handle
Definition: cssl_lib.h:46
struct __cssl_t * next
Definition: cssl_lib.h:51
Definition: cssl_lib.h:59
int fd
Definition: cssl_lib.h:38
Definition: cssl_lib.h:58
static cssl_t * head
Definition: cssl_lib.cpp:29
static cssl_error_t cssl_error
Definition: cssl_lib.cpp:43
Definition: cssl_lib.h:56
Definition: cssl_lib.h:60
Definition: cssl_lib.h:34
int32_t cssl_setup ( cssl_t serial,
int  baud,
int  bits,
int  parity,
int  stop 
)
258 {
259 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
260  tcflag_t baudrate;
261  tcflag_t databits;
262  tcflag_t stopbits;
263  tcflag_t checkparity;
264 #endif
265 
266  if (!cssl_started) {
268  return (cssl_error);
269  }
270 
271  if (!serial) {
273  return (cssl_error);
274  }
275 
276  /* get the proper baudrate */
277  serial->baud = baud;
278 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
279  switch (baud) {
280  case 75:
281  baudrate=B75;
282  break;
283  case 110:
284  baudrate=B110;
285  break;
286  case 150:
287  baudrate=B150;
288  break;
289  case 300:
290  baudrate=B300;
291  break;
292  case 600:
293  baudrate=B600;
294  break;
295  case 1200:
296  baudrate=B1200;
297  break;
298  case 2400:
299  baudrate=B2400;
300  break;
301  case 4800:
302  baudrate=B4800;
303  break;
304  case 9600:
305  baudrate=B9600;
306  break;
307  case 19200:
308  baudrate=B19200;
309  break;
310  case 38400:
311  baudrate=B38400;
312  break;
313  case 57600:
314  baudrate=B57600;
315  break;
316  case 115200:
317  baudrate=B115200;
318  break;
319  default:
320  baudrate=B9600;
321  }
322 
323  /* databits */
324  switch (bits) {
325  case 7:
326  databits=CS7;
327  break;
328  case 8:
329  databits=CS8;
330  break;
331  default:
332  databits=CS8;
333  }
334 
335  /* parity, */
336  switch (parity) {
337  case 0:
338  checkparity=0;
339  break;
340  case 1: /* odd */
341  checkparity=PARENB|PARODD;
342  break;
343  case 2:
344  checkparity=PARENB;
345  break;
346  default:
347  checkparity=0;
348  }
349 
350  /* and stop bits */
351  switch (stop) {
352  case 1:
353  stopbits=0;
354  break;
355  case 2:
356  stopbits=CSTOPB;
357  break;
358  default:
359  stopbits=0;
360  }
361 
362  /* now we setup the values in port's termios */
363  serial->tio.c_cflag=baudrate|databits|checkparity|stopbits|CLOCAL|CREAD;
364  serial->tio.c_iflag=IGNPAR;
365  serial->tio.c_oflag=0;
366  serial->tio.c_lflag=0;
367  serial->tio.c_cc[VMIN]=1;
368  serial->tio.c_cc[VTIME]=0;
369 
370  /* we flush the port */
371  tcflush(serial->fd,TCOFLUSH);
372  tcflush(serial->fd,TCIFLUSH);
373 
374  /* we send new config to the port */
375  tcsetattr(serial->fd,TCSANOW,&(serial->tio));
376 #else // windows
377  serial->dcb.BaudRate = baud;
378  serial->dcb.Parity = parity;
379  serial->dcb.StopBits = stop;
380  serial->dcb.ByteSize = bits;
381  serial->dcb.DCBlength = sizeof(DCB);
382  SetCommState(serial->handle, &(serial->dcb));
383 #endif
384 
386  return 0;
387 }
static int cssl_started
Definition: cssl_lib.cpp:26
struct _DCB dcb
Definition: cssl_lib.h:44
HANDLE handle
Definition: cssl_lib.h:46
Definition: cssl_lib.h:59
int fd
Definition: cssl_lib.h:38
Definition: cssl_lib.h:58
static cssl_error_t cssl_error
Definition: cssl_lib.cpp:43
Definition: cssl_lib.h:56
int baudrate
Definition: arduino_lib.cpp:60
int baud
Definition: cssl_lib.h:49
int32_t cssl_setflowcontrol ( cssl_t serial,
int  rtscts,
int  xonxoff 
)
390 {
391  if (!cssl_started)
392  {
394  return (cssl_error);
395  }
396 
397  if (!serial)
398  {
400  return (cssl_error);
401  }
402 
403 #if defined(COSMOS_WIN_OS)
404  GetCommState(serial->handle, &(serial->dcb));
405  if (rtscts)
406  {
407  serial->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
408  serial->dcb.fOutxCtsFlow = TRUE;
409 
410  }
411  else
412  {
413  serial->dcb.fRtsControl = RTS_CONTROL_DISABLE;
414  serial->dcb.fOutxCtsFlow = FALSE;
415  }
416 #else
417  /* We setup rts/cts (hardware) flow control */
418  if (rtscts)
419  {
420  serial->tio.c_cflag |= CRTSCTS;
421  } else
422  {
423  serial->tio.c_cflag &= ~CRTSCTS;
424  }
425 #endif
426 
427 #if defined(COSMOS_WIN_OS)
428  if (xonxoff)
429  {
430  serial->dcb.fInX = serial->dcb.fOutX = TRUE;
431  }
432  else
433  {
434  serial->dcb.fInX = serial->dcb.fOutX = FALSE;
435 
436  }
437  SetCommState(serial->handle, &(serial->dcb));
438 #else
439  /* We setup xon/xoff (soft) flow control */
440  if (xonxoff)
441  {
442  serial->tio.c_iflag |= (IXON|IXOFF);
443  }
444  else
445  {
446  serial->tio.c_iflag &= ~(IXON|IXOFF);
447  }
448 
449  tcsetattr(serial->fd,TCSANOW,&(serial->tio));
450 #endif
451 
453  return 0;
454 }
static int cssl_started
Definition: cssl_lib.cpp:26
struct _DCB dcb
Definition: cssl_lib.h:44
HANDLE handle
Definition: cssl_lib.h:46
Definition: cssl_lib.h:59
int fd
Definition: cssl_lib.h:38
#define FALSE
Definition: jpleph.cpp:69
Definition: cssl_lib.h:58
static cssl_error_t cssl_error
Definition: cssl_lib.cpp:43
#define TRUE
Definition: jpleph.cpp:68
Definition: cssl_lib.h:56
int32_t cssl_settimeout ( cssl_t serial,
int  ,
double  timeout 
)

Set read timeout.

471 {
472  if (!cssl_started)
473  {
475  return (cssl_error);
476  }
477 
478  if (!serial)
479  {
481  return (cssl_error);
482  }
483 
484 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
485  serial->tio.c_cc[VMIN]=minchar;
486  serial->tio.c_cc[VTIME]=(int)(timeout*10.+.4);
487 
488  tcsetattr(serial->fd,TCSANOW,&(serial->tio));
489 #else // windows
490  _COMMTIMEOUTS timeouts;
491  timeouts.ReadIntervalTimeout = timeout * 1000.;
492  SetCommTimeouts(serial->handle, &timeouts);
493 #endif
494 
496  return 0;
497 }
static int cssl_started
Definition: cssl_lib.cpp:26
HANDLE handle
Definition: cssl_lib.h:46
Definition: cssl_lib.h:59
int fd
Definition: cssl_lib.h:38
Definition: cssl_lib.h:58
static cssl_error_t cssl_error
Definition: cssl_lib.cpp:43
Definition: cssl_lib.h:56
int32_t cssl_putchar ( cssl_t serial,
uint8_t  c 
)
506 {
507  if (!cssl_started)
508  {
510  return (CSSL_ERROR_NOTSTARTED);
511  }
512 
513  if (!serial)
514  {
516  return (CSSL_ERROR_NULLPOINTER);
517  }
518 
519 #ifdef COSMOS_WIN_OS
520  int n=0;
521  WriteFile(serial->handle, &c, 1, (LPDWORD)((void *)&n), NULL);
522  if(n<0) return(-errno);
523 #else
524  if (write(serial->fd,&c,1) < 0)
525  {
526  return (-errno);
527  }
528 #endif
529 
530  COSMOS_USLEEP(10000000/serial->baud);
531  return 0;
532 }
static int cssl_started
Definition: cssl_lib.cpp:26
HANDLE handle
Definition: cssl_lib.h:46
Definition: cssl_lib.h:59
int fd
Definition: cssl_lib.h:38
Definition: cssl_lib.h:58
static cssl_error_t cssl_error
Definition: cssl_lib.cpp:43
int baud
Definition: cssl_lib.h:49
int32_t cssl_putstring ( cssl_t serial,
char *  str 
)
537 {
538  if (!cssl_started) {
540  return (cssl_error);
541  }
542 
543  if (!serial) {
545  return (cssl_error);
546  }
547  int nbytes = write(serial->fd,str,strlen(str));
548  return nbytes;
549 }
static int cssl_started
Definition: cssl_lib.cpp:26
const string & str
Definition: json11.cpp:360
Definition: cssl_lib.h:59
int fd
Definition: cssl_lib.h:38
Definition: cssl_lib.h:58
static cssl_error_t cssl_error
Definition: cssl_lib.cpp:43
int32_t cssl_putdata ( cssl_t serial,
uint8_t *  data,
uint32_t  datalen 
)
554 {
555 // int32_t iretn;
556 
557  if (!cssl_started)
558  {
560  return (CSSL_ERROR_NOTSTARTED);
561  }
562 
563  if (!serial)
564  {
566  return (CSSL_ERROR_NULLPOINTER);
567  }
568 
569  /*
570  for (uint32_t i=0; i<datalen; ++i)
571  {
572  if ((iretn=cssl_putchar(serial, data[i])) < 0)
573  {
574  return iretn;
575  }
576  }
577  */
578  if (write(serial->fd,data,datalen) < 0)
579  {
580  return (-errno);
581  }
582  return 0;
583 }
static int cssl_started
Definition: cssl_lib.cpp:26
Definition: cssl_lib.h:59
int fd
Definition: cssl_lib.h:38
int datalen
Definition: mcastclient.c:41
Definition: cssl_lib.h:58
static cssl_error_t cssl_error
Definition: cssl_lib.cpp:43
int32_t cssl_putslip ( cssl_t serial,
uint8_t *  buf,
size_t  size 
)
587 {
588  size_t i, j;
589 
590  i = 0;
591  cssl_putchar(serial,SLIP_FEND);
592  for (j=0; j<size; j++)
593  {
594  switch (buf[j])
595  {
596  case SLIP_FEND:
597  cssl_putchar(serial,SLIP_FESC);
598  cssl_putchar(serial,SLIP_TFEND);
599  i+=2;
600  break;
601  case SLIP_FESC:
602  cssl_putchar(serial,SLIP_FESC);
603  cssl_putchar(serial,SLIP_TFESC);
604  i+=2;
605  break;
606  default:
607  cssl_putchar(serial,buf[j]);
608  i++;
609  break;
610  }
611  }
612  cssl_putchar(serial,SLIP_FEND);
613  return (i);
614 }
int i
Definition: rw_test.cpp:37
#define SLIP_FESC
SLIP Buffer Escape character.
Definition: sliplib.h:60
int32_t cssl_putchar(cssl_t *serial, uint8_t c)
Definition: cssl_lib.cpp:505
#define SLIP_TFEND
SLIP Buffer Escaped End character.
Definition: sliplib.h:64
#define SLIP_FEND
SLIP Buffer End character.
Definition: sliplib.h:56
char buf[128]
Definition: rw_test.cpp:40
#define SLIP_TFESC
SLIP Buffer Escaped Escape character.
Definition: sliplib.h:68
int32_t cssl_putnmea ( cssl_t serial,
uint8_t *  buf,
size_t  size 
)
618 {
619  size_t j;
620  uint8_t cs_in, digit1, digit2;
621  string message_sent; // for debugging
622 
623  cs_in = 0;
624 
625  // All commands start with a dollar sign, followed by a
626  // character command, a comma, command specific parameters,
627  // an asterisk, a checksum, and a newline character.
628  // An example for VN-100 command is shown below.
629  // $VNRRG,11*73
630 
631  // start command '$'
632  cssl_putchar(serial,'$');
633  message_sent = '$';
634 
635  // iterate through the buffer to send each charcter to serial port
636  for (j=0; j<size; j++)
637  {
638  cssl_putchar(serial,buf[j]);
639  message_sent += buf[j];
640  // check sum (xor?)
641  cs_in ^= (uint8_t)buf[j];
642  }
643  // end of command '*'
644  cssl_putchar(serial,'*');
645  message_sent += '*';
646 
647  if (cs_in > 16)
648  {
649  digit1 = cs_in/16;
650  if (digit1 < 10)
651  {
652  cssl_putchar(serial, '0'+digit1);
653  message_sent += '0'+digit1;
654  }
655  else
656  {
657  cssl_putchar(serial, 'A'+digit1-10);
658  message_sent += 'A'+digit1-10;
659  }
660  }
661  else
662  {
663  cssl_putchar(serial, '0');
664  message_sent += '0';
665  }
666 
667  ++j;
668  digit2 = cs_in%16;
669  if (digit2 <10 )
670  {
671  cssl_putchar(serial, '0'+digit2);
672  message_sent += '0'+digit2;
673  }
674  else
675  {
676  cssl_putchar(serial, 'A'+digit2-10);
677  message_sent += 'A'+digit2-10;
678  }
679  ++j;
680  cssl_putchar(serial, '\n');
681  message_sent += "<CR><LF>";
682  return (j+3);
683 }
int32_t cssl_putchar(cssl_t *serial, uint8_t c)
Definition: cssl_lib.cpp:505
char buf[128]
Definition: rw_test.cpp:40
int32_t cssl_drain ( cssl_t serial)
686 {
687  if (!cssl_started) {
689  return (cssl_error);
690  }
691 
692  if (!serial) {
694  return (cssl_error);
695  }
696 
697 #if defined(COSMOS_LINUX_OS) || defined(COSMOS_CYGWIN_OS) || defined(COSMOS_MAC_OS)
698 // tcdrain(serial->fd);
699 // tcflush(serial->fd,TCOFLUSH);
700 // tcflush(serial->fd,TCIFLUSH);
701  tcflush(serial->fd, TCIOFLUSH);
702 #else // windows
703  PurgeComm(serial->handle, PURGE_RXCLEAR|PURGE_TXCLEAR);
704 #endif
705  return 0;
706 }
static int cssl_started
Definition: cssl_lib.cpp:26
HANDLE handle
Definition: cssl_lib.h:46
Definition: cssl_lib.h:59
int fd
Definition: cssl_lib.h:38
Definition: cssl_lib.h:58
static cssl_error_t cssl_error
Definition: cssl_lib.cpp:43
int32_t cssl_getchar ( cssl_t serial)
710 {
711  int result;
712  uint8_t c;
713 
714 #ifdef COSMOS_WIN_OS
715  int n=0;
716  result = ReadFile(serial->handle, &c, 1, (LPDWORD)((void *)&n), NULL);
717 #else
718  result=read(serial->fd,&c,sizeof(c));
719 #endif
720  if (result > 0)
721  {
722  return c;
723  }
724  else
725  {
726  if (result < 0)
727  {
728  return (-errno);
729  }
730  else
731  {
732  return (CSSL_ERROR_TIMEOUT);
733  }
734  }
735 }
HANDLE handle
Definition: cssl_lib.h:46
#define CSSL_ERROR_TIMEOUT
Definition: cosmos-errno.h:162
int fd
Definition: cssl_lib.h:38
int32_t cssl_getdata ( cssl_t serial,
uint8_t *  buffer,
int  size 
)
739 {
740  int32_t iretn;
741 
742  for (uint16_t i=0; i<size; ++i)
743  {
744  if ((iretn=cssl_getchar(serial)) < 0)
745  {
746  if (iretn == CSSL_ERROR_TIMEOUT)
747  {
748  return(i);
749  }
750  else
751  {
752  return iretn;
753  }
754  }
755  else
756  {
757  buffer[i] = iretn;
758  }
759  }
760  return (size);
761 }
int i
Definition: rw_test.cpp:37
#define CSSL_ERROR_TIMEOUT
Definition: cosmos-errno.h:162
int iretn
Definition: rw_test.cpp:37
static char buffer[255]
Definition: propagator_simple.cpp:60
int32_t cssl_getchar(cssl_t *serial)
Definition: cssl_lib.cpp:709
int32_t cssl_getxmodem ( cssl_t serial,
uint8_t *  buf 
)

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
serialHandle returned from :cssl_open.
bufByte array to store incoming data.
Returns
Packet number or negative error.
772 {
773  int16_t ch;
774 
775  ch = cssl_getchar(serial);
776  if (ch != XMODEM_SOH)
777  {
778  if (ch == XMODEM_EOT)
779  {
780  return CSSL_ERROR_EOT;
781  }
782  else
783  {
784  return CSSL_ERROR_READ;
785  }
786  }
787  uint8_t blocknum = cssl_getchar(serial);
788  ch = cssl_getchar(serial);
789  if (255-blocknum != ch)
790  {
791  return CSSL_ERROR_READ;
792  }
793 
794  uint16_t i = 0;
795  uint8_t csum = 0;
796  do
797  {
798  ch = cssl_getchar(serial);
799  if (ch < 0)
800  {
801  return (ch);
802  }
803  buf[i] = ch;
804  ++i;
805  csum += ch;
806  } while (i<128);
807 
808  ch = cssl_getchar(serial);
809  if (ch != csum)
810  {
811  return CSSL_ERROR_CHECKSUM;
812  }
813 
814  return blocknum;
815 }
#define CSSL_ERROR_READ
Definition: cosmos-errno.h:161
#define CSSL_ERROR_EOT
Definition: cosmos-errno.h:163
int i
Definition: rw_test.cpp:37
#define XMODEM_EOT
Definition: cssl_lib.h:25
#define CSSL_ERROR_CHECKSUM
Definition: cosmos-errno.h:160
#define XMODEM_SOH
Definition: cssl_lib.h:24
int32_t cssl_getchar(cssl_t *serial)
Definition: cssl_lib.cpp:709
char buf[128]
Definition: rw_test.cpp:40
int32_t cssl_getslip ( cssl_t serial,
uint8_t *  buf,
uint16_t  size 
)

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
serialHandle returned from :cssl_open.
bufByte array to store incoming data.
sizeSize of byte array.
Returns
Number of bytes read, up to maximum.
827 {
828  int16_t ch;
829  uint16_t i;
830 
831  do
832  {
833  ch = cssl_getchar(serial);
834  if (ch < 0)
835  {
836  if (ch == CSSL_ERROR_TIMEOUT)
837  {
838  return (CSSL_ERROR_SLIPIN);
839  }
840  else
841  {
842  return (ch);
843  }
844  }
845  } while (ch != SLIP_FEND);
846 
847  i = 0;
848  do
849  {
850  ch = cssl_getchar(serial);
851  if (ch < 0)
852  {
853  if (ch == CSSL_ERROR_TIMEOUT)
854  {
855  return (CSSL_ERROR_SLIPOUT);
856  }
857  else
858  {
859  return (ch);
860  }
861  }
862  if (i < size)
863  {
864  switch (ch)
865  {
866  case SLIP_FESC:
867  ch = cssl_getchar(serial);
868  switch (ch)
869  {
870  case SLIP_TFEND:
871  buf[i] = SLIP_FEND;
872  break;
873  case SLIP_TFESC:
874  buf[i] = SLIP_FESC;
875  break;
876  }
877  ++i;
878  break;
879  case SLIP_FEND:
880  break;
881  default:
882  buf[i] = ch;
883  ++i;
884  break;
885  }
886  }
887  } while (ch != SLIP_FEND);
888 
889  return (i);
890 }
int i
Definition: rw_test.cpp:37
#define CSSL_ERROR_TIMEOUT
Definition: cosmos-errno.h:162
#define SLIP_FESC
SLIP Buffer Escape character.
Definition: sliplib.h:60
#define CSSL_ERROR_SLIPIN
Definition: cosmos-errno.h:164
#define SLIP_TFEND
SLIP Buffer Escaped End character.
Definition: sliplib.h:64
#define CSSL_ERROR_SLIPOUT
Definition: cosmos-errno.h:165
int32_t cssl_getchar(cssl_t *serial)
Definition: cssl_lib.cpp:709
#define SLIP_FEND
SLIP Buffer End character.
Definition: sliplib.h:56
char buf[128]
Definition: rw_test.cpp:40
#define SLIP_TFESC
SLIP Buffer Escaped Escape character.
Definition: sliplib.h:68
int32_t cssl_getnmea ( cssl_t serial,
uint8_t *  buf,
uint16_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
serialHandle returned from :cssl_open.
bufByte array to store incoming data.
sizeSize of byte array.
Returns
Number of bytes read, up to maximum.
903 {
904  int16_t ch;
905  uint16_t i;
906  uint8_t cs_in, cs_out;
907  string input;
908 
909  do
910  {
911  ch = cssl_getchar(serial);
912  input += ch;
913  if (ch < 0) return (ch);
914  } while (ch != '$');
915 
916  i = 0;
917  cs_in = 0;
918  do
919  {
920  ch = cssl_getchar(serial);
921  input += ch;
922  if (ch < 0) return (ch);
923  if (i < size)
924  {
925  switch (ch)
926  {
927  case '*':
928  break;
929  default:
930  cs_in ^= (uint8_t)ch;
931  buf[i] = ch;
932  ++i;
933  break;
934  }
935  }
936  } while (ch != '*');
937  ch = cssl_getchar(serial);
938  input += ch;
939  if (ch < 0) return (ch);
940  if (ch > '9')
941  {
942  if (ch > 'F')
943  {
944  cs_out = (ch - 'a' + 10) * 16;
945  }
946  else
947  {
948  cs_out = (ch - 'A' + 10) * 16;
949  }
950  }
951  else
952  {
953  cs_out = (ch - '0') * 16;
954  }
955  ch = cssl_getchar(serial);
956  input += ch;
957  if (ch < 0) return (ch);
958  if (ch > '9')
959  {
960  if (ch > 'F')
961  {
962  cs_out += (ch - 'a' + 10);
963  }
964  else
965  {
966  cs_out += (ch - 'A' + 10);
967  }
968  }
969  else
970  {
971  cs_out += (ch - '0');
972  }
973  if (cs_in != cs_out)
974  return (CSSL_ERROR_CHECKSUM);
975  ch = cssl_getchar(serial);
976  input += ch;
977 
978  return (i);
979 }
int i
Definition: rw_test.cpp:37
#define CSSL_ERROR_CHECKSUM
Definition: cosmos-errno.h:160
int32_t cssl_getchar(cssl_t *serial)
Definition: cssl_lib.cpp:709
char buf[128]
Definition: rw_test.cpp:40