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