【转载】WinCE系统 USB Serial实现

转载自:http://blog.csdn.net/nanjianhui/article/details/4986390

 

USB Serial就是指基于USB的虚拟串口,实际上是将USB作为一个串口设备来进行操作。

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

 

1. 选择USB Client Serial组件

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

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

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

[HKEY_LOCAL_MACHINE/Drivers/USB/FunctionDrivers]

   "DefaultClientDriver"="Serial_class"

 

[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"=”Honeywell”

   "idProduct"=dword:2720

   "Product"=”Honeywell Product”

   "bcdDevice"=dword:0

   "DeviceType"=dword:0

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

 

3. 重新编译WinCE

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

 

4. PCUSB Serial驱动更新

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

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

%Honeywell% = Honeywell

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

[Honeywell]

%USB/Vid_0547&Pid_2720.DeviceDesc% = Host_Inst, USB/Vid_0547&Pid_2720

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

Honeywell = "Honeywell"

USB/Vid_0547&Pid_2720.DeviceDesc = "Honeywell USB Serial"

 

5. 安装USB Serial驱动

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

 

6. 基于USB Serial的串口通讯

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

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

 

#define WRITE_COM                         1

#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;

            }

 

#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./r/n", num);

                        }

                        else

                        {

                                    printf("Write COM Error./r/n");

                        }

                        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./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);

            }

#endif

 

            CloseHandle(hSerial);

 

            return 0;

}

 

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

posted @ 2011-10-13 11:32  风雨雪夜  阅读(320)  评论(0编辑  收藏  举报