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