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