Main Page   Modules   File List   Globals  

gp3linux.c

Go to the documentation of this file.
00001 
00009 #include <stdio.h>
00010 #include <fcntl.h>
00011 #include "gp3lib.h"
00012 
00013 #include <asm/termios.h>
00014 /* Must operate at 57600 baud */
00015 #define BAUDRATE B57600
00016 #define DEBUGOUT 0
00017 #if DEBUGOUT
00018 #define DEBUG(tag,x) printf("DEBUG: %s=%02X\n",tag,x)
00019 #else
00020 #define DEBUG(tag,x)
00021 #endif
00022 
00023 /* This is the part that is platform specific */
00024 /* This is for Linux or Cygwin */
00025 
00026 static int fd;  // file descriptor
00027 static struct termios oldtio; /* terminal info */
00028 
00029 
00030 /* Open port at 57600 baud */
00031 int gp3openport(char *port)
00032 {
00033   struct termios tio;
00034   int i;
00035   fd=open(port,O_RDWR|O_NOCTTY);
00036   if (fd<0)
00037     {
00038       return -1;
00039     }
00040   tcgetattr(fd,&oldtio); /* save old settings */
00041   memset(&tio,0,sizeof(tio));
00042   // Set up for 8N1, no processing */
00043   tio.c_cflag=BAUDRATE|CS8|CLOCAL|CREAD|CRTSCTS; 
00044   tio.c_iflag=IGNPAR;
00045   tio.c_oflag=0;
00046   tio.c_lflag=0;
00047   for (i=0;i<NCCS;i++) tio.c_cc[i]=0;
00048   tio.c_cc[VMIN]=1;
00049   tcflush(fd,TCIFLUSH);
00050   tcsetattr(fd,TCSANOW,&tio); // make settings take  
00051   return 0;
00052 }
00053 
00054 /* Close the port when done */
00055 void gp3closeport()
00056 {
00057   tcsetattr(fd,TCSANOW,&oldtio);
00058   close(fd);
00059 }
00060 
00061 /* Write a byte to the GP3 */
00062 int gp3write(int byte)
00063 {
00064   char c=(char)byte;
00065   DEBUG("write",byte);
00066   return write(fd,&c,1);
00067 }
00068 
00069 /* Read a byte from the GP3 */
00070 /* User may call this after repeat */
00071 unsigned int gp3read(void)
00072 {
00073   char c;
00074   unsigned int rv;
00075   read(fd,&c,1);
00076   rv=((unsigned int)c) & 0xFF;;  
00077   DEBUG("read",rv);
00078   return rv;
00079 }
00080 
00081 /* Check CTS (optional if you don't want to know if GP3 is busy.
00082    As written, the system will handle this automatically although
00083    your program will block until the GP3 is available. This will
00084    allow you to prevent that if you implement it.
00085    None of the portable code uses this!
00086 
00087    However, if you don't implement this, you'll need to remove
00088    the lines that call gp3ready from the example program.
00089 
00090    Not that your Linux/Unix or tty driver may not implement TIOCMGET
00091    or may not return correct results for this call. 
00092 */
00093 int gp3ready(void)
00094 {
00095   int s;
00096   usleep(100); // wait 100uS in case GP3 is just starting a command
00097   ioctl(fd,TIOCMGET,&s);
00098   return (s&TIOCM_CTS)==TIOCM_CTS;
00099 }
00100 
00101 
00102 
00103 
00104 

Generated on Wed Feb 18 00:34:20 2004 for GP3LIB by doxygen1.2.18