关于usb通讯方面的问题。请AKing大哥及相关高手来看看,万分感激!!!

aiyu33 2003-12-16 08:43:12
最今我接到了开发pc和pocket pc通过usb口通讯的任务。这两个星期我参考了很多方面的资料,特别是AKing大哥写的一些文章。但我始终没有打通这个关口,我贴出我的代码请AKing大哥看看,现在很急,多谢。
pc的os是windows2000professional
pocket pc是hp ipaq 2210,os 是pocket pc2003。
我装了activesync3.7。我查看过了,这个activesync目录下有pocket pc的驱动程序。pc也能识别。
下面是我在pc上调用usb的代码。
下面的代码我能打开hidn类型的usb,但是就是不能打开pocket pc的usb。
//classGuid = {25dbce51-6c8f-4a72-8a6d-b54c2b4fc835}
DEFINE_GUID(GUID_CLASS_PALM,
0x25dbce51, 0x6c8f, 0x4a72, 0x8a, 0x6d, 0xb5, 0x4c, 0x2b,
0x4f, 0xc8, 0x35);
GUID HidGuid = GUID_CLASS_PALM;
HANDLE Get_DeviceHandle( GUID* pGuid)
{

HDEVINFO info = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);

if(info==INVALID_HANDLE_VALUE)
{
printf("No HDEVINFO available for this GUID\n");
return NULL;
}
SP_INTERFACE_DEVICE_DATA ifdata;
ifdata.cbSize = sizeof(ifdata);
//0000是我在注册表中找到的序号
if(!SetupDiEnumDeviceInterfaces(info, NULL, pGuid, 0000, &ifdata))
{
printf("No SP_INTERFACE_DEVICE_DATA available for this GUID instance %d \n", instance+1);
SetupDiDestroyDeviceInfoList(info);
return NULL;
}

// Get size of symbolic link name
DWORD ReqLen;

SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL);

PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new char[ReqLen]);

if( ifDetail==NULL)
{
SetupDiDestroyDeviceInfoList(info);
return NULL;
}

// Get symbolic link name
ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

if( !SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL))
{
SetupDiDestroyDeviceInfoList(info);
delete ifDetail;
return NULL;
}

printf("Symbolic link is %s\n",ifDetail->DevicePath);


/*\\?\\usb#vid_03f0&pid_1016#5&1bc41f6c&0&1#{25DBCE51-6C8F-4A72-8A6D-B54C2B4FC835}这是ifDetail->DevicePath的内容*/
// Open file
//前面都很正常但就是下面始终得出一个无效的句柄,我也就无法开展下一步的工作.
HANDLE pDevice = CreateFile(
ifDetail->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if( pDevice==INVALID_HANDLE_VALUE) pDevice = NULL;
SetupDiDestroyDeviceInfoList(info);

return pDevice;
}
再次感谢关注这个问题的人们。
...全文
65 点赞 收藏 12
写回复
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
aiyu33 2003-12-19
不好意思,是因为ActiveSync3.7一直在系统服务程序中运行,占用了驱动程序,所以不能打开设备。我删除了ActiveSync3.7后就可以了。
回复
kingcaiyao 2003-12-17
Pocket与PC连接肯定有驱动的,如果驱动程序没有将Pocket虚拟成一个串口,你也可以直接通过设备GUID和设备序列号进行访问,详情你参见我专栏上的一篇文章。当然了,通过虚拟串口来访问设备肯定要方便,简单些。
回复
kingcaiyao 2003-12-17
你开发的这种软件类似于Palm Desktop Software,负责Pocket与PC之间通讯,我建议你最好先写一个驱动,将USB口虚拟成一个串口,然后直接对串口操作会很方便的。我现在做的程序可能和你相似是一个CDMA模块,通过USB口与PC机连接,然后由驱动程序将它虚拟成一个串口,我的工作就是和这个虚拟的串口通讯。
回复
aiyu33 2003-12-17
upupupupupupup
回复
HUANG_JH 2003-12-17

/*****************************************************************
now will build a read command packet
with the first byte is 'R'.
and the following bytes are:
start ram address byte high, start ram address byte low,
datalength byte high, data length byte low.
this packet will send to device through WritePipe1,
which is endpoint 2 of HW9911.
when the device receive this command packet,
it will read TotalLen bytes data from Ram on the board
******************************************************************/

