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