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