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