Window 95串列埠通訊函式集合(只適用於32位) (轉)
Window 95串列埠通訊集合(只適用於32位)
需要說明的是:這是的一部分,因此有一些與具體應用無關的部分。
關鍵是原理,而不是程式本身.後面有些使用介紹,幫助理解這長的程式。
頭(.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_H 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;
D 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 s (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(npGPSInfo));
LocalFree(npGPSInfo);
return (TRUE);
}
BOOL NEAR OpenConnection()
{
char szPort[15];
BOOL fRetVal;
HCURSOR hOldCursor,hWaitCursor;
HANDLE hCommWatchThread;
DWORD dwThreadID;
COMMTIMEOUTS CommTimeOuts;
if (!npGPSInfo) return (FALSE);
hWaitCursor=LoadCursor(NULL,IDC_WAIT) ;
hOldCursor=SetCursor(hWaitCursor) ;
wsprintf(szPort,"COM%d",PORT(npGPSInfo));
if
((COMDEV(npGPSInfo)=CreateFile(szPort,GENERIC_READ|GENERIC_WRITE,
0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
NULL))==(HANDLE)-1)
return ( FALSE ) ;
else
{ SetCommMask(COMDEV(npGPSInfo),EV_RXCHAR);
SetupComm(COMDEV(npGPSInfo),4096,4096);
PurgeComm(COMDEV(npGPSInfo),PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|
PURGE_R
XCLEAR);
CommTimeOuts.ReadIntervalTimeout=0xFFFFFFFF;
CommTimeOuts.ReadTotalTimeoutMultiplier=0;
CommTimeOuts.ReadTotalTimeoutConstant=1000;
CommTimeOuts.WriteTotalTimeoutMultiplier=0;
CommTimeOuts.WriteTotalTimeoutConstant=1000;
SetCommTimeouts(COMDEV(npGPSInfo),&CommTimeOuts);
}
fRetVal=SetupConnection();
if (fRetVal)
{ CONNECTED(npGPSInfo)=TRUE;
if
(NULL==(hCommWatchThread=CreateThread((LPSECURITY_ATTRIBUTES)NULL,
0,(LPTHREAD_START_ROUTINE)CommWatchProc,
(LPVOID)NULL,0,&dwThreadID)))
{ CONNECTED(npGPSInfo)=FALSE;
CloseHandle(COMDEV(npGPSInfo));
fRetVal=FALSE;
}
else
{ THREADID(npGPSInfo)=dwThreadID;
HTHREAD(npGPSInfo)=hCommWatchThread;
EscapeCommFunction(COMDEV(npGPSInfo),SETDTR);
}
}
else
{ CONNECTED(npGPSInfo)=FALSE;
CloseHandle(COMDEV(npGPSInfo));
}
SetCursor(hOldCursor);
return (fRetVal);
}
BOOL NEAR SetupConnection()
{ BOOL fRetVal;
DCB dcb;
if (!npGPSInfo) return(FALSE);
dcb.DCBlength=sizeof(DCB);
GetCommState(COMDEV(npGPSInfo),&dcb);
dcb.BaudRate=BAUDRATE(npGPSInfo);
dcb.ByteSize=BYTESIZE(npGPSInfo);
dcb.Parity=PARITY(npGPSInfo);
dcb.StopBits=STOPBITS(npGPSInfo);
dcb.fOutxDsrFlow=FALSE;
dcb.fDtrControl=DTR_CONTROL_ENABLE;
dcb.fOutxCtlow=FALSE;
dcb.fRtntrol=RTS_CONTROL_ENABLE;
dcb.fInX=dcb.fOutX=FALSE;
dcb.fBinary=TRUE;
dcb.fParity=TRUE;
fRetVal=SetCommState(COMDEV(npGPSInfo),&dcb);
return (fRetVal);
}
BOOL NEAR CloseConnection()
{
if (!npGPSInfo) return(FALSE);
CONNECTED(npGPSInfo)=FALSE;
SetCommMask(COMDEV(npGPSInfo),0);
while(THREADID(npGPSInfo)!=0);
EscapeCommFunction(COMDEV(npGPSInfo),CLRDTR);
PurgeComm(COMDEV(npGPSInfo),PURGE_TXABORT|PURGE_RXABORT|
PURGE_TXCLEAR|PURGE_RXCLEAR);
CloseHandle(COMDEV(npGPSInfo));
return (TRUE);
}
int NEAR ReadCommBlock(LPSTR lpszBlock,int nMaxLength)
{
BOOL fReadStat ;
COMSTAT ComStat ;
DWORD dwErrorFlags;
DWORD dwLength;
DWORD dwError;
if (!npGPSInfo) return(FALSE);
ClearCommError(COMDEV(npGPSInfo),&dwErrorFlags,&ComStat);
dwLength=min((DWORD)nMaxLength,ComStat.cbInQue);
if (dwLength>0)
{ fReadStat=ReadFile(COMDEV(npGPSInfo),lpszBlock,
dwLength,&dwLength,&READ_OS(npGPSInfo));
if (!fReadStat)
{ if (GetLastError()==ERROR_IO_PENDING)
{ OutputDeString("nrIO Pending");
while(!GetOverlappedResult(COMDEV(npGPSInfo),&READ_OS(npGPSInfo),&dwLe
ngth,TR
UE))
UE))
{ dwError=GetLastError();
if(dwError == ERROR_IO_INCOMPLETE)
continue;
}
}
else
{ dwLength=0;
ClearCommError(COMDEV(npGPSInfo),&dwErrorFlags,&ComStat);
}
}
}
return ( dwLength ) ;
}
BOOL NEAR WriteCommBlock(LPSTR lpByte,DWORD dwBytesToWrite)
{ BOOL fWriteStat;
DWORD dwBytesWritten;
DWORD dwErrorFlags;
DWORD dwError;
COMSTAT ComStat;
if (!npGPSInfo) return(FALSE);
fWriteStat=WriteFile(COMDEV(npGPSInfo),lpByte,dwBytesToWrite,
&dwBytesWritten,&WRITE_OS(npGPSInfo));
if (!fWriteStat)
{ if(GetLastError()==ERROR_IO_PENDING)
{ while(!GetOverlappedResult(COMDEV(npGPSInfo),
&WRITE_OS(npGPSInfo),&dwBytesWritten,TRUE))
{ dwError=GetLastError();
if(dwError == ERROR_IO_INCOMPLETE)
continue;
else
{
ClearCommError(COMDEV(npGPSInfo),&dwErrorFlags,&ComStat);
break;
}
}
}
else
{
{
ClearCommError(COMDEV(npGPSInfo),&dwErrorFlags,&ComStat);
return ( FALSE );
}
}
return ( TRUE ) ;
}
DWORD FAR PASCAL CommWatchProc(LPSTR)
{ DWORD dwEvtMask;
OVERLAPPED os;
int nLength;
BYTE abIn[1024];
memset(&os,0,sizeof(OVERLAPPED));
// create I/O event used for overlapped read
os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
if (os.hEvent==NULL)
{ MessageBox(NULL,"Failed to create event for thread!","GPS
Error!",MB_ICONEXCLAMATION|MB_OK);
return ( FALSE ) ;
}
if (!SetCommMask(COMDEV(npGPSInfo),EV_RXCHAR)) return (FALSE);
while (CONNECTED( npGPSInfo))
{ dwEvtMask=0 ;
WaitCommEvent(COMDEV(npGPSInfo),&dwEvtMask,NULL);
if ((dwEvtMask&EV_RXCHAR)==EV_RXCHAR)
{ do
{ if
(nLength=ReadCommBlock((LPSTR)abIn,1024))
{
//WriteCommBlock((LPSTR)abIn,nLength );
*(abIn+nLength)=0;
::SendMessage(hGPSWnd,CN_SEND,nLength,(LONG)(LPSTR)abIn);
}
}
while ((nLength>0)&&(CONNECTED( npGPSInfo)));
}
}
}
CloseHandle(os.hEvent);
THREADID(npGPSInfo)=0;
HTHREAD(npGPSInfo)=NULL;
return(TRUE);
}
就這些了,希望能對問這些問題的朋友有所幫助!
一般使用的順序是:
CreateGPSInfo(被通知的視窗控制程式碼,串列埠埠號1或2);
OpenConnection();//建立聯結它會SetupConnection
DestroyGPSInfo();//解除聯結它會呼叫CloseConnection
可以用ReadCommBlock/WriteCommBlock來讀/寫串列埠
CommWatchProc是監視串列埠的執行緒,由OpenConnection建立
當串列埠有資料來的時侯,它會通知'被通知的視窗控制程式碼'的視窗資料傳到的訊息(自
定義的)
SendMessage(hGPSWnd,CN_SEND,nLength,(LONG)(LPSTR)abIn);
好了,文章結束了!希望能幫助你!
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-987947/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 串列埠通訊 (轉)串列埠
- 用VB除錯串列埠通訊 (轉)除錯串列埠
- VC++ 串列埠通訊(轉)C++串列埠
- 串列埠通訊串列埠
- VC++ 的串列埠通訊 (轉)C++串列埠
- 串列埠通訊應用程式的解決方案 (轉)串列埠
- linux 串列埠通訊Linux串列埠
- 串列埠通訊協議串列埠協議
- Android 串列埠通訊Android串列埠
- C# 串列埠通訊C#串列埠
- 11. 串列埠通訊串列埠
- 串列埠通訊型別串列埠型別
- (10)uart串列埠通訊串列埠
- 通過串列埠進行通訊 :串列埠
- 串列埠資料抓取及串列埠通訊模擬串列埠
- 安卓串列埠通訊疑問安卓串列埠
- java串列埠通訊例項 -Java串列埠
- 串列埠無法正常通訊串列埠
- 基於WebSocket的modbus通訊(三)- websocket和串列埠Web串列埠
- 序列通訊的基本原理及用MFC實現串列埠通訊程式設計 (轉)串列埠程式設計
- Android USB 轉串列埠通訊開發基本流程Android串列埠
- Linux下串列埠通訊詳解(下)讀寫串列埠及關閉串列埠Linux串列埠
- AndroidSerialPort:安卓串列埠通訊庫Android安卓串列埠
- 串列埠通訊gui介面顯示串列埠GUI
- ROS環境下串列埠通訊ROS串列埠
- Android藍芽串列埠通訊Android藍芽串列埠
- 小型plc串列埠通訊簡介串列埠
- 啟用樹莓派3B串列埠通訊能力樹莓派串列埠
- STM32串列埠通訊串列埠
- C#串列埠通訊遇到的坑C#串列埠
- android ndk 虛擬串列埠通訊Android串列埠
- C#串列埠通訊程式SerialPort類C#串列埠
- 用C#一步步寫串列埠通訊C#串列埠
- .NET Compact Framework下的串列埠通訊Framework串列埠
- STMF4串列埠通訊使用串列埠
- 串列埠通訊與其他通訊方式相比有什麼優勢?串列埠
- 如何使用Java串列埠進行資料通訊及應用案例Java串列埠
- 【嵌入式系統】STM32串列埠通訊的四種方法(基於RTOS)串列埠