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