Main Page   Modules   File List   Globals  

gp3win.c

Go to the documentation of this file.
00001 
00009 #include <windows.h>
00010 #include <stdio.h>
00011 #include "gp3lib.h"
00012 
00017 #define DEBUGOUT 0
00018 #if DEBUGOUT
00019 #define DEBUG(tag,x) printf("DEBUG: %s=%02X\n",tag,x)
00020 #else
00021 #define DEBUG(tag,x)
00022 #endif
00023 
00024 /* This is the part that is platform specific */
00025 /* This is for Windows */
00026 
00027 static HANDLE comport;  // file descriptor
00028 
00029 
00030 /* Open port at 57600 baud */
00031 int gp3openport(char *port)
00032 {
00033         DCB dcb;
00034         COMMTIMEOUTS cto;
00035         DWORD rv;
00036         comport=CreateFile(port,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
00037         if (comport) 
00038         {
00039            SetupComm(comport,128,128);
00040            cto.ReadIntervalTimeout=0;
00041            cto.ReadTotalTimeoutMultiplier=100;
00042            cto.ReadTotalTimeoutConstant=0;
00043            cto.WriteTotalTimeoutMultiplier=0;
00044            cto.WriteTotalTimeoutConstant=100;
00045            SetCommTimeouts(comport,&cto);
00046            dcb.DCBlength=sizeof(dcb);
00047            GetCommState(comport,&dcb);
00048            rv=BuildCommDCB("baud=57600 parity=N data=8 stop=1",&dcb);
00049            dcb.fBinary=TRUE;
00050            dcb.fDtrControl=DTR_CONTROL_DISABLE;
00051            dcb.fOutxDsrFlow=FALSE;
00052            dcb.fNull=FALSE;
00053            dcb.fRtsControl=RTS_CONTROL_ENABLE;
00054            dcb.fOutxCtsFlow=TRUE;
00055            dcb.fDsrSensitivity=FALSE;
00056            dcb.fOutX=FALSE;
00057            dcb.fInX=FALSE;
00058            rv=SetCommState(comport,&dcb);
00059            if (!rv) 
00060            {
00061                    CloseHandle(comport);
00062                    comport=NULL;
00063            }
00064         }
00065         return comport!=NULL?0:-1;
00066 }
00067 
00068 /* Close the port when done */
00069 void gp3closeport()
00070 {
00071   CloseHandle(comport);
00072 }
00073 
00074 /* Write a byte to the GP3 */
00075 int gp3write(int byte)
00076 {
00077   char c=(char)byte;
00078   DWORD n=0;
00079   while (!gp3ready());
00080   DEBUG("write",byte);
00081   WriteFile(comport,&c,1,&n,NULL);
00082   return n==1;
00083 }
00084 
00085 /* Read a byte from the GP3 */
00086 /* User may call this after repeat */
00087 unsigned int gp3read(void)
00088 {
00089   char c;
00090   DWORD n;
00091   unsigned int rv;
00092   ReadFile(comport,&c,1,&n,NULL);
00093   rv=((unsigned int)c) & 0xFF;;  
00094   DEBUG("read",rv);
00095   return (n==1)?rv:-1;
00096 }
00097 
00098 /* Check CTS (optional if you don't want to know if GP3 is busy.
00099    As written, the system will handle this automatically although
00100    your program will block until the GP3 is available. This will
00101    allow you to prevent that if you implement it.
00102    None of the portable code uses this!
00103 
00104    However, if you don't implement this, you'll need to remove
00105    the lines that call gp3ready from the example program.
00106 
00107 */
00108 int gp3ready(void)
00109 {
00110   DWORD stat;
00111   Sleep(1); // wait 1ms in case GP3 is just starting a command
00112   GetCommModemStatus(comport,&stat);
00113   return (stat & MS_CTS_ON)!=0;
00114 }
00115 
00116 
00117 
00118 
00119 

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