HANDLE CreateFile ( LPCTSTR lpFileName, //将要打开的串口逻辑名,如COM1 或COM2 DWORD dwAccess, //指定串口访问的类型,可以是读取、写入或两者并列 DWORD dwShareMode, //指定共享属性,由于串口不能共享,该参数必须置为0 LPSECURITY_ATTRIBUTES lpsa, //引用安全性属性结构,缺省值为NULL DWORD dwCreate, //创建标志,对串口操作该参数必须置为OPEN EXISTING DWORD dwAttrsAndFlags, //属性描述,用于指定该串口是否可进行异步操作, //FILE_FLAG_OVERLAPPED:可使用异步的I/O HANDLE hTemplateFile //指向模板文件的句柄,对串口而言该参数必须置为NULL ); |
HANDLE hCom; DWORD dwError; hCon = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hCom == (HANDLE)0xFFFFFFFF) { dwError = GetLastError(); MessageBox(dwError); } |
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; 而SetupComm函数的原型则为: BOOL SetupComm( HANDLE hFile, // handle to communications device DWORD dwInQueue, // size of input buffer DWORD dwOutQueue // size of output buffer ); |
DCB dcb; dcb.BaudRate = 9600; //波特率为9600 dcb.ByteSize = 7; //数据位数为7位 dcb.Parity = EVENPARITY; //偶校验 dcb.StopBits = 2; //两个停止位 dcb.fBinary = TRUE; dcb.fParity = TRUE; if (!SetCommState(hCom, &dcb)) { MessageBox("串口设置出错!"); } SetupComm(hCom, 1024, 1024); PurgeComm(hCom, PURCE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); |
typedef struct _COMMTIMEOUTS { DWORD ReadIntervalTimeout; //定义两个字符到达的最大时间间隔,单位:毫秒 //当读取完一个字符后,超过了ReadIntervalTimeout,仍未读取到下一个字符,就会 //发生超时 DWORD ReadTotalTimeoutMultiplier; DWORD ReadTotalTimeoutConstant; //其中各时间所满足的关系如下: //ReadTotalTimeout = ReadTotalTimeOutMultiplier* BytesToRead + ReadTotalTimeoutConstant DWORD WriteTotalTimeoutMultiplier; DWORD WriteTotalTimeoutConstant; } COMMTIMEOUTS, *LPCOMMTIMEOUTS; |
BOOL SetCommTimeouts( HANDLE hFile, // handle to communications device LPCOMMTIMEOUTS lpCommTimeouts // pointer to comm time-out structure ); |
COMMTIMEOUTS to; memset(&to, 0, sizeof(to)); to.ReadIntervalTimeout = 10; SetCommTimeouts(hCom, &to); |
BOOL GetCommTimeouts( HANDLE hFile, // handle of communications device LPCOMMTIMEOUTS lpCommTimeouts // pointer to comm time-out structure ); |
BOOL SetCommMask( HANDLE hFile, //标识通信端口的句柄 DWORD dwEvtMask //能够使能的通信事件 ); |
BOOL GetCommMask( HANDLE hFile, //标识通信端口的句柄 LPDWORD lpEvtMask // address of variable to get event mask ); |
BOOL WaitCommEvent( HANDLE hFile, //标识通信端口的句柄 LPDWORD lpEvtMask, // address of variable for event that occurred LPOVERLAPPED lpOverlapped, // address of overlapped structure ); |
BOOL ReadFile( HANDLE hFile, // handle of file to read LPVOID lpBuffer, // pointer to buffer that receives data DWORD nNumberOfBytesToRead, // number of bytes to read LPDWORD lpNumberOfBytesRead, // pointer to number of bytes read LPOVERLAPPED lpOverlapped // pointer to structure for overlapped I/O ); |
BOOL WriteFile( HANDLE hFile, // handle to file to write to LPCVOID lpBuffer, // pointer to data to write to file DWORD nNumberOfBytesToWrite, // number of bytes to write LPDWORD lpNumberOfBytesWritten, // pointer to number of bytes written LPOVERLAPPED lpOverlapped // pointer to structure for overlapped I/O ); |
BOOL CloseHandle( HANDLE hObject // handle to object to close ); |
|