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