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