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