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