- BOOL InitPort(HWND pPortOwner, // the owner (CWnd) of the port (receives message)
- UINT portnr, // portnumber (1..4)
- UINT baud, // baudrate
- char parity, // parity
- UINT databits, // databits
- UINT stopbits, // stopbits
- DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc
- UINT writebuffersize) // size to the writebuffer
- {
- HANDLE m_hComm ;
- char *szPort = new char[50];
- char *szBaud = new char[50];
- sprintf(szPort, "COM%d", portnr);
- sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits);
- // get a handle to the port
- m_hComm = CreateFileW(szPort, // communication port string (COMX)
- GENERIC_READ | GENERIC_WRITE, // read/write types
- 0, // comm devices must be opened with exclusive access
- NULL, // no security attributes
- OPEN_EXISTING, // comm devices must use OPEN_EXISTING
- 0, // Async I/O
- NULL);
- }
上面的代码在VS2005下编译没有错误,CreateFileW要改为CreateFileA。
移到WINCE下就出了小错误。
环境:EVC4.0
编译错误:
error C2664: 'CreateFileW' : cannot convert parameter 1 from 'char *' to 'const unsigned short *'
解决办法:强制转换
m_hComm = CreateFileW((LPCWSTR)szPort,