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