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