需要说明的是:这是
程序的一部分,因此有一些与具体应用无关的部分。
关键是原理,而不是程序本身.后面有些使用介绍,帮助理解这长的
程序。
头文件(.H)
#include "StdAfx.h"
#define GWL_PGPSINFO 0
#define GPSEXTRABYTES sizeof( LONG )
#define MAXPORTS 4
#define CN_SEND WM_USER+100
#define RXQUEUE 4096
#define TXQUEUE 4096
// cursor states
#define CS_HIDE 0x00
#define CS_SHOW 0x01
// Flow control flags
#define FC_DTRDSR 0x01
#define FC_RTSCTS 0x02
#define FC_XONXOFF 0x04
// ascii definitions
#define ASCII_BEL 0x07
#define ASCII_BS 0x08
#define ASCII_LF 0x0A
#define ASCII_CR 0x0D
#define ASCII_XON 0x11
#define ASCII_XOFF 0x13
// data structures
typedef struct tagGPSINFO
{
HANDLE idComDev;
BYTE bPort;
BOOL fConnected;
BYTE bByteSize,bParity,bStopBits;
DWORD dwBaudRate;
HANDLE hPostEvent,hWatchThread,hWatchEvent;
HWND hTermWnd;
DWORD dwThreadID;
OVERLAPPED osWrite,osRead;
} GPSINFO, *PGPSINFO ;
#define COMDEV( x ) (x -> idComDev)
#define PORT( x ) (x -> bPort)
#define CONNECTED( x ) (x -> fConnected)
#define BYTESIZE( x ) (x -> bByteSize)
#define PARITY( x ) (x -> bParity)
#define STOPBITS( x ) (x -> bStopBits)
#define BAUDRATE( x ) (x -> dwBaudRate)
#define POSTEVENT( x ) (x -> hPostEvent)
#define HTHREAD( x ) (x -> hWatchThread)
#define THREADID( x ) (x -> dwThreadID)
#define WRITE_OS( x ) (x -> osWrite)
#define READ_OS( x ) (x -> osRead)
// function prototypes (private)
LRESULT NEAR CreateGPSInfo(HWND,BYTE nPort=1);
BOOL NEAR DestroyGPSInfo();
int NEAR ReadCommBlock(LPSTR,int);
BOOL NEAR WriteCommBlock(LPSTR,DWORD);
BOOL NEAR OpenConnection();
BOOL NEAR SetupConnection();
BOOL NEAR CloseConnection();
// function prototypes (public)
DWORD FAR PASCAL CommWatchProc(LPSTR);
具体实现请看下文(为了这文章,我都段线2次了)
CPP实现部分:
#include "StdAfx.h"
#include "Com.h"
HWND hGPSWnd=NULL;
PGPSINFO npGPSInfo=NULL;
LRESULT NEAR CreateGPSInfo(HWND hWnd,BYTE nPort)
{
if (NULL==(npGPSInfo=(PGPSINFO)LocalAlloc(LPTR,sizeof(GPSINFO))))
return ((LRESULT)-1) ;
hGPSWnd=hWnd;
COMDEV(npGPSInfo)=0;
CONNECTED(npGPSInfo)=FALSE;
PORT(npGPSInfo)=nPort;
BAUDRATE(npGPSInfo)=CBR_9600;
BYTESIZE(npGPSInfo)=8;
PARITY(npGPSInfo)=NOPARITY;
STOPBITS(npGPSInfo)=ONESTOPBIT;
WRITE_OS(npGPSInfo).Offset=0;
WRITE_OS(npGPSInfo).OffsetHigh=0;
READ_OS(npGPSInfo).Offset=0;
READ_OS(npGPSInfo).OffsetHigh=0;
// create I/O event used for overlapped reads / writes
READ_OS(npGPSInfo).hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
if (READ_OS(npGPSInfo).hEvent==NULL)
{ LocalFree( npGPSInfo ) ;
return ( -1 ) ;
}
WRITE_OS(npGPSInfo).hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
if (NULL==WRITE_OS(npGPSInfo).hEvent)
{ CloseHandle(READ_OS(npGPSInfo).hEvent);
LocalFree(npGPSInfo) ;
return (-1) ;
}
return ( (LRESULT) TRUE ) ;
}
BOOL NEAR DestroyGPSInfo()
{
if (!npGPSInfo) return (FALSE);
if (CONNECTED(npGPSInfo)) CloseConnection();
CloseHandle(READ_OS(npGPSInfo).hEvent);
CloseHandle(WRITE_OS(npGPSInfo).hEvent);
CloseHandle(POSTEVENT(n