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