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