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