COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
arduino_lib.h File Reference
#include "support/configCosmos.h"
#include <termios.h>
#include <sys/ioctl.h>
Include dependency graph for arduino_lib.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int arduino_init (char *port, int baud)
 
int arduino_printstring (char *str)
 
int arduino_printnum (int num)
 
int arduino_delay (int delay)
 
char * arduino_setport (char *port)
 
int arduino_setbaud (int baud)
 
char * arduino_read (char *str)
 
int arduino_closeport ()
 
int serialport_init (const char *serialport, int baud)
 
int serialport_writebyte (int fd, uint8_t b)
 
int serialport_write (int fd, const char *str)
 
int serialport_read_until (int fd, char *buf, char until)
 

Function Documentation

int arduino_init ( char *  port,
int  baud 
)
65  {
67  arduino_setbaud(baud);
68  if((fd = serialport_init(serialport, baudrate)) == -1)
69  return -1;
70  return fd;
71 }
static string port
Definition: add_radio.cpp:16
int fd
Definition: arduino_lib.cpp:61
int arduino_setbaud(int baud)
Definition: arduino_lib.cpp:93
char serialport[256]
Definition: arduino_lib.cpp:58
char * arduino_setport(char *port)
Definition: arduino_lib.cpp:89
int baudrate
Definition: arduino_lib.cpp:60
int serialport_init(const char *serialport, int baud)
Definition: arduino_lib.cpp:150
int arduino_printstring ( char *  str)
72  {
73  int32_t iretn;
74  strcpy(buf,str);
75  if((iretn = serialport_write(fd, buf)) == -1)
76  return -1;
77  return iretn;
78 }
char buf[256]
Definition: arduino_lib.cpp:62
int fd
Definition: arduino_lib.cpp:61
int iretn
Definition: rw_test.cpp:37
const string & str
Definition: json11.cpp:360
int serialport_write(int fd, const char *str)
Definition: arduino_lib.cpp:119
int arduino_printnum ( int  num)
79  {
80  int32_t iretn;
81  if((iretn = serialport_writebyte(fd, (uint8_t)num)) == -1)
82  return -1;
83  return iretn;
84 }
int fd
Definition: arduino_lib.cpp:61
int iretn
Definition: rw_test.cpp:37
int serialport_writebyte(int fd, uint8_t b)
Definition: arduino_lib.cpp:111
int arduino_delay ( int  delay)
85  {
86  COSMOS_USLEEP(delay * 1000);
87  return delay;
88 }
uint16_t delay
Definition: netperf_send.cpp:38
char* arduino_setport ( char *  port)
89  {
90  strcpy(serialport, port);
91  return serialport;
92 }
static string port
Definition: add_radio.cpp:16
char serialport[256]
Definition: arduino_lib.cpp:58
int arduino_setbaud ( int  baud)
93  {
94  baudrate = baud;
95  return baudrate;
96 }
int baudrate
Definition: arduino_lib.cpp:60
char* arduino_read ( char *  str)
97  {
99  //printf("read: %s\n",str);
100  COSMOS_SLEEP(1) ;
101  return str;
102 }
int fd
Definition: arduino_lib.cpp:61
int serialport_read_until(int fd, char *buf, char until)
Definition: arduino_lib.cpp:128
const string & str
Definition: json11.cpp:360
int arduino_closeport ( )
104  {
105  if(fd < 0)
106  return -1;
107  if(close(fd) < 0)
108  return -1;
109  return fd;
110 }
int fd
Definition: arduino_lib.cpp:61
int serialport_init ( const char *  serialport,
int  baud 
)
151 {
152  serial = cssl_open(serialport, baud, 8, 0, 1);
153  cssl_settimeout(serial, 0, 1.96);
155 
156 /*
157  struct termios toptions;
158  int fd;
159 
160  fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
161  if (fd == -1) {
162  perror("init_serialport: Unable to open port ");
163  return -1;
164  }
165 
166  if (tcgetattr(fd, &toptions) < 0) {
167  perror("init_serialport: Couldn't get term attributes");
168  return -1;
169  }
170  speed_t brate = baud; // let you override switch below if needed
171  switch(baud) {
172  case 4800: brate=B4800; break;
173  case 9600: brate=B9600; break;
174 #ifdef B14400
175  case 14400: brate=B14400; break;
176 #endif
177  case 19200: brate=B19200; break;
178 #ifdef B28800
179  case 28800: brate=B28800; break;
180 #endif
181  case 38400: brate=B38400; break;
182  case 57600: brate=B57600; break;
183  case 115200: brate=B115200; break;
184  }
185  cfsetispeed(&toptions, brate);
186  cfsetospeed(&toptions, brate);
187 
188  // 8N1
189  toptions.c_cflag &= ~PARENB;
190  toptions.c_cflag &= ~CSTOPB;
191  toptions.c_cflag &= ~CSIZE;
192  toptions.c_cflag |= CS8;
193  // no flow control
194  toptions.c_cflag &= ~CRTSCTS;
195 
196  toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
197  toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
198 
199  toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
200  toptions.c_oflag &= ~OPOST; // make raw
201 
202  // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
203  toptions.c_cc[VMIN] = 0;
204  toptions.c_cc[VTIME] = 20;
205 
206  tcflush (fd, TCIFLUSH) ;
207  if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
208  perror("init_serialport: Couldn't set term attributes");
209  return -1;
210  }
211  */
212 
213  return serial->fd;
214 }
int fd
Definition: cssl_lib.h:38
char serialport[256]
Definition: arduino_lib.cpp:58
int32_t cssl_setflowcontrol(cssl_t *serial, int rtscts, int xonxoff)
Definition: cssl_lib.cpp:389
cssl_t * cssl_open(const char *fname, int baud, int bits, int parity, int stop)
Definition: cssl_lib.cpp:108
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
int serialport_writebyte ( int  fd,
uint8_t  b 
)
112 {
113  int n = write(fd,&b,1);
114  if( n!=1)
115  return -1;
116  return 0;
117 }
int fd
Definition: arduino_lib.cpp:61
long b
Definition: jpegint.h:371
int serialport_write ( int  fd,
const char *  str 
)
120 {
121  int len = strlen(str);
122  int n = write(fd, str, len);
123  if( n!=len )
124  return -1;
125  return 0;
126 }
int fd
Definition: arduino_lib.cpp:61
const string & str
Definition: json11.cpp:360
int serialport_read_until ( int  fd,
char *  buf,
char  until 
)
129 {
130  char b[1];
131  int i=0;
132  do {
133  int n = read(fd, b, 1); // read a char at a time
134  if( n==-1) return -1; // couldn't read
135  if( n==0 ) {
136  COSMOS_USLEEP( 10 * 1000 ); // wait 10 msec try again
137  continue;
138  }
139  buf[i] = b[0]; i++;
140  } while( b[0] != until );
141 
142  buf[i] = 0; // null terminate the string
143  return 0;
144 }
char buf[256]
Definition: arduino_lib.cpp:62
int fd
Definition: arduino_lib.cpp:61
int i
Definition: rw_test.cpp:37
long b
Definition: jpegint.h:371