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