|
看到这样一个usb通讯的帖子,可能对你有用! 主 题: 关于usb通讯方面的问题。请AKing大哥及相关高手来看看,万分感激!!! ) v& j6 y; H4 w$ o9 Z# `
作 者: aiyu33 ()
% u# E1 [3 P, s* e$ N等 级: 8 X% P8 |5 c E$ D- n: _: b
信 誉 值: 100
. S0 J! P( o0 h所属论坛: C++ Builder Windows SDK/API 0 Y6 @! z# R* s) v# b: q) t
问题点数: 100 6 S+ Y) u/ s- @; k, ^: O4 z) h& J
回复次数: 12 2 c1 J: d$ u# R* i! _1 n
发表时间: 2003-12-16 20:43:13
- B3 F$ g, L" F' a: w g/ v
1 f; t9 N) V/ G 4 K" a& |# X1 E: y0 {
最今我接到了开发pc和pocket pc通过usb口通讯的任务。这两个星期我参考了很多方面的资料,特别是AKing大哥写的一些文章。但我始终没有打通这个关口,我贴出我的代码请AKing大哥看看,现在很急,多谢。# \% g" a6 `7 _9 ?* k4 W
pc的os是windows2000professional+ B e W; W- w8 P
pocket pc是hp ipaq 2210,os 是pocket pc2003。2 g0 ^! v1 t. ~4 ~% U
我装了activesync3.7。我查看过了,这个activesync目录下有pocket pc的驱动程序。pc也能识别。
4 Y; n- f! O0 i" D) k: z% v' U/ ~6 n下面是我在pc上调用usb的代码。
. h- G5 S: H3 E5 N下面的代码我能打开hidn类型的usb,但是就是不能打开pocket pc的usb。
6 T0 g+ f: j+ Q$ X, g% H5 s//classGuid = {25dbce51-6c8f-4a72-8a6d-b54c2b4fc835}* d+ s- z, ~* n4 ~% y
DEFINE_GUID(GUID_CLASS_PALM,
4 i4 e6 K, M% a% z7 h4 F% J$ ]9 r" F 0x25dbce51, 0x6c8f, 0x4a72, 0x8a, 0x6d, 0xb5, 0x4c, 0x2b, : Q. Z2 m/ }6 w" z3 E
0x4f, 0xc8, 0x35);2 |. V2 l2 b/ b. _" }
GUID HidGuid = GUID_CLASS_PALM;
3 J% G/ ~. k! @- h7 y NHANDLE Get_DeviceHandle( GUID* pGuid)$ { v9 |* X4 y* k8 Y h2 _
{( X% d* x. m" w0 X; T' n: P
, ~; }" I! c9 b$ ]/ h" } HDEVINFO info = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);3 M) s+ R3 H7 h
1 U7 T! o7 ^0 q5 q if(info==INVALID_HANDLE_VALUE)5 p7 H4 V8 x$ e% ?
{1 ^' d4 d; F$ O" ~# W* z$ i
printf("No HDEVINFO available for this GUID\n");1 S# D9 u6 b' ?7 O- V
return NULL;
* \/ E* r- a+ q) }. ~ }7 n, K2 k; ]9 \/ x0 P+ f: r" I
SP_INTERFACE_DEVICE_DATA ifdata;* M9 M, P7 B% B9 a
ifdata.cbSize = sizeof(ifdata);
; w; C5 D4 x1 y0 B& r//0000是我在注册表中找到的序号! z9 p! H1 g4 _- ?0 I
if(!SetupDiEnumDeviceInterfaces(info, NULL, pGuid, 0000, &ifdata))# V3 U1 r2 J6 H
{
+ q' j3 z* ?7 y+ w printf("No SP_INTERFACE_DEVICE_DATA available for this GUID instance %d \n", instance+1);% |9 p$ L( t3 y9 u0 D4 X9 K- ]
SetupDiDestroyDeviceInfoList(info);
6 A: G B6 g/ f+ l& V1 W return NULL;3 Z1 [/ X3 }" t. |: A
} // Get size of symbolic link name* b0 S. j& A! g$ o9 M
DWORD ReqLen;! p; z* ]: t( |" y' ~* b% u* [
2 x3 ^2 t' ^/ ?8 c2 @5 j
SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL);
, [5 b# p# ~0 |
6 B: K! H. M m PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new char[ReqLen]);
. O* _- c) ]/ x ( c" _; {1 G6 q( I
if( ifDetail==NULL)4 Z; \& K: W: F0 {
{
4 J3 [9 i% w; [) v0 V SetupDiDestroyDeviceInfoList(info);
7 x: i; }6 l! Q' a return NULL;
" p* v3 X+ S1 b! L' A6 ~5 l } // Get symbolic link name& b+ o# c, H3 s/ i& p' e! r4 [
ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
# Q8 f6 p! l! z( }) z7 X1 Y" B
$ L( z" C6 V! ?% x+ } if( !SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL))/ w6 \( J% }' A( q9 ~/ O
{/ X3 S/ s+ a+ { e @" E
SetupDiDestroyDeviceInfoList(info);' Z8 N/ @# f! K2 I% d3 c) O
delete ifDetail;
. ?. Y; d& ?( | return NULL;
! Z& \5 Z% a' {9 c; |( @" j } printf("Symbolic link is %s\n",ifDetail->DevicePath);
7 O, W4 F/ {) Y, w# | /*\\?\\usb#vid_03f0&pid_1016#5&1bc41f6c&0&1#{25DBCE51-6C8F-4A72-8A6D-B54C2B4FC835}这是ifDetail->DevicePath的内容*/) \1 V7 G$ z G2 Z8 I: |. [
// Open file
$ c: S7 c& J J( {% B//前面都很正常但就是下面始终得出一个无效的句柄,我也就无法开展下一步的工作.
6 R% x: e4 d4 G# w6 } n& Z4 Y I HANDLE pDevice = CreateFile(* E% R5 Y4 q& ?" [2 l1 K
ifDetail->DevicePath,
# k# A" |9 X( ^3 D GENERIC_READ | GENERIC_WRITE,
, ~$ b4 k, V7 n! O) P" g FILE_SHARE_READ | FILE_SHARE_WRITE,
6 r, N4 k, M5 s: ^! E NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);5 J$ f# G/ ?. `4 W
+ O- G, v M0 ?, q if( pDevice==INVALID_HANDLE_VALUE) pDevice = NULL;
& |, M& G# |0 a/ v e+ O SetupDiDestroyDeviceInfoList(info);
* \' q+ d' z4 } c/ G: C6 B- L3 T2 Q : [* ~7 z3 a1 K) J8 Z4 Y1 J( S. f
return pDevice;
( V5 N, \! T2 T; v}, P7 i) D7 T9 p! P7 \, L" W
再次感谢关注这个问题的人们。 2 l0 v. }# ^7 d) q- U# l3 G8 [, n
# m) F! w4 Q+ W0 U$ J* @- ?
- x0 e3 q7 ]) s: n2 Z 回复人: constantine(飘遥的安吉儿) ( ) 信誉:101 2003-12-16 21:23:57 得分:0 0 t6 o( M6 p- p" ^4 t" U7 Z
. q* c5 k4 T3 z ' J, b" [% {5 Y4 g" s5 v/ z' k
6 x9 j2 G+ t4 y. N& \5 q) ^study
) f5 v9 K/ [: v+ y& \9 r - A' G: g0 z: D ?, @8 P& J
Top
0 u. u+ F4 k7 G1 }$ p- ^3 o& ^' n; f% p% F
9 g# @/ H* a# S) u! t9 U 回复人: wenyongjie(一个想成为大鸟的小菜鸟) ( ) 信誉:95 2003-12-16 22:28:46 得分:0
$ V# F( J/ [* A8 b& z* K 0 i _& c/ H8 E! G$ o3 S
! T/ d" ^5 k6 e$ I( q$ k- K
; w% }( g6 ]( x! Y7 s3 |- L
只有学习的份 ( ?5 |* o# X( M W
6 v {0 ]. S" n, s/ c: }Top
( m; ]- W! y7 l 0 L1 v1 @5 Q, Y& B" w B8 V
回复人: HUANG_JH(保卫钓鱼岛) ( ) 信誉:115 2003-12-17 9:50:41 得分:10
# U( h: m) c/ L" [8 Q1 J
& J P0 P! y3 ^' c$ n) W: Z' a D) W2 a, N5 w! d4 {0 C, n! f1 m- g
. ?" o" |& {* m$ k1 N$ V
不知道有没有用! ///////////////////////////////////////////////////////////////
& Y: x& e: _8 B- q- u// HW9911 evaluation board software Rev. 1.0
1 e. F! T, m& F9 M1 R// program by Liu Ding, Bei Jing HEAD Elec. i: q- p, U4 ?$ W" N
// this is the main program for the HW9911 USB device evaluation board
* P! a( G8 k5 ]5 f5 N// it illustrate the basical read/write process of HW9911: K! S( [8 R' k: g9 g8 p; l% k
// by three examples:
( B9 J! }4 N- C# g& }+ `3 b// 1. write datas to ram on the evaluation board
/ Y# U4 S2 [- H6 m, T* I// 2. read the datas from the ram on the board
' l# n( p) j: M, M0 {// 3. send datas through RS-232 serial prot on the board
. K" Q2 S* |6 _5 A9 y3 R T// ) @8 ~( _+ m. s! l& X0 H5 l
// the whole project are build with MSVC 6.0& ]3 ]/ X: U- |! O! l
// and tested on windows 98 platform.
/ b" F2 p) N2 p e( S//$ s% k0 g9 D" V/ v0 `6 l0 @9 f% v
// notes: you can use hyper terminal to reaceive datas form RS232 port" K) f. v$ ?2 W. |+ s' y% L, O+ f
//2 o1 M# k% t4 a) y& e6 ^5 Y5 }
// this software are designed by Liu Ding5 _* l! u0 c1 d. }8 i) O) M# y: Q/ Q
// if you meet any problems, pls contact by email:4 ^: ~/ d$ A2 W
// hugehard@263.net0 P4 N. N: ?3 G. q
// or contact HEAD Co. by Tel 86-10-87312497 d9 }2 U7 R& t( ^
// or by fax: 86-10-87312495) E+ ]/ o. x4 L% p+ F. D y
/////////////////////////////////////////////////////////////// #include "hwdll.h"9 @, C ^0 V, s
#include "stdafx.h". {! \4 s$ J( V4 F- S2 D% w% t
#include <stdio.h>' A! |, n4 Z/ C& d
#include <windows.h>
" @ j% g- I4 I% ^ V$ D6 W9 V3 B8 c#include <memory.h>
$ g+ ~5 q' R# V& l( s#include <DEVIOCTL.H>, L& d5 q- { s4 Z7 H- Z6 f
#include <stdlib.h>
x" X& b# i$ \ v: u1 ^: D#include <conio.h>
g! p1 k- {+ v$ m( e#include "hw9911.h"
0 t" h: C6 N% b! Z4 L* G, \. V#include "test9911.h"
! E$ q1 `' u1 B$ mint main()3 K" L9 W2 W7 f4 j6 m
{
2 C# D. q% `+ ]% D! l3 D3 S int hOpen=FALSE;
9 |) h8 \7 S5 q _ int hCommandLength=32;
/ k4 r9 ~5 K$ P# g" s8 X# B4 P int hDataLength=64;1 O% u4 o9 h! _ A& {6 Q6 b$ j; M
int i,VertAddr=0,HorAddr=0;
1 y; i4 p. \- Q- I* z2 p int addr,TotalLen;$ K2 J' Y0 F _
USC StAddrH=0,StAddrL=0;
. e+ D+ K4 L2 x8 |% U" @* d- E USC DataLenH,DataLenL;6 k+ a, l. q- g* t# }1 |' o
USC *hDataBuffer,*hCommandBuffer;% p- l' o; E. I; N
USC ch1=0x61;
# X J- O* r- h$ k2 @( V* K" | HANDLE hDeviceHandle;: N+ Q' \. ^; x+ v2 {2 ^
+ p& X+ ]( M4 g, c1 w printf("************************************************\n");
5 j4 X2 I: n) x1 h printf("*******HW9911 USB DEVELOPMENT BOARD*************\n");
( v# P) r+ H9 Q' i( _" m' ~ printf("************************************************\n\n");
, [- P# N# i. K0 A printf("now press any key to open board\n");) r9 u. g( C6 Q; F$ O
getch(); /********* call hDeviceOpen in hwdll.dll to open the board***/. s6 X! o# M$ R+ j& u. O3 B
//hOpenDevice 说明:
" Z4 n( Y* \- i/ p8 `// BOOL hOpenDevice(HANDLE *DeviceHandle)
1 U. I% j* \. G+ v// 作用:打开设备
3 t' @$ D; y# E// 参数说明:* ^% }9 x9 G3 ]$ p; o
// DeviceHandle:设备句柄
5 c8 C# |4 L1 j9 h0 L# i// 返回值:
; [& |8 z) a* S4 l# B! k- g// 设备打开成功返回 TRUE,失败则返回FALSE9 ^/ u% e: J9 i1 W
if( (hOpen = hOpenDevice( &hDeviceHandle ))==FALSE)
$ d' m% K- s0 k4 T( ?+ L! U3 c {) C7 [$ y# W# V' h% `8 n
printf("can't open device\n");1 q$ \! a) ~7 z" y1 b# v9 q. V
printf("press any key to exit\n");; F' _" A( X u- q# I0 F: D
getch();2 i+ I' a3 v2 j8 \4 i* |7 u
return 0;6 L4 S3 F% E+ V) e$ @6 W
}" L" t0 u. b" x6 e2 m0 |) R
else ! s$ a9 _1 n" V, q K
printf("\nCongratulations! device opened!\n\n"); /****** build and send command **********/
3 Q1 M2 w' D0 l printf("how many bytes do you want to access?");0 @. h: D8 o# I* S8 ?. B7 o! l7 ]
scanf("%x",&TotalLen);
$ t! H [- c. W% d while(TotalLen > 0x7fff)
* l) r* d. y2 L( w& T {
% I$ g5 N, U' j! ?0 @+ S printf("pls input a hex data less than 08000\n");
- b2 x, c! s: P6 t' \/ i6 G, Q5 I scanf("%x",&TotalLen);
# [3 X1 x' q" K& o% c, l1 m }
+ i6 s' T/ | }- h h: I DataLenH=(USC)(TotalLen/0x100); //data length high bytes
" ~3 ~. h/ p- a+ @. I2 T DataLenL=(USC)(TotalLen%0x100); //data length low bytes printf("************************************************\n");, o5 L6 T6 E2 R+ f8 y3 y
printf("now program will write datas to ram on board\n");0 y' {" F. ?% E8 M) R/ q- R# _0 x
printf("total bytes of datas is %x\n",TotalLen);* A7 {- N8 S& J+ F6 E
printf("press any key to continue\n\n");
K& q+ x* W) g4 B" e* r getch();6 o( b3 X' P* w, U! W
/*****************************************************************
% T- E2 |5 }: W! x$ e [ now will build a write command packet ; x4 Y0 ? P' b/ K/ D" T1 {
with the first byte is 'w'.
4 k& X3 _0 ^& n- M/ N; n2 V and the following bytes are:# U3 \( {. P4 L2 \0 U% [
start ram address byte high, start ram address byte low,; U9 ^( a# n+ I; A0 O: a
datalength byte high, data length byte low.
& w6 c- o: j Z* s: X& o this packet will send to device through WritePipe1,7 u4 [2 O; ^4 b
which is endpoint 2 of HW9911.' u. k4 R( r1 F
when the device receive this command packet,
6 y% O5 N5 \2 o3 q2 s$ f* c& m it will write TotalLen bytes data to Ram on the board
' D0 ? ^6 n* K( n( J- i. _& j2 S******************************************************************/
$ |( p* F: C& K9 j, J6 Z hCommandBuffer=(USC *)malloc(hCommandLength);
# V; T8 G: y1 y+ d4 @* t hDataBuffer=(USC *)malloc(hDataLength);) i. `: S3 \3 A% A8 `2 e
hCommandBuffer[0] = 'w'; //read command
7 E# ]! G a- H: O! e hCommandBuffer[1] = StAddrH;
6 O- u. c9 V: \* e3 n% c l7 M hCommandBuffer[2] = StAddrL;
# y, h1 v- Y$ x8 r hCommandBuffer[3] = DataLenH;
& ^3 X: [& u1 E# b* o4 l9 \0 X hCommandBuffer[4] = DataLenL; /* call hUSBIO() in hwdll.dll to send command packet to device*/
* v- V$ s5 \8 m4 y. q// hUSBIO 说明:
- |! ~' G( J0 {2 h+ G7 d// BOOL hOpenDevice(HANDLE *DeviceHandle,7 p5 }. c5 \" n: e2 V* T$ U
// unsigned char *IOBuffer,
, Q* Z9 F$ _. H: d// int BufferLength,1 [6 ?6 O" E: }# \
// int PipeNumber,7 y8 M2 k4 ]7 y {1 d7 _2 g
// int Action)
; t# V& y# ]+ O% K& N) X: Y// 作用:设备读写( U8 j' @* g4 h$ \2 N
// 参数说明:# _* z3 } G' Z. W) o2 b
// DeviceHandle:设备句柄
& S" _& O. ^2 S3 m O5 b// IOBuffer: 指向要传送的数据指针! _7 h9 ~0 m; G/ h
// BufferLength: 数据包长度* i7 F. H: y9 C/ J$ l `
// 对于发送缓存1、2和接收缓存1、2,BufferLength必须小于或等于32
Z1 f% {7 U% A: }// 对于发送缓存3和接收缓存3,BufferLength必须小于或等于64
2 O& w8 E* S6 G. J// PipeNumber: 通道号
7 d) h5 i% C' @ b// 发送缓存1:PipeNumber=0;
! |0 C. t2 Q: ]8 W0 g/ B// 接收缓存1:PipeNumber=1;
n Y6 |8 r- o. e! w9 h// 发送缓存3:PipeNumber=2;2 W' x+ p: g4 D% l0 t, P. g. ]
// 接收缓存3:PipeNumber=3;/ I, _& z; L+ N6 j8 T: W! @$ K
// Action: 读写标志。TRUE代表从设备读数据到主机
, e8 H( }; V' A# q; B' }// FALSE代表从主机发送数据到设备
+ K$ b/ Q( U6 @1 I4 { L8 A$ [( k' x! B// 操作接收缓存,Action必须为FALSE
( C( q6 Y$ n2 E% ~6 |; x! r2 U4 L' x" n// 操作发送缓存,Action必须为TRUE$ X2 l _4 \1 _8 b7 y% c
// 返回值:
- w$ A7 n) t8 B: ?. f8 R) I) P- M" J// 操作成功返回TRUE,失败返回FALSE if ( hUSBIO( &hDeviceHandle,4 f: L3 d$ M) n F: o% i6 e. c! U
hCommandBuffer,
. @, @: g. p* T6 |% [ hCommandLength,: m5 A* @) o+ u' ]( S. v$ Y" E
hWritePipe1,
' B+ ?2 @; }6 m8 e, m6 X; R+ I FALSE) == TRUE)
- c. g$ @% q/ i, x {
, Q/ i S& f3 D: V printf("command 'w' writed to board\n");0 [2 {) j+ _; M! g7 Z6 n
printf("now press any key to write datas to ram\n");
) \, r R7 c$ Q C getch();
. S) L# U! T7 M6 S0 B/ k3 K8 F$ N }* y* o. t* a9 s6 {
else% K7 N: W' a8 M
printf("data can't write to device\n"); /******** write ram data to board **********/ printf(" "); x9 P2 E! Y+ u
for(VertAddr=0;VertAddr<=0xf;VertAddr++): |7 X* D3 U3 m F7 {
printf("%02x ",VertAddr);, |( l: u2 i4 i) `) I: T$ e
printf("\n");" _, i) J2 C) q" S( C
addr=0;1 G9 _; ?' Q' Z$ I9 T/ ?" S
while (addr<TotalLen)+ F- ^; S) u8 x% Y+ F" R' d5 T
{. t5 \5 n2 o2 S# F/ `
if (addr+hDataLength > TotalLen)
& [) \; S: a/ x hDataLength = TotalLen-addr;2 j i* ]- G1 D' k/ y; F* p3 f
for(i=0;i<hDataLength;i++)$ |2 U; D8 H0 v% } t1 f
{; a3 l( H/ Q( s+ N2 c
hDataBuffer=ch1++;: y( `' F# @, i' E. y- h+ d' t% T
if (ch1 > 0x80) . k$ j3 {+ G. S R+ y
ch1 = 0x61;/ I( M, e0 d# F# _0 u. K8 Y
}9 {% V* k7 V/ n9 R1 L0 \1 A
/* call hUSBIO to write datas to write pipe3, which A( n+ K) O3 s% P6 {4 F" T
is endoint 6 of HW9911*/ if ( hUSBIO( &hDeviceHandle,' o' g6 E g$ G1 b
hDataBuffer," G# r$ a( n6 f3 h
hDataLength,. n' }! Q, u% a8 ^
hWritePipe3, ' [& x* A, h+ Q6 Y1 M. m
FALSE) == TRUE) //FALSE means Write operate
; w5 B% o! Y3 b8 @: ^! S7 }9 { {$ j' M+ B0 A6 o+ {
for(i=0;i<hDataLength;i++)0 K: E6 Z; f, z9 ]9 b' K6 G ]
{- g& ~( }# ~1 F
/* print format control */
- Y7 K4 ^' n0 n% r9 b if(i % 16 == 0)
4 Y0 y4 j% j, G3 E; u2 U {* j. O) j" V9 M; ~! K
printf("\n%04x ",HorAddr);
+ E, B/ X7 B* L2 y( p7 a& Q HorAddr=HorAddr+16;
& Z* ?; d% A K }) f# y6 b% G( ~0 D' \# c% d% S6 n9 Y
printf("%02x ",(USC)hDataBuffer);
3 {& `5 U+ \" {9 Q }
6 r$ D6 j3 A# v) q addr=addr+hDataLength;
4 l8 {5 O- h5 W }3 |1 T; k) j8 C2 e
else3 S" h- p( b8 H0 J) b [
{
+ d( Y5 n+ G5 d printf("can't write to board\n");2 B: ]( T- q; e1 A6 f5 H0 e
exit(0);
4 o4 q' P7 d4 O4 d9 p8 b) r }8 k. n: ~- a- g& x9 q
}
0 p- n$ [& n& p7 x3 }5 y! F1 C printf("\n\nTotal%x bytes write to ram OK\n\n",TotalLen); & | \) V% P) X
1 w5 \. o: J9 q% A( X" |Top
% q1 ~9 S& M2 N4 u, @! j* |" a+ o
5 D! ]' y4 c4 [, B" B1 R 回复人: HUANG_JH(保卫钓鱼岛) ( ) 信誉:115 2003-12-17 9:50:51 得分:0 ) N* k- @" }% B# A' c! B9 b
8 r& o% v, P! N1 g
$ v3 b0 e# w1 F' A8 \ /*****************************************************************
6 [( F4 \; D8 s' U+ v now will build a read command packet
: W+ p% N: I9 K* {- J) u: I% y with the first byte is 'R'.
" g9 f @4 A# L; @" a and the following bytes are:. j9 p3 I& q. W5 L
start ram address byte high, start ram address byte low,0 a" w1 V# Z* x: B: P! `; o! B
datalength byte high, data length byte low.
& o- C9 W3 n9 a this packet will send to device through WritePipe1,! ~1 T; C; l |; ~
which is endpoint 2 of HW9911.* W t; ?, I" B7 ?
when the device receive this command packet,
6 m0 x. m. i4 w" v it will read TotalLen bytes data from Ram on the board' z8 ~" v5 K' e8 L" E V, N
******************************************************************/ printf("************************************************\n");9 C) y$ P/ w) C; a4 Z+ A: |
printf("now program will read datas from ram on board\n");2 s U) O6 g- O: t
printf("total bytes of datas is %x\n",TotalLen);& |% J- g1 {) a6 Y% U! ^! T: W
printf("press any key to continue\n\n");
8 N2 w$ [/ g n: J# ?5 V% I getch();0 r$ y- W3 W% S! K1 r
hDataLength=64;
# Z6 I" i! A& U2 [1 B1 s; o$ }; D5 P- q hCommandBuffer[0] = 'r'; //read command
9 N9 R' m4 S* c+ g4 N hCommandBuffer[1] = StAddrH;
! E! d* H) o9 O) g# \0 e8 _ hCommandBuffer[2] = StAddrL; + ?' T9 \8 L* G/ R K
hCommandBuffer[3] = DataLenH;
' n) B# p$ f8 u, t hCommandBuffer[4] = DataLenL;
& p& y* M! p. C; A; B* j/* call hUSBIO to send packet */
7 w2 }5 d+ [- j! M; H: R0 | if ( hUSBIO( &hDeviceHandle,, m- S; p: q+ W7 l- v9 s
hCommandBuffer, i. g- s' H# c/ ]. j( d$ X
hCommandLength,
* L2 r+ a L. p* u& L/ b hWritePipe1,
# q* s1 ^+ s* J FALSE) == TRUE)
. ~& m% {' i# o {
& t; c3 v% T" b* I: W% z( Z! T printf("command 'r' writed to board\n");
. d1 l2 \; u6 B4 q5 n a1 @! b printf("now press any key to read datas from ram\n\n");
3 x% ~) k) ^5 @! w8 p getch();0 ~* @7 t6 r9 X, I$ B
}* \# M2 U9 C& E
else8 I' `8 S5 z3 I7 w" @
printf("data can't write to device\n"); /******** read ram data form board **********/
% Q: @+ U& `- q! Y printf(" ");
) G8 x' i9 c6 b/ b$ S j8 z# A* e4 [ for(VertAddr=0;VertAddr<=0xf;VertAddr++)
, a9 e" T/ O1 A" l printf("%02x ",VertAddr);) h8 L N9 }- t3 @7 Z
printf("\n");
: Q: t4 T8 X- E& B; P3 a( I4 R& J0 z addr=0;+ L. ^% Z# U7 ]- f3 [
HorAddr=0;
4 X0 `: f- |( s* s6 s$ @ while (addr<TotalLen)1 w( E* V. I, E
{2 T1 C$ `! X' [" x* i- T4 E" e _! U
if (addr+hDataLength > TotalLen)
3 s& a: M7 N; n1 r+ S- V" I( q% n; h | hDataLength = TotalLen-addr;
0 B' F2 p" G" p+ I. o1 C/* call hUSBIO to read datas from board */
q3 `* V+ A; E. c P$ p if ( hUSBIO( &hDeviceHandle,
8 ^1 o9 `# w3 X# \. B8 ]& t hDataBuffer,; R& x9 i( ^* p4 \& G
hDataLength,
- i3 b8 \* P- i! I" j3 d. [ hReadPipe3,
. s/ ~0 S" E- D) h- w* G; p TRUE) == TRUE) //TRUE means Read Operate3 @1 j$ M6 Q( W& Z' J
{
9 q% U- h3 E# q" f' R3 D for(i=0;i<hDataLength;i++)
" B- R" x: Q. i1 _+ R3 `0 R) e {
G; v6 `, ^' n9 t% a/* print format control */; l, e0 ` s5 g9 V) i* `: N. h
if(i % 16 == 0)
/ G( d7 m! @3 [: z% [# `, N {
$ _" g# M" a4 X$ x, K5 A# A/ j7 F printf("\n%04x ",HorAddr);
! X4 M2 ^8 E z/ f# ^ HorAddr=HorAddr+16;
- t# O2 O- F/ i2 @ }2 d. J) D- G4 H0 L5 Z! Y
printf("%02x ",(USC)hDataBuffer);
' j$ F+ C+ R4 f. R9 U% ? }
& u8 M; |! Y- `' s/ f addr=addr+hDataLength;
, |3 M {' y$ s' P8 E% \: w } ^# n" L" }1 ^2 o8 X
else
- `, h: |; j- g% O {
8 ?/ C% ^# V3 z9 h' g! Q7 z; J printf("can't read from board\n");
, j8 k+ i1 h& G0 O! O4 h exit(0);$ E* I5 I! u* H2 U6 V4 d7 p
}
0 l% T8 y$ p9 E: d. U3 H9 h+ s }
2 o% d O! i2 E- h, Y printf("\n\n");
; X' z; k: j( r T: F printf("%x bytes read from ram OK\n\n",TotalLen); /*****************************************************/
: h/ c' Z/ {$ o( X4 L/* process rs232 test */
/ ?7 z4 F0 G7 R) B p3 ~2 \/* the first byte in this packet is command 's' */ ^2 i5 _8 w4 I! {- l. L5 b" a0 M% i7 H
/* and the following 2 bytes are used to set */
4 p( v8 S2 l- z: T. J7 q/* the baud rate of 8051 */
$ d- n3 d3 g; Q, ?% p8 d/* they will be write to TH1 and TL1 respectively*/5 D. n7 u$ H& C9 e/ u
/* the following bytes are datas will be */
' ^8 {: w/ H& e1 Q# k. w# r! L/* send through RS232 serial poart */+ q4 u/ A! \0 O! ?
/*****************************************************/% c5 k& B$ i$ M1 L
printf("\npress any key to process RS232 Test\n");/ e9 a2 l7 l9 N9 j
getch();4 y C2 `( t' V% F+ L2 u& J- P
2 N6 j3 c! ~- u4 h, o. }4 u
USC TH1,TL1;: s$ D: V$ s$ F- K. b U- U
char hStr[30];
J; d( ^8 ~& K, r2 {& l TH1=(USC)(BD9600/0x100); X5 _4 ]+ i1 y% P* X8 [
TL1=(USC)(BD9600%0x100);
, K+ F. H* Q' b1 `3 g hCommandBuffer[0] = 's'; //read command' h7 i% }' l1 ]2 x1 C. m
hCommandBuffer[1] = TH1;
. F1 v; ?1 |' ]8 \ hCommandBuffer[2] = TL1; //start address is 0x0000 strcpy(hStr, "Hi, dear HW9911 consumers! ");
/ T7 a# W9 a* V4 A; S printf("Hi, dear HW9911 consumers! ");2 p* z, z5 }7 A: a! k1 X
memcpy(&(hCommandBuffer[3]),hStr,27); if ( hUSBIO( &hDeviceHandle,
* _9 A- p4 H. a8 ^- p1 W hCommandBuffer,9 F. X4 Q1 [, o8 Y# x+ T7 {
30,
7 ~( m: x1 ]( ^( J. {$ A; l. r hWritePipe1,0 W- H8 w* ?' j
FALSE) == FALSE)8 k2 H0 Y# o% K0 k
{
! ?, }! [0 C' W( ?& N4 ^ printf("can't write datas to RS232\n");6 S+ A, v) N5 C' }! T3 X* p
exit(0);
5 |7 k' w8 o2 Q, ^% \ } strcpy(hStr, "I'm HW9911 Evaluation Board, ");
8 Q5 H& j/ F2 n* c( p( h printf("I'm HW9911 Evaluation Board, ");
: d/ R- C; i) z2 W/ M memcpy(&(hCommandBuffer[3]),hStr,29);( b" r$ W6 G0 U0 f5 Z
2 o9 u3 ^- [5 W
if ( hUSBIO( &hDeviceHandle,
/ {" _/ d1 W' R' g( l$ d hCommandBuffer,
; P. X; @& m# R B! V J 32,! g) c, F z' G9 w+ C
hWritePipe1,4 u/ a& o4 ~5 d" a' [7 @+ {# O
FALSE) == FALSE)& ^$ u: h2 b/ X n$ P3 d" A
{
2 m' q9 Q2 a9 K, r& t: j printf("can't write datas to RS232\n");
4 |( `- k' j6 i" H4 ]3 c8 c5 w; V exit(0);
' X1 q! u5 F: o$ f1 ]* m9 F. }! y } strcpy(hStr, "thanks for purchasing me, ");
2 v! Y4 Y ~, ~: {: _/ ` printf("thanks for purchasing me, ");7 k1 W& J2 \/ Z6 b+ ?( M! E% W$ b
memcpy(&(hCommandBuffer[3]),hStr,26);# w2 R# g" u! Z1 \% J+ |
+ O; t+ [, Y, @. O3 c6 d) {
if ( hUSBIO( &hDeviceHandle,
3 E/ y. O. N+ [: l hCommandBuffer,+ f1 u6 I0 x# x# q: H
29,
8 C$ j m+ b- ]# K$ C9 c hWritePipe1,
5 V& _7 L i# u) O: X) [ FALSE) == FALSE)
. M# M- t* v$ C; B% {! Z8 M9 f {
; y7 h% t5 D7 x4 u- B$ }' B printf("can't write datas to RS232\n");
* l# ?! `, A; Z3 S$ N exit(0);. a" F2 l$ f6 L
}1 @; r1 [4 O2 q2 o4 |& a1 m
2 w" M) C1 z) E; K- r- p# R
strcpy(hStr, "If you meet some problems, ");+ h, U7 B, H3 B* i8 g
printf(hStr, "If you meet some problems, ");
# C; G" Z4 N. B2 }- r- }9 j memcpy(&(hCommandBuffer[3]),hStr,27);6 R4 S; `4 h8 C; g
) A% t" p, O) `% ` Z
if ( hUSBIO( &hDeviceHandle,* T# K" P# U! c& }3 b7 \
hCommandBuffer,
8 h3 N( B {3 a# W6 [, P( Q5 {: c 30,
/ B. Y, y6 J4 n1 ~7 ?7 Y+ S hWritePipe1,
8 D$ r; L7 F& o FALSE) == FALSE)
& H# C- r& c3 ?* N {" d6 ~6 i+ L) }8 b
printf("can't write datas to RS232\n");
# v4 M# [! S( V exit(0);6 m' u* Z d, d, K
} strcpy(hStr, "pls contact my designer ");
& J2 p z. |, t# \5 N& S( l printf("pls contact my designer ");
, l6 z8 [% @* g4 s. z' F3 x memcpy(&(hCommandBuffer[3]),hStr,24);
1 v3 {0 T1 E2 }1 ~% L6 r
v* E1 G% h. T3 S, `3 g if ( hUSBIO( &hDeviceHandle,
( y: g/ x, ~; w hCommandBuffer,9 v3 Q$ |; V h: q" W) [% I8 v- p
27,
$ P# k3 R; Q9 V1 e; u) X) b hWritePipe1,* Z, e V, T# x! @: E7 F
FALSE) == FALSE)
4 P) D. \$ J) K {& U& p" i4 |: }7 j: U* g
printf("can't write datas to RS232\n");
: M, e# L+ ]* \. f( {, e# Z+ N, ~ exit(0);; {# v3 \5 S" q z- E$ r0 T
} strcpy(hStr, "by email: hugehard@263.net.");
2 g- ~. B- J# A printf("by email: hugehard@263.net.\n");2 t& y' Z5 Q7 H) u
memcpy(&(hCommandBuffer[3]),hStr,27);0 i- v2 @( R1 h6 ^; k
+ ]5 W' m( \, K' ^) ?3 \3 q9 T
if ( hUSBIO( &hDeviceHandle,
' E1 h. z; y; h5 f* R! R. O hCommandBuffer,% n0 ^( s: B& a4 j# F& O+ {
30,2 x* E, E. V3 ?! {( A7 d- G
hWritePipe1,
1 N% ?' b, U' S( B- @) p% R FALSE) == FALSE)( i1 [# N/ d& d8 E
{1 n- L2 Y* `8 P1 \
printf("can't write datas to RS232\n");) R. @( g# A; O! C
exit(0); L" m* H+ Q" {' S" F
}7 ]6 S% ^" W. u8 N8 b$ \& u, q
/********* call hDeviceOpen in hwdll.dll to open the board***/& _' ^0 V1 F- X1 f
// hCloseDevice 说明:% [) x. q h/ ]3 S) W' h8 Y$ ?! }
// BOOL hCloseDevice(HANDLE *DeviceHandle)/ C1 A9 l! @$ V8 R1 v' j
// 作用:关闭设备$ \2 X* P' E) o( `# L
// 参数说明:% |& p( l. u2 ~' `/ ~( \7 _
// DeviceHandle:设备句柄% }5 g; g. p- z1 V7 i D
// 返回值:
" P9 t% P0 }5 v2 @/ h// 设备关闭成功返回 TRUE,失败则返回FALSE
& D5 M4 c5 `& d* |$ Y if( (hOpen = hCloseDevice( &hDeviceHandle ))==FALSE)( | A* [4 I3 e% c$ X1 P; n5 s+ p
{. y! h+ _! _( j' S/ I4 ^' Y
printf("can't open device\n");$ D* ]7 j8 T3 a9 S l9 x
printf("press any key to exit\n");
C% J8 A, M$ w' b/ |) q getch();
$ m5 S7 S( H# d# ?8 v8 E" r return 0;9 w- h# `0 n1 o! E' T) \
}' a& K4 i) M& V) j9 {# L" \
else
5 H5 N" Q+ @+ N printf("device closed\n"); printf("press any key to exit the program\n");1 E% W+ N' h( {* e* _6 d1 d
free(hDataBuffer);! i! E. A& `! U4 B5 [/ n
free(hCommandBuffer);8 M1 j: Q! i q' [: Y, l9 A
getch();. t$ u1 Y B, f3 `8 ~
return 0;5 _: ]3 T( ^3 Y% m6 \+ Y2 Y
} # d) V' Q, J6 o, Q+ E) y
A1 [3 }6 ?4 j ) t5 z8 W% l) i& Y
Top
7 z9 L; f% C$ p/ F9 N2 G4 r
+ A/ B( W( a, [4 _* K 回复人: aiyu33() ( ) 信誉:100 2003-12-17 11:41:24 得分:0 ( h7 K' {5 D8 q/ o2 N
, G$ Y/ k; e( h ' m* J* C+ q ] @7 |: v/ e5 n% }
: c0 W5 K) `/ M: n& G1 U, I' ^
upupupupupupup
, y1 ^; X* D" b, ` 5 ^6 |: A; |* \3 [
Top $ q" O6 e. f9 F- u& I1 k
$ V+ ~' `$ l8 I4 K A# r% f8 J3 ] 回复人: kingcaiyao(AKing) ( ) 信誉:110 2003-12-17 12:53:39 得分:0
7 J7 f- Q7 V4 z( ^* ^ ' ^8 c" A* c5 n5 k( p
. w' H0 \; f4 Q" j7 v3 |
/ T6 T4 x' n' T- D' e4 Y你开发的这种软件类似于Palm Desktop Software,负责Pocket与PC之间通讯,我建议你最好先写一个驱动,将USB口虚拟成一个串口,然后直接对串口操作会很方便的。我现在做的程序可能和你相似是一个CDMA模块,通过USB口与PC机连接,然后由驱动程序将它虚拟成一个串口,我的工作就是和这个虚拟的串口通讯。 + q# \* @! A* I* _# ~
, y7 d# M3 z/ H3 L
Top 3 b! @! H# K& L# f6 z( ^) Z
% K* _3 `- K7 [, u: c1 q
回复人: kingcaiyao(AKing) ( ) 信誉:110 2003-12-17 12:56:27 得分:0
. h+ B; i; `5 ^9 w. C6 M
: B. k' ?, |2 d 2 Z; _/ u( z* K& q
3 g7 Q8 o6 J c6 g5 l: i. R' fPocket与PC连接肯定有驱动的,如果驱动程序没有将Pocket虚拟成一个串口,你也可以直接通过设备GUID和设备序列号进行访问,详情你参见我专栏上的一篇文章。当然了,通过虚拟串口来访问设备肯定要方便,简单些。 ' p* L5 t; H L9 ]0 N9 L3 R
1 a* `) I; [# G4 _6 X' j
Top ) m) ^/ p; b5 `2 l X6 ^9 D" w. R# g
# J! S' I5 ^. U5 m 回复人: aiyu33() ( ) 信誉:100 2003-12-17 13:31:08 得分:0 2 ^3 u* g. j- z2 X4 ~$ D% E p
% M9 v) f) V/ \ " C/ S: j E. s2 Y/ N
G4 U) t7 W+ V. {- e我得这款pocketpc默认只支持使用usb与pc相连的,而且activesync3.7就是使用usb传输数据的。如果我自己写驱动的话,不仅不熟悉而且很可能activesync3.7这个软件就使用不了。我只需要利用它的驱动。你的专栏上的那篇文章我也看了,我就是模仿你的写的代码,但就是到createfile这步就不行了,我的同事是用palm开发类似的程序,也是到这步就行不通了,是不是有其他的原因?! r2 _8 T2 k% N" _8 D3 L
我还想问问如果驱动程序将pocket虚拟成串口,如何查看它的符号名呢?
1 g( h7 I* `) E( v: I
) n8 C8 h: h4 \ : v! i/ W; N G+ T$ u t
Top
$ `- M$ R; u, q) {; r, `
5 [; |, L. n7 z) D 回复人: kingcaiyao(AKing) ( ) 信誉:110 2003-12-17 16:53:17 得分:0
4 j; e/ O+ ]4 v& I4 e" r
" M8 [& P. N! y# t
' H: o5 S& c! g 5 a, L' `& y$ {% n2 l0 H* p
假如将一个USB设备虚拟成串口的话,那么它的符号名可能是COM3,COM4,或COMn,这根据你当前PC机的配置而定 。它的符号名位于注册表中HKEY_LOCAL_MACHINE\CurrentControlSet中,你可以搜索注册表,另外在2000或以上的操作系统中,你可以到HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SerialComm中看到。 5 b: D) i7 M+ ~ Z
1 Z# l) K) r& V0 h# Y; m; O1 q
Top + o& A2 S7 B! T$ j( Z- P5 l7 T4 o2 |
" Z0 G4 x/ x. X5 z) o
回复人: kingcaiyao(AKing) ( ) 信誉:110 2003-12-17 16:55:33 得分:0
& X, E5 i' Q4 O: {, }$ V8 ^ + `7 | j9 Z- J8 B# G6 E0 s- Y
9 M6 {: v7 n2 c1 _* | U: N* o3 r
' K+ V) s+ E) |) U8 I9 [我以前在一张贴子中详细回答了如何利用设备序列号和设备GUID来访问该设备,你可以搜索一下CSDN。你所说的CreateFile不能打通设备,你要查一下,你的符号名是否正确,包括设备序列号和设备GUID是否正确,设备GUID你可以从驱动程序的安装向导文件.inf中找到,设备序列号则需要你到注册表中去找。
! t2 d1 G3 g- h, `! a 4 X( b# u! r5 m/ m# @1 {4 ^- e
Top
? k2 c; N# c p# @ + E! ~2 h4 j/ D8 b5 B* P. q
回复人: gyj_china(透明) ( ) 信誉:98 2003-12-17 18:15:30 得分:90
, Q/ `1 ?. L6 w* i9 x9 N9 F1 B, I: I
o H/ J) U$ B
b/ N, D/ D: z. y# E
, d' G) K O j0 C: H c \没有星星不敢发言:(
& \0 W* v5 w: W2 f $ T5 |! J/ e. ?
Top
0 H# _7 e$ Y* {7 k$ A1 c/ o ' O" k, j0 F4 Z9 d: T2 p/ V
回复人: aiyu33() ( ) 信誉:100 2003-12-19 18:27:28 得分:0 ) S) ~9 p, E! ]9 x% j8 Y
) \3 W4 o7 i4 @9 }3 |5 @
1 q7 I9 F+ R& f9 T
, m8 w# a) E2 ]
不好意思,是因为ActiveSync3.7一直在系统服务程序中运行,占用了驱动程序,所以不能打开设备。我删除了ActiveSync3.7后就可以了。 |