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