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