printf("************************************************\n");
printf("now program will read datas from ram on board\n");
printf("total bytes of datas is %x\n",TotalLen);
printf("press any key to continue\n\n");
getch();
hDataLength=64;
hCommandBuffer[0] = 'r'; //read command
hCommandBuffer[1] = StAddrH;
hCommandBuffer[2] = StAddrL;
hCommandBuffer[3] = DataLenH;
hCommandBuffer[4] = DataLenL;
/* call hUSBIO to send packet */
if ( hUSBIO( &hDeviceHandle,
hCommandBuffer,
hCommandLength,
hWritePipe1,
FALSE) == TRUE)
{
printf("command 'r' writed to board\n");
printf("now press any key to read datas from ram\n\n");
getch();
}
else
printf("data can't write to device\n");

/******** read ram data form board **********/
printf(" ");
for(VertAddr=0;VertAddr<=0xf;VertAddr++)
printf("%02x ",VertAddr);
printf("\n");
addr=0;
HorAddr=0;
while (addr<TotalLen)
{
if (addr+hDataLength > TotalLen)
hDataLength = TotalLen-addr;
/* call hUSBIO to read datas from board */
if ( hUSBIO( &hDeviceHandle,
hDataBuffer,
hDataLength,
hReadPipe3,
TRUE) == TRUE) //TRUE means Read Operate
{
for(i=0;i<hDataLength;i++)
{
/* print format control */
if(i % 16 == 0)
{
printf("\n%04x ",HorAddr);
HorAddr=HorAddr+16;
}
printf("%02x ",(USC)hDataBuffer[i]);
}
addr=addr+hDataLength;
}
else
{
printf("can't read from board\n");
exit(0);
}
}
printf("\n\n");
printf("%x bytes read from ram OK\n\n",TotalLen);

/*****************************************************/
/* process rs232 test */
/* the first byte in this packet is command 's' */
/* and the following 2 bytes are used to set */
/* the baud rate of 8051 */
/* they will be write to TH1 and TL1 respectively*/
/* the following bytes are datas will be */
/* send through RS232 serial poart */
/*****************************************************/
printf("\npress any key to process RS232 Test\n");
getch();

USC TH1,TL1;
char hStr[30];
TH1=(USC)(BD9600/0x100);
TL1=(USC)(BD9600%0x100);
hCommandBuffer[0] = 's'; //read command
hCommandBuffer[1] = TH1;
hCommandBuffer[2] = TL1; //start address is 0x0000

strcpy(hStr, "Hi, dear HW9911 consumers! ");
printf("Hi, dear HW9911 consumers! ");
memcpy(&(hCommandBuffer[3]),hStr,27);

if ( hUSBIO( &hDeviceHandle,
hCommandBuffer,
30,
hWritePipe1,
FALSE) == FALSE)
{
printf("can't write datas to RS232\n");
exit(0);
}

strcpy(hStr, "I'm HW9911 Evaluation Board, ");
printf("I'm HW9911 Evaluation Board, ");
memcpy(&(hCommandBuffer[3]),hStr,29);

if ( hUSBIO( &hDeviceHandle,
hCommandBuffer,
32,
hWritePipe1,
FALSE) == FALSE)
{
printf("can't write datas to RS232\n");
exit(0);
}

strcpy(hStr, "thanks for purchasing me, ");
printf("thanks for purchasing me, ");
memcpy(&(hCommandBuffer[3]),hStr,26);

if ( hUSBIO( &hDeviceHandle,
hCommandBuffer,
29,
hWritePipe1,
FALSE) == FALSE)
{
printf("can't write datas to RS232\n");
exit(0);
}

strcpy(hStr, "If you meet some problems, ");
printf(hStr, "If you meet some problems, ");
memcpy(&(hCommandBuffer[3]),hStr,27);

if ( hUSBIO( &hDeviceHandle,
hCommandBuffer,
30,
hWritePipe1,
FALSE) == FALSE)
{
printf("can't write datas to RS232\n");
exit(0);
}

strcpy(hStr, "pls contact my designer ");
printf("pls contact my designer ");
memcpy(&(hCommandBuffer[3]),hStr,24);

if ( hUSBIO( &hDeviceHandle,
hCommandBuffer,
27,
hWritePipe1,
FALSE) == FALSE)
{
printf("can't write datas to RS232\n");
exit(0);
}

strcpy(hStr, "by email: hugehard@263.net.");
printf("by email: hugehard@263.net.\n");
memcpy(&(hCommandBuffer[3]),hStr,27);

