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