Main Page   Modules   File List   Globals  

gp3cygwin.c

Go to the documentation of this file.
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 /* This is the part that is platform specific */
00029 /* This is for Cygwin */
00030 
00031 static int fd;  // file descriptor
00032 static struct termios oldtio; /* terminal info */
00033 
00034 
00035 /* Open port at 57600 baud */
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); /* save old settings */
00046   memset(&tio,0,sizeof(tio));
00047   // Set up for 8N1, no processing */
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); // make settings take  
00056   return 0;
00057 }
00058 
00059 /* Close the port when done */
00060 void gp3closeport()
00061 {
00062   tcsetattr(fd,TCSANOW,&oldtio);
00063   close(fd);
00064 }
00065 
00066 /* Write a byte to the GP3 */
00067 int gp3write(int byte)
00068 {
00069   char c=(char)byte;
00070   DEBUG("write",byte);
00071   return write(fd,&c,1);
00072 }
00073 
00074 /* Read a byte from the GP3 */
00075 /* User may call this after repeat */
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 /* Check CTS (optional if you don't want to know if GP3 is busy.
00087    As written, the system will handle this automatically although
00088    your program will block until the GP3 is available. This will
00089    allow you to prevent that if you implement it.
00090    None of the portable code uses this!
00091 
00092    However, if you don't implement this, you'll need to remove
00093    the lines that call gp3ready from the example program.
00094 */
00095 int gp3ready(void)
00096 {
00097   int s;
00098   usleep(100); // wait 100uS in case GP3 is just starting a command
00099   ioctl(fd,TIOCMGET,&s);
00100   return (s&TIOCM_CTS)==TIOCM_CTS;
00101 }
00102 
00103 
00104 
00105 
00106 

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