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
00025
00026
00027 static HANDLE comport;
00028
00029
00030
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
00069 void gp3closeport()
00070 {
00071 CloseHandle(comport);
00072 }
00073
00074
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
00086
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
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 int gp3ready(void)
00109 {
00110 DWORD stat;
00111 Sleep(1);
00112 GetCommModemStatus(comport,&stat);
00113 return (stat & MS_CTS_ON)!=0;
00114 }
00115
00116
00117
00118
00119