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