00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <termios.h>
00028 #include <stdio.h>
00029 #include <fcntl.h>
00030 #include <sys/types.h>
00031 #include <sys/time.h>
00032 #include <string.h>
00033 #include <unistd.h>
00034 #include "linuxser.h"
00035
00036 #define DEBUG 0
00037
00038
00039
00040
00041 static int err;
00042
00043 static unsigned char peeked_char, peeked_char_avail = FALSE;
00044
00045
00046
00047
00048
00049 portstream_fd openserial(char *portname)
00050 {
00051 int pt_fd;
00052 struct termios info;
00053
00054 fprintf(stderr, "openserial: opening device %s\n", portname);
00055 if ((pt_fd=open(portname, O_RDWR|O_NONBLOCK)) < 0)
00056 {
00057 perror("Error opening serial port");
00058 return PORT_NOT_OPENED;
00059 }
00060
00061 if (tcgetattr(pt_fd, &info) < 0)
00062 {
00063 perror("Error using TCGETS in ioctl.");
00064 close(pt_fd);
00065 return PORT_NOT_OPENED;
00066 }
00067
00068
00069 info.c_iflag=1280;
00070 info.c_oflag=5;
00071 info.c_oflag=5;
00072 info.c_cflag=3261;
00073 info.c_lflag=35387;
00074
00075 if (tcsetattr(pt_fd, TCSANOW, &info) < 0)
00076 {
00077 perror("Error using TCSETS in ioctl.");
00078 close(pt_fd);
00079 return PORT_NOT_OPENED;
00080 }
00081 close(pt_fd);
00082
00083 if ((pt_fd=open(portname, O_RDWR)) < 0)
00084 {
00085 perror("Error opening serial port");
00086 return PORT_NOT_OPENED;
00087 }
00088
00089 if (tcgetattr(pt_fd, &info) < 0)
00090 {
00091 perror("Error using TCGETS in ioctl");
00092 close(pt_fd);
00093 return PORT_NOT_OPENED;
00094 }
00095
00096 info.c_iflag = IGNBRK | IGNPAR;
00097 info.c_iflag &= ~(INLCR | ICRNL | IUCLC | ISTRIP | IXON | BRKINT);
00098 info.c_oflag &= ~OPOST;
00099 info.c_lflag &= ~(ICANON | ISIG | ECHO);
00100 info.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
00101
00102 if (tcsetattr(pt_fd, TCSANOW, &info) < 0)
00103 {
00104 perror("Error using TCSETS in ioctl");
00105 close(pt_fd);
00106 return PORT_NOT_OPENED;
00107 }
00108
00109 return pt_fd;
00110 }
00111
00112 char closeserial(portstream_fd pt_fd)
00113 {
00114 if (pt_fd != PORT_NOT_OPENED) {
00115 close(pt_fd);
00116 }
00117 return TRUE;
00118 }
00119
00120
00121 char SerialBytesOut(portstream_fd pt_fd, unsigned char *buffer,
00122 int nBytes)
00123 {
00124 register int nout;
00125 nout = write(pt_fd, buffer, nBytes);
00126
00127 if (nout == nBytes)
00128 return TRUE;
00129 else
00130 return FALSE;
00131 }
00132
00133
00134 char SerialBytesIn (portstream_fd pt_fd, unsigned char *buffer,
00135 unsigned int nBytes, long timeoutVal)
00136 {
00137 fd_set readfds;
00138 register int i,noconn=0,n,bytesrec=0;
00139 char inchars[256],*vptr;
00140 struct timeval timeout={((timeoutVal > 0) ? timeoutVal : 10),0};
00141
00142 if (pt_fd == PORT_NOT_OPENED)
00143 return FALSE;
00144
00145 vptr=(char *) buffer;
00146
00147 FD_ZERO(&readfds); FD_SET(pt_fd,&readfds);
00148
00149
00150 for (i=0; (i<nBytes) && (noconn==0); i++) {
00151 if (select(FD_SETSIZE,&readfds,(fd_set *)NULL,
00152 (fd_set *)NULL,&timeout)>0) {
00153 n=read(pt_fd,inchars,1);
00154 *vptr++ = inchars[0];
00155 bytesrec++;
00156 } else {
00157 noconn=1;
00158 fprintf(stderr,"SerialBytesIn: timeout during data receive\n");
00159 }
00160 }
00161 #if DEBUG
00162 if (bytesrec == 1)
00163 fprintf(stderr,"SerialBytesIn: received <%u>\n", *buffer);
00164 else
00165 fprintf(stderr,"SerialBytesIn: received <%s> %d bytes - req. %d bytes\n", buffer, bytesrec, nBytes);
00166 #endif
00167 if (bytesrec == nBytes) {
00168 return TRUE;
00169 } else {
00170 return FALSE;
00171 }
00172 }
00173
00174
00175 char PeekByte(portstream_fd portstream, unsigned char *peekedByte)
00176 {
00177 return FALSE;
00178 }
00179
00180
00181 char FlushInputBuffer(portstream_fd pt_fd) {
00182 return TRUE;
00183 }
00184
00185
00186
00187 char SerialStringOut(portstream_fd pt_fd, unsigned char *buffer)
00188 {
00189 char s[256];
00190 register int len,nout;
00191
00192 sprintf(s,"%s",buffer);
00193
00194
00195
00196 len=strlen(s);
00197
00198 nout = write(pt_fd,s,len);
00199 nout+= write(pt_fd," ",1);
00200
00201 return TRUE;
00202 }
00203
00204
00205
00206 char ReadSerialLine(portstream_fd pt_fd, unsigned char *strbuffer,
00207 long timeoutVal, int *charsRead)
00208 {
00209 char out[2],*c,*sb;
00210 fd_set readfds;
00211 int n;
00212 unsigned char done=FALSE;
00213 struct timeval timeout={((timeoutVal > 0) ? timeoutVal : 10),0};
00214
00215
00216 sprintf((char*)strbuffer,"");
00217
00218 FD_ZERO(&readfds); FD_SET(pt_fd,&readfds);
00219 c=out;
00220 sb=(char *)strbuffer;
00221
00222 while (!done && select(FD_SETSIZE,&readfds,(fd_set *)NULL,
00223 (fd_set *)NULL,&timeout)>0) {
00224 n=read(pt_fd,c,1);
00225 if (*c == '\n') {
00226 *sb = '\0';
00227 done=TRUE;
00228 } else if (*c == '\r') {
00229
00230 } else {
00231
00232 *sb++ = *c;
00233 }
00234 *charsRead += n;
00235 }
00236
00237 if (!done) {
00238 return(TIMEOUT_CHAR_READ);
00239 }
00240
00241 if (strbuffer[0] == '!' || strbuffer[1] == '!') {
00242
00243
00244
00245
00246 fprintf(stderr, "ReadSerialLine: Error <%s>\n", strbuffer);
00247 return(FALSE);
00248 } else {
00249 #if DEBUG
00250 fprintf(stderr, "ReadSerialLine: got string <%s>\n", strbuffer);
00251 #endif
00252 return(TRUE);
00253 }
00254 }
00255
00256 void do_delay(long millisec)
00257 {
00258 struct timeval tv;
00259 double elapsedTime, startTime;
00260 double delayTime = millisec/1000.0;
00261
00262 gettimeofday(&tv, NULL);
00263 startTime = tv.tv_sec + tv.tv_usec/1000000.0;
00264 do {
00265 gettimeofday(&tv, NULL);
00266 elapsedTime = (tv.tv_sec + tv.tv_usec/1000000.0) - startTime;
00267 } while (elapsedTime < delayTime);
00268 }
00269
00270
00271
00272
00273
00274
00275
00276 #define INT_REVERSED
00277
00278
00279
00280 char GetSignedShort(portstream_fd portstream, signed short *SHORTval, long
00281 timeout) {
00282 #ifdef INT_REVERSED
00283 SerialBytesIn( portstream, (((unsigned char *) SHORTval)+1), 1,
00284 timeout);
00285 SerialBytesIn( portstream, ( (unsigned char *) SHORTval), 1, timeout);
00286 #if DEBUG
00287 fprintf(stderr,"GetSignedShort: received <%d>\n", *SHORTval);
00288 #endif
00289 #else
00290 SerialBytesIn( portstream, ( (unsigned char *) SHORTval), 1, timeout);
00291 SerialBytesIn( portstream, (((unsigned char *) SHORTval)++), 1,
00292 timeout);
00293 #endif
00294 return TRUE;
00295 }
00296
00297
00298 char PutSignedShort(portstream_fd portstream, signed short *SHORTval) {
00299 #ifdef INT_REVERSED
00300 SerialBytesOut( portstream, (((unsigned char *) SHORTval)+1), 1);
00301 SerialBytesOut( portstream, ( (unsigned char *) SHORTval), 1);
00302 #else
00303 SerialBytesOut( portstream, ( (unsigned char *) SHORTval), 1);
00304 SerialBytesOut( portstream, (((unsigned char *) SHORTval)++), 1);
00305 #endif
00306 return TRUE;
00307 }
00308
00309
00310 char GetUnsignedShort(portstream_fd portstream, unsigned short
00311 *USHORTval, long timeout) {
00312 #ifdef INT_REVERSED
00313 SerialBytesIn( portstream, (((unsigned char *) USHORTval)+1), 1,
00314 timeout);
00315 SerialBytesIn( portstream, ( (unsigned char *) USHORTval), 1,
00316 timeout);
00317 #if DEBUG
00318 fprintf(stderr,"GetUnsignedShort: received <%d>\n", *USHORTval);
00319 #endif
00320 #else
00321 SerialBytesIn( portstream, ( (unsigned char *) USHORTval), 1, timeout);
00322 SerialBytesIn( portstream, (((unsigned char *) USHORTval)++), 1,
00323 timeout);
00324 #endif
00325 return TRUE;
00326 }
00327
00328
00329 char PutUnsignedShort(portstream_fd portstream, unsigned short *USHORTval) {
00330
00331 #ifdef INT_REVERSED
00332 SerialBytesOut( portstream, (((unsigned char *) USHORTval)+1), 1);
00333 SerialBytesOut( portstream, ( (unsigned char *) USHORTval), 1);
00334 #else
00335 SerialBytesOut( portstream, ( (unsigned char *) USHORTval), 1);
00336 SerialBytesOut( portstream, (((unsigned char *) USHORTval)++), 1);
00337 #endif
00338 return TRUE;
00339 }
00340
00341
00342 char GetSignedLong(portstream_fd portstream, signed long *LONGval, long
00343 timeout) {
00344 long i, incr = 1;
00345
00346 #ifdef INT_REVERSED
00347 LONGval = (signed long *) (((unsigned char *) LONGval) + 3);
00348 incr = -1;
00349 #endif
00350 for (i=0; i<4; i++) {
00351 SerialBytesIn( portstream, ((unsigned char *) LONGval), 1,
00352 timeout);
00353 LONGval = (signed long *) (((unsigned char *) LONGval) + incr);
00354 }
00355 return TRUE;
00356 }
00357
00358
00359 char PutSignedLong(portstream_fd portstream, signed long *LONGval) {
00360
00361 #ifdef INT_REVERSED
00362 SerialBytesOut( portstream, ((unsigned char *) LONGval)+3, 1);
00363 SerialBytesOut( portstream, ((unsigned char *) LONGval)+2, 1);
00364 SerialBytesOut( portstream, ((unsigned char *) LONGval)+1, 1);
00365 SerialBytesOut( portstream, ((unsigned char *) LONGval), 1);
00366 #else
00367 SerialBytesOut( portstream, ((unsigned char *) LONGval),4);
00368 #endif
00369 return TRUE;
00370 }