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