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