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