1、深入浅出 VC+串口编程之第三方类 深入浅出 VC+串口编程之第三方类 源代码下载串口类从本系列文章连载三、四可以看出,与通过 WIN32 API 进行串口访问相比,通过 MScomm 这个Activex 控件进行串口访问要来的方便许多,它基本上可以向用户屏蔽多线程的细节,以事件(发出OnComm 消息)方式实现串口的异步访问。尽管如此,MScomm 控件的使用仍有诸多不便,譬如其发送和接收数据都要进行 VARIANT 类型对象与字符串的转化等。因此,国内外许多优秀的程序员自己编写了一些串口类,使用这些类,我们将可以更方便的操作串口。在笔者的深入浅出 Win32 多线程程序设计之综合实例 (网
2、址:http:/)一文中,曾向读者展示了由 Remon Spekreijse 编写的 CSerialPort 串口类,而本文将向您展示由程序员 llbird 编写的 cnComm(中国串口?) 串口类。llbird 是一位优秀的程序员,他的代码风格简洁而紧凑,类的声明和实现都被定义在一个头文件中,使用这个类的朋友只需要在工程中包含这一头文件即可:/*Comm Base Library(WIN98/NT/2000) ver 1.1Compile by: BC+ 5; C+ BUILDER 4, 5, 6, X; VC+ 5, 6; VC.NET; GCC;copyright(c) 2004.5
3、- 2005.8 llbird */ #ifndef _CN_COMM_H_#define _CN_COMM_H_#pragma warning(disable: 4530)#pragma warning(disable: 4786)#pragma warning(disable: 4800)#include #include #include /送到窗口的消息 WPARAM 端口号#define ON_COM_RECEIVE WM_USER + 618#define ON_COM_CTS WM_USER + 619 /LPARAM 1 valid#define ON_COM_DSR WM_U
4、SER + 621 /LPARAM 1 valid#define ON_COM_RING WM_USER + 623#define ON_COM_RLSD WM_USER + 624#define ON_COM_BREAK WM_USER + 625#define ON_COM_TXEMPTY WM_USER + 626#define ON_COM_ERROR WM_USER + 627 /LPARAM save Error ID#define DEFAULT_COM_MASK_EVENT EV_RXCHAR | EV_ERR | EV_CTS | EV_DSR | EV_BREAK | EV
5、_TXEMPTY | EV_RING | EV_RLSDclass cnCommpublic:/-Construction-/ 第 1 个参数为是否在打开串口时启动监视线程, 第 2 个参数为 IO 方式 阻塞方式(0)/ 异步重叠方式(默认)cnComm(bool fAutoBeginThread = true, DWORD dwIOMode =FILE_FLAG_OVERLAPPED): _dwIOMode(dwIOMode), _fAutoBeginThread(fAutoBeginThread)Init();virtual cnComm()Close();UnInit();/-Attr
6、ibutes-/ 判断串口是否打开inline bool IsOpen()return _hCommHandle != INVALID_HANDLE_VALUE;/ 判断串口是否打开operator bool()return _hCommHandle != INVALID_HANDLE_VALUE;/ 获得串口句炳inline HANDLE GetHandle()return _hCommHandle;/ 获得串口句炳operator HANDLE()return _hCommHandle;/ 获得串口参数 DCBDCB *GetState()return IsOpen() / 设置串口参数
7、DCBbool SetState(DCB *pdcb = NULL)return IsOpen() ? :SetCommState(_hCommHandle, pdcb = NULL ? / 设置串口参数:波特率,停止位,等 支持设置字符串 “9600, 8, n, 1“bool SetState(char *szSetStr)if (IsOpen()if (:GetCommState(_hCommHandle, if (:BuildCommDCB(szSetStr, return :SetCommState(_hCommHandle, return false;/ 设置串口参数:波特率,停止
8、位,等bool SetState(DWORD dwBaudRate, DWORD dwByteSize = 8, DWORD dwParity =NOPARITY, DWORD dwStopBits = ONESTOPBIT)if (IsOpen()if (:GetCommState(_hCommHandle, _DCB.BaudRate = dwBaudRate;_DCB.ByteSize = (unsigned char)dwByteSize;_DCB.Parity = (unsigned char)dwParity;_DCB.StopBits = (unsigned char)dwSto
9、pBits;return :SetCommState(_hCommHandle, return false;/ 获得超时结构LPCOMMTIMEOUTS GetTimeouts(void)return IsOpen() / 设置超时bool SetTimeouts(LPCOMMTIMEOUTS lpCO)return IsOpen() ? :SetCommTimeouts(_hCommHandle, lpCO) = TRUE:false;/ 设置串口的 I/O 缓冲区大小bool SetBufferSize(DWORD dwInputSize, DWORD dwOutputSize)retur
10、n IsOpen() ? :SetupComm(_hCommHandle, dwInputSize, dwOutputSize)= TRUE: false;/ 关联消息的窗口句柄inline void SetWnd(HWND hWnd)assert(:IsWindow(hWnd);_hNotifyWnd = hWnd;/ 设定发送通知, 接受字符最小值inline void SetNotifyNum(DWORD dwNum)_dwNotifyNum = dwNum;/ 线程是否运行inline bool IsThreadRunning()return _hThreadHandle != NUL
11、L;/ 获得线程句柄inline HANDLE GetThread()return _hThreadHandle;/ 设置要监视的事件, 打开前设置有效void SetMaskEvent(DWORD dwEvent = DEFAULT_COM_MASK_EVENT)_dwMaskEvent = dwEvent;/ 获得读缓冲区的字符数int GetInputSize()COMSTAT Stat;DWORD dwError;return :ClearCommError(_hCommHandle, /-Operations-/ 打开串口 缺省 9600, 8, n, 1bool Open(DWOR
12、D dwPort)return Open(dwPort, 9600);/ 打开串口 缺省 baud_rate, 8, n, 1bool Open(DWORD dwPort, DWORD dwBaudRate)if (dwPort 1024)return false;BindCommPort(dwPort);if (!OpenCommPort()return false;if (!SetupPort()return false;return SetState(dwBaudRate);/ 打开串口, 使用类似“9600, 8, n, 1“的设置字符串设置串口bool Open(DWORD dwPo
13、rt, char *szSetStr)if (dwPort 1024)return false;BindCommPort(dwPort);if (!OpenCommPort()return false;if (!SetupPort()return false;return SetState(szSetStr);/ 读取串口 dwBufferLength 个字符到 Buffer 返回实际读到的字符数 可读任意数据DWORD Read(LPVOID Buffer, DWORD dwBufferLength, DWORD dwWaitTime = 10)if (!IsOpen()return 0;C
14、OMSTAT Stat;DWORD dwError;if (:ClearCommError(_hCommHandle, return 0; if (!Stat.cbInQue)/ 缓冲区无数据return 0;unsigned long uReadLength = 0;dwBufferLength = dwBufferLength Stat.cbInQue ? Stat.cbInQue :dwBufferLength;if (!:ReadFile(_hCommHandle, Buffer, dwBufferLength, / 结束异步 I/Oif (!:GetOverlappedResult(
15、_hCommHandle, elseuReadLength = 0;return uReadLength;/ 读取串口 dwBufferLength - 1 个字符到 szBuffer 返回 ANSI C 模式字符串指针 适合一般字符通讯char *ReadString(char *szBuffer, DWORD dwBufferLength, DWORD dwWaitTime =20)unsigned long uReadLength = Read(szBuffer, dwBufferLength - 1,dwWaitTime);szBufferuReadLength = 0;return
16、szBuffer;/ 写串口 可写任意数据 “abcd“ or “x0x1x2“DWORD Write(LPVOID Buffer, DWORD dwBufferLength)if (!IsOpen()return 0;DWORD dwError;if (:ClearCommError(_hCommHandle, unsigned long uWriteLength = 0;if (!:WriteFile(_hCommHandle, Buffer, dwBufferLength, return uWriteLength;/写串口 写 ANSI C 模式字符串指针 DWORD Write(con
17、st char *szBuffer)assert(szBuffer);return Write(void*)szBuffer, strlen(szBuffer);/读串口 同步应用DWORD ReadSync(LPVOID Buffer, DWORD dwBufferLength)if (!IsOpen()return 0;DWORD dwError;if (:ClearCommError(_hCommHandle, return 0;DWORD uReadLength = 0;:ReadFile(_hCommHandle, Buffer, dwBufferLength, return uRe
18、adLength;/写串口 同步应用DWORD WriteSync(LPVOID Buffer, DWORD dwBufferLength)if (!IsOpen()return 0;DWORD dwError;if (:ClearCommError(_hCommHandle, unsigned long uWriteLength = 0;:WriteFile(_hCommHandle, Buffer, dwBufferLength, return uWriteLength;/写串口 szBuffer 可以输出格式字符串 包含缓冲区长度DWORD Write(char *szBuffer, D
19、WORD dwBufferLength, char *szFormat, .)if (!IsOpen()return 0;va_list va;va_start(va, szFormat);_vsnprintf(szBuffer, dwBufferLength, szFormat, va);va_end(va);return Write(szBuffer);/写串口 szBuffer 可以输出格式字符串 不检查缓冲区长度 小心溢出DWORD Write(char *szBuffer, char *szFormat, .)if (!IsOpen()return 0;va_list va;va_s
20、tart(va, szFormat);vsprintf(szBuffer, szFormat, va);va_end(va);return Write(szBuffer);/关闭串口 同时也关闭关联线程virtual void Close()if (IsOpen()PurgeComm(_hCommHandle, PURGE_TXABORT | PURGE_TXCLEAR);EndThread();:CloseHandle(_hCommHandle);_hCommHandle = INVALID_HANDLE_VALUE;/DTR 电平控制bool SetDTR(bool OnOrOff)ret
21、urn IsOpen() ? EscapeCommFunction(_hCommHandle, OnOrOff ? SETDTR :CLRDTR): false;/RTS 电平控制bool SetRTS(bool OnOrOff)return IsOpen() ? EscapeCommFunction(_hCommHandle, OnOrOff ? SETRTS :CLRRTS): false;/bool SetBreak(bool OnOrOff)return IsOpen() ? EscapeCommFunction(_hCommHandle, OnOrOff ? SETBREAK: CL
22、RBREAK): false;/辅助线程控制 建监视线程bool BeginThread()if (!IsThreadRunning()_fRunFlag = true;_hThreadHandle = NULL;DWORD id;_hThreadHandle = :CreateThread(NULL, 0, CommThreadProc, this, 0,return (_hThreadHandle != NULL);return false;/暂停监视线程inline bool SuspendThread()return IsThreadRunning() ? :SuspendThread
23、(_hThreadHandle) !=0xFFFFFFFF: false;/恢复监视线程inline bool ResumeThread()return IsThreadRunning() ? :ResumeThread(_hThreadHandle) !=0xFFFFFFFF: false;/终止线程bool EndThread(DWORD dwWaitTime = 100)if (IsThreadRunning()_fRunFlag = false;:SetCommMask(_hCommHandle, 0);:SetEvent(_WaitOverlapped.hEvent);if (:Wa
24、itForSingleObject(_hThreadHandle, dwWaitTime) !=WAIT_OBJECT_0)if (!:TerminateThread(_hThreadHandle, 0)return false;:CloseHandle(_hThreadHandle);:ResetEvent(_WaitOverlapped.hEvent);_hThreadHandle = NULL;return true;return false;protected:volatile DWORD _dwPort; /串口号volatile HANDLE _hCommHandle; /串口句柄
25、char _szCommStr20; /保存 COM1 类似的字符串DCB _DCB; /波特率,停止位,等COMMTIMEOUTS _CO; /超时结构DWORD _dwIOMode; / 0 同步 默认 FILE_FLAG_OVERLAPPED 重叠 I/O 异步OVERLAPPED _ReadOverlapped, _WriteOverlapped; / 重叠 I/Ovolatile HANDLE _hThreadHandle; /辅助线程volatile HWND _hNotifyWnd; / 通知窗口volatile DWORD _dwNotifyNum; /接受多少字节(=_dwNotifyNum)发送通知消息volatile DWORD _dwMaskEvent; /监视的事件