WinCE系统 USB Serial实现

时间:2022-04-09 06:55:45
这里介绍一下USB Serial。USBSerial就是指基于USB的虚拟串口,实际上是将USB作为一个串口设备来进行操作。

对于应用程序来说,通讯起来比较简单,就是打开串口,然后接收发送数据就可以了。下面介绍一下具体步骤:

 

1. 选择USB Client Serial组件

在定制WinCE的时候,在Catalog Items View中选择”DeviceDrivers”->”USB Function”->”USBFunction Clients”->”RNDIS Clients”,如图:

WinCE系统 USB Serial实现


2. 更改USB Client的注册表配置

选择默认驱动为Serial_class类,如下:

[HKEY_LOCAL_MACHINEDriversUSBFunctionDrivers]

  "DefaultClientDriver"="Serial_class"

 

[HKEY_LOCAL_MACHINEDriversUSBFunctionDriversSerial_Class]

   "Dll"="serialusbfn.dll"

   "DeviceName"="USBFNS1:"

   "Prefix"="COM"

   "IClass"=""

   "idVendor"=dword:0547

  "Manufacturer"=”Honeywell”

   "idProduct"=dword:2720

   "Product"=”HoneywellProduct”

   "bcdDevice"=dword:0

   "DeviceType"=dword:0

具体这些配置不多说了,以前已经介绍过Mass Storage和RNDIS,这个应该很好理解。

 


3. 重新编译WinCE

完成了上面的配置以后,重新编译WinCE,然后下载到板子上面运行。


4. PC端USB Serial驱动更新

如果想让PC识别出目标板的USB设备,这里必须安装相应的驱动。驱动包含在微软提供的ActiveSync软件中,从微软的网站上面下载就可以了,然后在PC端进行安装。安装好后,进入安装目录找到”Drivers”目录,在该目录下有个文件叫”wceusbsh.inf”,打开这个文件,添加相应的驱动信息,VID和PID要和前面的注册表配置保持一致。具体改动如下:

(1) 找到第一个并添加如下信息:

%Honeywell% = Honeywell

(2) 搜索到第二个并添加如下信息:

[Honeywell]

%USBVid_0547&Pid_2720.DeviceDesc% = Host_Inst,USBVid_0547&Pid_2720

(3) 搜索到第三个并添加如下信息:

Honeywell = "Honeywell"

USBVid_0547&Pid_2720.DeviceDesc = "Honeywell USBSerial"


5. 安装USB Serial驱动

具有USBSerial功能的WinCE在目标板上运行之后,将USB插到PC上面,这时会弹出安装驱动的对话框,根据向导将驱动程序的位置指向”MicrosoftActivesyncDrivers”目录,然后就可以成功安装了。


6. 基于USB Serial的串口通讯

对于WinCE目标板来说,直接打开串口就可以,在我的系统里面支持多个串口,USBSerial是”COM5:”,通过CreateFile打开就可以,然后通过WriteFile和ReadFile函数来发送和接收数据,和一般的串口通讯是一样的。

对于PC来说,需要打开”wceusbsh001”设备,同样用CreateFile,这一点和普通的串口通讯略有区别,发送和接收数据同样用WriteFile和ReadFile,这里还是给个例子吧,搞清楚例子是PC端的串口通讯,如下:


#defineWRITE_COM                        1

#defineUSBSERIAL_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.rn");

                       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.rn");

      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.rn");

       CloseHandle(hSerial);

                       return 1;

           }

 

#if WRITE_COM

           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.rn", num);

                       }

                       else

                       {

                                   printf("Write COM Error.rn");

                       }

                       Sleep(1000);

           }

#else

           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.rn");

                                   }

                                   else

                                   {

                                               printf("Read data: ");

                                               for (i = 0; i < 16; i ++)

                                               {

                                                           printf("0x%x, ", buf[i]);

                                               }

                                               printf("rn");

                                   }

                       }

                       else

                       {

                                   printf("Read COM Error.rn");

                       }

                       Sleep(1000);

           }

#endif

 

           CloseHandle(hSerial);

 

           return 0;

}

我应该是在VS2005下写的这个代码,时间有点久了,应该可以用,只是发送接收数据,供大家参考。