if ( hUSBIO( &hDeviceHandle,
hCommandBuffer,
30,
hWritePipe1,
FALSE) == FALSE)
{
printf("can't write datas to RS232\n");
exit(0);
}
/********* call hDeviceOpen in hwdll.dll to open the board***/
// hCloseDevice 说明:
// BOOL hCloseDevice(HANDLE *DeviceHandle)
// 作用:关闭设备
// 参数说明:
// DeviceHandle:设备句柄
// 返回值:
// 设备关闭成功返回 TRUE,失败则返回FALSE
if( (hOpen = hCloseDevice( &hDeviceHandle ))==FALSE)
{
printf("can't open device\n");
printf("press any key to exit\n");
getch();
return 0;
}
else
printf("device closed\n");

printf("press any key to exit the program\n");
free(hDataBuffer);
free(hCommandBuffer);
getch();
return 0;
}
回复
HUANG_JH 2003-12-17
不知道有没有用!

///////////////////////////////////////////////////////////////
// HW9911 evaluation board software Rev. 1.0
// program by Liu Ding, Bei Jing HEAD Elec.
// this is the main program for the HW9911 USB device evaluation board
// it illustrate the basical read/write process of HW9911
// by three examples:
// 1. write datas to ram on the evaluation board
// 2. read the datas from the ram on the board
// 3. send datas through RS-232 serial prot on the board
//
// the whole project are build with MSVC 6.0
// and tested on windows 98 platform.
//
// notes: you can use hyper terminal to reaceive datas form RS232 port
//
// this software are designed by Liu Ding
// if you meet any problems, pls contact by email:
// hugehard@263.net
// or contact HEAD Co. by Tel 86-10-87312497
// or by fax: 86-10-87312495
///////////////////////////////////////////////////////////////

#include "hwdll.h"
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <memory.h>
#include <DEVIOCTL.H>
#include <stdlib.h>
#include <conio.h>
#include "hw9911.h"
#include "test9911.h"


