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