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