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