现在大多数WinCE设备都带有USB Device功能,Device也即设备,在PC看来,WinCE就是作为一个设备,可以表现为U盘,也可表现为网络设备(RNDIS),也可以做为串口(ActiveSync同步使用),也可作为modem。
1. 选择USB Client的组件。
在Catalog Items View中选择”Device Drivers”->”USB Function”->”USB Function Clients”->”RNDIS Clients”,如图:
2. 注册表设置
[HKEY_LOCAL_MACHINE\Drivers\USB\FunctionDrivers] "DefaultClientDriver"="Serial_class" ;"DefaultClientDriver"="RNDIS" ;"DefaultClientDriver"="Mass_Storage_Class"
; Serial Config [HKEY_LOCAL_MACHINE\Drivers\USB\FunctionDrivers\Serial_Class] "Dll"="serialusbfn.dll" "DeviceName"="USBFNS1:" "Prefix"="COM" "IClass"="{CC5195AC-BA49-48a0-BE17-DF6D1B0173DD}" "idVendor"=dword:0547 "Manufacturer"="Sozhou" "idProduct"=dword:2720 "Product"="Sozhou Product" "bcdDevice"=dword:0 "DeviceType"=dword:0
; RNDIS Config [HKEY_LOCAL_MACHINE\Drivers\USB\FunctionDrivers\RNDIS] "UseActiveSyncIds"=dword:1 "Dll"="rndisfn.dll" "FriendlyName"="Rndis" "idVendor"=dword:0162 "Manufacturer"="Sozhou" "idProduct"=dword:0001 "Product"="Sozhou RNDIS" "bcdDevice"=dword:0 [HKEY_LOCAL_MACHINE\Comm\RndisFn1\Parms\TcpIp] "DefaultGateway"="" "UseZeroBroadcast"=dword:0 "IpAddress"="192.168.0.12" "Subnetmask"="255.255.255.0" "EnableDHCP"=dword:0
; U Disk [HKEY_LOCAL_MACHINE\Drivers\USB\FunctionDrivers\Mass_Storage_Class] "Dll"="usbmsfn.dll" ;USB Client的驱动 "DeviceName"="DSK1:" ;被映射为U盘的存储设备的设备名 "DeviceName1"="DSK2:" ;更多的存储设备,比如SD卡 "FriendlyName"="Mass Storage" ;显示设备名 "idVendor"=dword:2310 ;Vendor ID,应该向USB组织申请 "idProduct"=dword:1234 ;Product ID,由厂商定义 "Manufacturer"="Sozhou" ;厂商名 "Product"="Sozhou Mass Storage" ;产品名 "bcdDevice"=dword:0 ;设备的版本号 "InterfaceSubClass"=dword:06 ;USB Host端通过该值来枚举设备,06h表示Mass Storage。 "InterfaceProtocol"=dword:50 ;USB设备所支持的传输协议,50h表示bulk-only。
3. 重新编译WinCE,下载运行
4. 基于USB Serial的串口通讯
对于WinCE目标板来说,直接打开串口就可以,在我的系统里面支持多个串口,USB Serial是"COM5:",通过CreateFile打开就可以,然后通过WriteFile和ReadFile函数来发送和接收数据,和一般的串口通讯是一样的。
对于PC来说,需要打开"wceusbsh001"设备,同样用CreateFile,这一点和普通的串口通讯略有区别,发送和接收数据同样用WriteFile和ReadFile,这里还是给个例子吧,搞清楚例子是PC端的串口通讯,如下:
#define USBSERIAL_NAME "\\\\.\\wceusbsh001"
int _tmain(int argc, _TCHAR* argv[]) { HANDLE hSerial; DCB PortDCB; COMMTIMEOUTS CommTimeouts; unsigned char buf[64]; DWORD i, num; BOOL SerialFlag; hSerial = CreateFile(_T(USBSERIAL_NAME), (GENERIC_READ | GENERIC_WRITE), 0, NULL, OPEN_EXISTING, 0, NULL); if (hSerial == NULL) { printf("Open Error.\r\n"); return 1; }
PortDCB.DCBlength = sizeof(DCB); GetCommState(hSerial, &PortDCB); PortDCB.BaudRate = 115200; PortDCB.ByteSize = 8; PortDCB.Parity = NOPARITY; PortDCB.StopBits = ONESTOPBIT; if (! SetCommState(hSerial, &PortDCB)) { printf("Set COM Parameter Error.\r\n"); CloseHandle(hSerial); return 1; }
GetCommTimeouts(hSerial, &CommTimeouts); CommTimeouts.ReadIntervalTimeout = MAXDWORD; CommTimeouts.ReadTotalTimeoutMultiplier = 10; CommTimeouts.ReadTotalTimeoutConstant = 10; CommTimeouts.WriteTotalTimeoutMultiplier = 50; CommTimeouts.WriteTotalTimeoutConstant = 100;
if (!SetCommTimeouts(hSerial, &CommTimeouts)) { printf("Set COM timeout parameter error.\r\n"); CloseHandle(hSerial); return 1; } // Write data
for (i = 0; i < 10; i ++) { memset(buf, i, sizeof(buf)); SerialFlag = WriteFile(hSerial, buf, sizeof(buf), &num, NULL); if (SerialFlag) { printf("Write %d bytes to COM.\r\n", num); } else { printf("Write COM Error.\r\n"); } Sleep(1000); }
// Receive Data
while(1) { memset(buf, 0, sizeof(buf)); SerialFlag = ReadFile(hSerial, buf, 16, &num, 0); if (SerialFlag) { if (num == 0) { printf("Read Successfully, but didn't read any data.\r\n"); } else { printf("Read data: "); for (i = 0; i < 16; i ++) { printf("0x%x, ", buf[i]); } printf("\r\n"); } } else { printf("Read COM Error.\r\n"); } Sleep(1000); }
CloseHandle(hSerial);
return 0; }
|
|