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