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