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