以下的东西是我在阅读 "Communications Programming for Windows 95" 时做的一点笔记, 所以在 topic 上大致上都和这书上的第三章一样. 一些 structure 和 API 的宣告都是 copy from VC 5.0 的 on-line help.
这些东西主要谈的是如何在 Win32 的平台下对 serial port 的通讯. 可能可以带来帮助的是那些了解 serial port 的通讯, 但是不清楚在 Win32 平台究竟有那些 API 可用的 programer (就像我? :p) 我只是做做整理罢了, 更清楚的内容可能要再翻翻 on-line help 或是书本. 如果你要尝试在 Win32 下做像是对 modem 做控制(例如拨号, 通讯)的程序, 但是对于 serial port 并不了解, 那你应该再去找一本有讲到这些东西的书看看.
1.开启一个 Serial Port
利用一般开启档案的 CreatFile() 即可开启 serial port device
HANDLE CreateFile( LPCTSTR lpFileName, // pointer to name of the file DWORD dwDesiredAccess, // access (read-write) mode DWORD dwShareMode, // share mode LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes DWORD dwCreationDistribution, // how to create DWORD dwFlagsAndAttributes, // file attributes HANDLE hTemplateFile // handle to file with attributes to copy ); |
用 CreateFile() API.
lpFileName 为 "COM1" 或是 "COM2"
dwDersiredAccess 一般为 GENERIC_READ|GENERIC_WRITE
dwShareMode "必须"为 0, 即不能共享, 但同一个 process 中的不同 thread 在一开启之后就可以共享.
lpSecurityAttributes 一般为 NULL
dwCreateionDistributon 在这里"必须"为 OPEN_EXISTING
dwFlagsAndAttributes 定义了开启的属性, 若是设成 FILE_FLAG_OVERLAPPED 则可使用异步的 I/O.
hTemplateFile "必须"为 NULL
传回档案 handle
设定 Serial Port 传送及接收缓冲区的大小
在开启完 serial port 之后, 可以藉由呼叫 SetupComm() 来进行配置传送时的缓冲区及接收时的缓冲区. 如果没有呼叫 SetupComm() 的话, Win95 会配置内定的缓冲区.
BOOL SetupComm( HANDLE hFile, // handle of communications device DWORD dwInQueue, // size of input buffer DWORD dwOutQueue // size of output buffer ); |
2.关闭 Serial Port file
利用一般的 CloseHandle() 即可.
BOOL CloseHandle( HANDLE hObject // handle to object to close ) |
3.取得 Seial Port 的信息
在 Win32 里头, 将一些通讯时会用到的信息用 COMMPROP 这个结构来表示. (当然不仅仅是 Serial Port) 可以用 GetCommProperties() 来取得:
BOOL GetCommProperties( HANDLE hFile, // handle of communications device LPCOMMPROP lpCommProp // address of communications properties structure ); |
COMMPROP 长的像这个样子:
typedef struct _COMMPROP { // cmmp WORD wPacketLength; // packet size, in bytes WORD wPacketVersion; // packet version DWORD dwServiceMask; // services implemented DWORD dwReserved1; // reserved DWORD dwMaxTxQueue; // max Tx bufsize, in bytes DWORD dwMaxRxQueue; // max Rx bufsize, in bytes DWORD dwMaxBaud; // max baud rate, in bps DWORD dwProvSubType; // specific provider type DWORD dwProvCapabilities; // capabilities supported DWORD dwSettableParams; // changable parameters DWORD dwSettableBaud; // allowable baud rates WORD wSettableData; // allowable byte sizes WORD wSettableStopParity; // stop bits/parity allowed DWORD dwCurrentTxQueue; // Tx buffer size, in bytes DWORD dwCurrentRxQueue; // Rx buffer size, in bytes DWORD dwProvSpec1; // provider-specific data DWORD dwProvSpec2; // provider-specific data WCHAR wcProvChar[1]; // provider-specific data } COMMPROP; |
在这里, lpCommProp 需要 programmer 自行配置空间. 有趣的问题是, 系统在这个结构之后会需要额外的空间. 但是配置者也就是 programmer 却不知道系统会需要多少. 很简单的做法是配置一大块绝对会够的空间. 另一个聪明的做法是执行两次 GetCommProperties() , 第一次只配置 sizeof(COMMPROP) 这么大的空间, 因为还没有开始执行一些动作, 所以系统并不会尝试着在后面填东西, 所以不会出问题. 接着执行第一次的 GetCommProperties(), 得到结果, 取出结构中的 wPacketLength, 这个 member 代表实际上需要的大小, 然后依据这个大小重新配置一个新的. 这样的话 , 就不会有浪费任何空间的问题了.
至于上述 COMMPROP 结构的成员所代表的意思, on-line help 中应该写的都满清楚的 .