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