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