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