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