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