
| HADLE CreateFile(PCTSTR lpFileName, //通信端口名,如"COM1" WORD dwDesiredAccess, //对资源的访问类型 WORD dwShareMode, //指定共享模式,COM不能共享,该参数为0 PSECURITY_ATTRIBUTES lpSecurityAttributes, //安全描述符指针,可为NULL WORD dwCreationDisposition, //创建方式 WORD dwFlagsAndAttributes, //文件属性,可为NULL HANDLE hTemplateFile //模板文件句柄,置为NULL ); |
| BOOL WINAPI GetCommState( HANDLE hFile, //标识通信端口的句柄 LPDCB lpDCB //指向一个设备控制块(DCB结构)的指针 ); |
| BOOL SetCommState( HANDLE hFile, //标识通信端口的句柄 LPDCB lpDCB //指向一个设备控制块(DCB结构)的指针 ); |
| typedef struct _DCB { // dcb DWORD DCBlength; // sizeof(DCB) DWORD BaudRate; // current baud rate DWORD fBinary: 1; // binary mode, no EOF check DWORD fParity: 1; // enable parity checking DWORD fOutxCtsFlow: 1; // CTS output flow control DWORD fOutxDsrFlow: 1; // DSR output flow control DWORD fDtrControl: 2; // DTR flow control type DWORD fDsrSensitivity: 1; // DSR sensitivity DWORD fTXContinueOnXoff: 1; // XOFF continues Tx DWORD fOutX: 1; // XON/XOFF out flow control DWORD fInX: 1; // XON/XOFF in flow control DWORD fErrorChar: 1; // enable error replacement DWORD fNull: 1; // enable null stripping DWORD fRtsControl: 2; // RTS flow control DWORD fAbortOnError: 1; // abort reads/writes on error DWORD fDummy2: 17; // reserved WORD wReserved; // not currently used WORD XonLim; // transmit XON threshold WORD XoffLim; // transmit XOFF threshold BYTE ByteSize; // number of bits/byte, 4-8 BYTE Parity; // 0-4=no,odd,even,mark,space BYTE StopBits; // 0,1,2 = 1, 1.5, 2 char XonChar; // Tx and Rx XON character char XoffChar; // Tx and Rx XOFF character char ErrorChar; // error replacement character char EofChar; // end of input character char EvtChar; // received event character WORD wReserved1; // reserved; do not use } DCB; |
| BOOL SetCommMask( HANDLE hFile, //标识通信端口的句柄 DWORD dwEvtMask //能够使能的通信事件 ); |
| 值 | 事件描述 |
| EV_BREAK | A break was detected on input. |
| EV_CTS | The CTS (clear-to-send) signal changed state. |
| EV_DSR | The DSR(data-set-ready) signal changed state. |
| EV_ERR | A line-status error occurred. Line-status errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY. |
| EV_RING | A ring indicator was detected. |
| EV_RLSD | The RLSD (receive-line-signal-detect) signal changed state. |
| EV_RXCHAR | A character was received and placed in the input buffer. |
| EV_RXFLAG | The event character was received and placed in the input buffer. The event character is specified in the device's DCB structure, which is applied to a serial port by using the SetCommState function. |
| EV_TXEMPTY | The last character in the output buffer was sent. |
| BOOL WaitCommEvent( HANDLE hFile, //标识通信端口的句柄 LPDWORD lpEvtMask, //指向存放事件标识变量的指针 LPOVERLAPPED lpOverlapped, // 指向overlapped结构 ); |
| BOOL ReadFile(HANDLE hFile, //标识通信端口的句柄 LPVOID lpBuffer, //输入数据Buffer指针 DWORD nNumberOfBytesToRead, // 需要读取的字节数 LPDWORD lpNumberOfBytesRead, //实际读取的字节数指针 LPOVERLAPPED lpOverlapped //指向overlapped结构 ); BOOL WriteFile(HANDLE hFile, //标识通信端口的句柄 LPCVOID lpBuffer, //输出数据Buffer指针 DWORD nNumberOfBytesToWrite, //需要写的字节数 LPDWORD lpNumberOfBytesWritten, //实际写入的字节数指针 LPOVERLAPPED lpOverlapped //指向overlapped结构 ); |