00001
00009 #include <stdio.h>
00010 #include <fcntl.h>
00011 #include "gp3lib.h"
00012
00013 #include <termios.h>
00015 #define BAUDRATE B57600
00016
00021 #define DEBUGOUT 0
00022 #if DEBUGOUT
00023 #define DEBUG(tag,x) printf("DEBUG: %s=%02X\n",tag,x)
00024 #else
00025 #define DEBUG(tag,x)
00026 #endif
00027
00028
00029
00030
00031 static int fd;
00032 static struct termios oldtio;
00033
00034
00035
00036 int gp3openport(char *port)
00037 {
00038 struct termios tio;
00039 int i;
00040 fd=open(port,O_RDWR|O_NOCTTY);
00041 if (fd<0)
00042 {
00043 return -1;
00044 }
00045 tcgetattr(fd,&oldtio);
00046 memset(&tio,0,sizeof(tio));
00047
00048 tio.c_cflag=BAUDRATE|CS8|CLOCAL|CREAD|CRTSCTS;
00049 tio.c_iflag=IGNPAR;
00050 tio.c_oflag=0;
00051 tio.c_lflag=0;
00052 for (i=0;i<NCCS;i++) tio.c_cc[i]=0;
00053 tio.c_cc[VMIN]=1;
00054 tcflush(fd,TCIFLUSH);
00055 tcsetattr(fd,TCSANOW,&tio);
00056 return 0;
00057 }
00058
00059
00060 void gp3closeport()
00061 {
00062 tcsetattr(fd,TCSANOW,&oldtio);
00063 close(fd);
00064 }
00065
00066
00067 int gp3write(int byte)
00068 {
00069 char c=(char)byte;
00070 DEBUG("write",byte);
00071 return write(fd,&c,1);
00072 }
00073
00074
00075
00076 unsigned int gp3read(void)
00077 {
00078 char c;
00079 unsigned int rv;
00080 read(fd,&c,1);
00081 rv=((unsigned int)c) & 0xFF;;
00082 DEBUG("read",rv);
00083 return rv;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 int gp3ready(void)
00096 {
00097 int s;
00098 usleep(100);
00099 ioctl(fd,TIOCMGET,&s);
00100 return (s&TIOCM_CTS)==TIOCM_CTS;
00101 }
00102
00103
00104
00105
00106