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