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