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