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