int main()
{
int hOpen=FALSE;
int hCommandLength=32;
int hDataLength=64;
int i,VertAddr=0,HorAddr=0;
int addr,TotalLen;
USC StAddrH=0,StAddrL=0;
USC DataLenH,DataLenL;
USC *hDataBuffer,*hCommandBuffer;
USC ch1=0x61;
HANDLE hDeviceHandle;

printf("************************************************\n");
printf("*******HW9911 USB DEVELOPMENT BOARD*************\n");
printf("************************************************\n\n");
printf("now press any key to open board\n");
getch();

/********* call hDeviceOpen in hwdll.dll to open the board***/
//hOpenDevice 说明:
// BOOL hOpenDevice(HANDLE *DeviceHandle)
// 作用:打开设备
// 参数说明:
// DeviceHandle:设备句柄
// 返回值:
// 设备打开成功返回 TRUE,失败则返回FALSE
if( (hOpen = hOpenDevice( &hDeviceHandle ))==FALSE)
{
printf("can't open device\n");
printf("press any key to exit\n");
getch();
return 0;
}
else
printf("\nCongratulations! device opened!\n\n");

/****** build and send command **********/
printf("how many bytes do you want to access?");
scanf("%x",&TotalLen);
while(TotalLen > 0x7fff)
{
printf("pls input a hex data less than 08000\n");
scanf("%x",&TotalLen);
}
DataLenH=(USC)(TotalLen/0x100); //data length high bytes
DataLenL=(USC)(TotalLen%0x100); //data length low bytes

printf("************************************************\n");
printf("now program will write datas to ram on board\n");
printf("total bytes of datas is %x\n",TotalLen);
printf("press any key to continue\n\n");
getch();
/*****************************************************************
now will build a write command packet
with the first byte is 'w'.
and the following bytes are:
start ram address byte high, start ram address byte low,
datalength byte high, data length byte low.
this packet will send to device through WritePipe1,
which is endpoint 2 of HW9911.
when the device receive this command packet,
it will write TotalLen bytes data to Ram on the board
******************************************************************/


hCommandBuffer=(USC *)malloc(hCommandLength);
hDataBuffer=(USC *)malloc(hDataLength);
hCommandBuffer[0] = 'w'; //read command
hCommandBuffer[1] = StAddrH;
hCommandBuffer[2] = StAddrL;
hCommandBuffer[3] = DataLenH;
hCommandBuffer[4] = DataLenL;

/* call hUSBIO() in hwdll.dll to send command packet to device*/
// hUSBIO 说明:
// BOOL hOpenDevice(HANDLE *DeviceHandle,
// unsigned char *IOBuffer,
// int BufferLength,
// int PipeNumber,
// int Action)
// 作用:设备读写
// 参数说明:
// DeviceHandle:设备句柄
// IOBuffer: 指向要传送的数据指针
// BufferLength: 数据包长度
// 对于发送缓存1、2和接收缓存1、2,BufferLength必须小于或等于32
// 对于发送缓存3和接收缓存3,BufferLength必须小于或等于64
// PipeNumber: 通道号
// 发送缓存1:PipeNumber=0;
// 接收缓存1:PipeNumber=1;
// 发送缓存3:PipeNumber=2;
// 接收缓存3:PipeNumber=3;
// Action: 读写标志。TRUE代表从设备读数据到主机
// FALSE代表从主机发送数据到设备
// 操作接收缓存,Action必须为FALSE
// 操作发送缓存,Action必须为TRUE
// 返回值:
// 操作成功返回TRUE,失败返回FALSE

if ( hUSBIO( &hDeviceHandle,
hCommandBuffer,
hCommandLength,
hWritePipe1,
FALSE) == TRUE)
{
printf("command 'w' writed to board\n");
printf("now press any key to write datas to ram\n");
getch();
}
else
printf("data can't write to device\n");

/******** write ram data to board **********/

printf(" ");
for(VertAddr=0;VertAddr<=0xf;VertAddr++)
printf("%02x ",VertAddr);
printf("\n");
addr=0;
while (addr<TotalLen)
{
if (addr+hDataLength > TotalLen)
hDataLength = TotalLen-addr;
for(i=0;i<hDataLength;i++)
{
hDataBuffer[i]=ch1++;
if (ch1 > 0x80)
ch1 = 0x61;
}
/* call hUSBIO to write datas to write pipe3, which
is endoint 6 of HW9911*/

if ( hUSBIO( &hDeviceHandle,
hDataBuffer,
hDataLength,
hWritePipe3,
FALSE) == TRUE) //FALSE means Write operate
{
for(i=0;i<hDataLength;i++)
{
/* print format control */
if(i % 16 == 0)
{
printf("\n%04x ",HorAddr);
HorAddr=HorAddr+16;
}
printf("%02x ",(USC)hDataBuffer[i]);
}
addr=addr+hDataLength;
}
else
{
printf("can't write to board\n");
exit(0);
}
}
printf("\n\nTotal%x bytes write to ram OK\n\n",TotalLen);

回复
gyj_china 2003-12-17
没有星星不敢发言:(
回复
kingcaiyao 2003-12-17
我以前在一张贴子中详细回答了如何利用设备序列号和设备GUID来访问该设备,你可以搜索一下CSDN。你所说的CreateFile不能打通设备,你要查一下,你的符号名是否正确,包括设备序列号和设备GUID是否正确,设备GUID你可以从驱动程序的安装向导文件.inf中找到,设备序列号则需要你到注册表中去找。
回复
kingcaiyao 2003-12-17
假如将一个USB设备虚拟成串口的话,那么它的符号名可能是COM3,COM4,或COMn,这根据你当前PC机的配置而定 。它的符号名位于注册表中HKEY_LOCAL_MACHINE\CurrentControlSet中,你可以搜索注册表,另外在2000或以上的操作系统中,你可以到HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SerialComm中看到。
回复
aiyu33 2003-12-17
我得这款pocketpc默认只支持使用usb与pc相连的,而且activesync3.7就是使用usb传输数据的。如果我自己写驱动的话,不仅不熟悉而且很可能activesync3.7这个软件就使用不了。我只需要利用它的驱动。你的专栏上的那篇文章我也看了,我就是模仿你的写的代码,但就是到createfile这步就不行了,我的同事是用palm开发类似的程序,也是到这步就行不通了,是不是有其他的原因?
我还想问问如果驱动程序将pocket虚拟成串口,如何查看它的符号名呢?
回复
wenyongjie 2003-12-16
只有学习的份
回复
constantine 2003-12-16
study
回复
发动态
发帖子
Windows SDK/API
创建于2007-08-02

1202

社区成员

C++ Builder Windows SDK/API
申请成为版主
社区公告
暂无公告