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