serial.cc

00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <sys/types.h>
00004 #include <sys/stat.h>
00005 #include <fcntl.h>
00006 #include <termios.h>
00007 #include <string.h>
00008 #include "serial.h"
00009 
00010 #include <unistd.h>
00011 #include <errno.h>
00012 
00013 struct termios oldtio,newtio;
00014 
00015 int openPort( char *serialDev )
00016 {
00017   int fd;
00018 
00019   printf("Opening port %s\n", serialDev);
00020   fd = open(serialDev, O_RDWR | O_NOCTTY );
00021   if (fd <0) {
00022     perror(serialDev);
00023     return(1);
00024   }
00025 
00026   tcgetattr(fd,&oldtio); /* save current port settings */
00027 
00028   bzero(&newtio, sizeof(newtio));
00029   //newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
00030   newtio.c_cflag &= ~PARENB;
00031   newtio.c_cflag &= ~CSTOPB;
00032   newtio.c_cflag &= ~CSIZE;
00033   newtio.c_cflag |= BAUDRATE | CS8 | CLOCAL | CREAD;
00034 
00035   newtio.c_iflag = IGNPAR;
00036   newtio.c_oflag = 0;
00037 
00038   /* set input mode (non-canonical, no echo,...) */
00039   //newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //0;
00040   newtio.c_lflag = 0;
00041 
00042   newtio.c_cc[VTIME]    = 2;   /* inter-character timer unused .2 second */
00043   newtio.c_cc[VMIN]     = 0;   /* blocking read until 1 chars received or timeout */
00044 
00045   tcflush(fd, TCIFLUSH);
00046   tcsetattr(fd,TCSANOW,&newtio);
00047 
00048   return fd;
00049 }
00050 
00051 void closePort(int fd)
00052 {
00053   tcsetattr(fd,TCSANOW,&oldtio);
00054   close(fd);
00055 }
00056 
00057 
00058 void sendData(int fd, unsigned char *data, int len)
00059 {
00060    write(fd, data, len);
00061 }
00062 
00063 void getData(int fd, unsigned char *data, int *len)
00064 {
00065 
00066   char buf[255];
00067   int i,j;
00068 
00069   for (j=0; j<10; j++)
00070   {
00071     *len = read(fd,buf,255);
00072     if (*len > 0)
00073     {
00074       printf("Got: ");
00075       for (i=0; i<*len; i++)
00076         printf("%i ", buf[i]);
00077       printf("\n");
00078     }
00079   }
00080 
00081 }
00082 
Generated on Sun May 8 08:41:21 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3