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