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