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