项目需要用MSComm控件实现射频设备与PC的串口通信。先说一下设备发送命令后会返回的相应的数据:2 W1 A# h! S* }
8 h0 A) c+ Y, t
当发送命令"H!i"时,为寻找芯片。9 v: n' ]- T( k* f. x7 j
当设备连接正确又无芯片在感应区时,设备循环返回:0630 和 15373 g5 j+ d3 Y* K
当设备当设备连接正确而芯片又在感应区时,设备返回0630,稍有停顿,继续返回4831+其他信息
0 S) Z. s' b j A! S! e% e
) F6 V! E2 E4 s3 {当芯片在感应区时,发送命令“R00000016U”,读取芯片前十六字节数据, u6 G0 a# T |7 P. o+ Y, g- d
没有错误时,设备返回:0630
. B# W7 ]2 n+ b. J4 N# z+ @此时,发送命令“STX”,或十六进制命令“02”/ O" S- U; ^+ C" g, s7 y
设备返回前十六字节数据/ @5 Z# A6 A; Z( `$ _. K9 {$ ^
# o/ g' ~" L4 d/ c之后继续发送命令“H!i”,寻找下一个芯片' P" O$ S/ E0 S4 d5 L0 W/ d- x
--------------------------------------------------------------------
2 T5 l4 U4 V3 s项目用MSComm进行串口通信
0 O# l. I2 R( @/ n/ C# [' c/ N. _ ]需要将上述几条命令循环起来。我用的是比较MSComm1.Input是否为争取的返回值“4831”和“0630”。
9 {; @2 x0 k& {2 I e' m用了三个timer来控制三条命令“H!i”、“R00000016U”和“STX”的发送。把发送命令都放在了timer_timer下。. [' _8 z0 d2 `4 O3 x7 R5 n
但是这样的话,就用到了三个Private Sub MSComm1_oncomm()。系统报错,“发现二义性名称”。意思应该是不能重复定义Private Sub MSComm1_oncomm()吧?* c( `3 }& I |+ S1 H( X0 E' R4 a
----------------------------------------------------------------------------4 |2 S \/ ]& n5 R" U# A) ]
其实程序说简单些就是:, h3 @, _$ U6 g- r' f) w
发送命令—>从数据返回区判定->发送命令->数据返回区判定->再发送命令->接收数据
& x$ U5 I' @1 P4 R0 V Y6 W4 S这个循环要怎么实现呢?3 @& T. u- w2 c" p, C4 c" f
先谢谢各位!8 d4 L4 h: G M4 z" l& N3 }) _
5 P1 i4 P$ C$ x# h0 |7 @2 }
--------------------------------------------------------------------
' K$ r6 ^7 {0 g" ^8 T4 ]# {下面是循环部分的代码。系统报错
) @" m6 _: b6 y* HPrivate Sub cmdStart_click()
) d& |' x d* L/ c# ]) S Timer1.Enabled = True
* k4 d. m* j0 A; Z% } F C2 U3 r
7 H' ]4 Q' V; F& U# u, [: X) u1 X m
& G6 U4 `. F; B4 }4 i
# p- _) T9 N# R, q+ uEnd Sub
! {8 |1 [) C+ y+ v" b6 JPrivate Sub timer1_timer()) e! p0 C8 P; d5 V
6 l# ? _8 t5 u6 ~
MSComm1.OutBufferCount = 0 '先清空缓存
% n) h8 `' ~, |. c( M3 c sendbuffer = "H!i"5 a4 Z% O% X6 C9 u' s
MSComm1.Output = sendbuffer" I' r1 @" h3 [+ Y! R) |8 N
End Sub( a |" Z6 W. J) S
'结束- d0 E; l4 H' k3 J7 O! _' `
Private Sub cmdStop_click(): w+ F" i- G0 _1 H! z' ?
Timer1.Enabled = False, a6 s! s; `' V Z+ F' g
Timer2.Enabled = False
# Z$ k ?, l0 t% } a4 V Timer3.Enabled = False
# K3 N: @" s x( BEnd Sub
6 [, K4 g7 y2 _! p& |* J
1 k& j8 R+ B" S" H B* T6 b( d) q'接收数据
2 n9 E8 K) q) ^/ _2 FPrivate Sub MSComm1_oncomm()
' X* y5 [! {3 k& q Dim inbyte() As Byte
, H+ X, M. r; W- s6 Z& r* Z Dim revbuffer As String
+ b4 N" h- j1 j+ ^/ V2 x7 T Dim a, i, b; v) W7 a* @' a! P- b7 W. e( J2 `
Select Case MSComm1.CommEvent
- b E- D# r. ]" `' V" F '如果有CT在感应区 f( P1 g+ m- l! U. d# ?& P3 K9 k7 D5 o3 T
* s# c5 n- K" O. K+ ~# i
Case comEvReceive) n9 h) G+ L$ C1 l5 f$ U- }
inbyte = MSComm1.Input
9 \! O9 m9 A$ M7 h4 v' ^8 { For i = LBound(inbyte) To UBound(inbyte) '返回数据转换为十六进制
" |" X& g# U9 k3 X revbuffer = revbuffer + Hex(inbyte(i))
& L. s& o% `: { Next i
# U& p3 G" v( ?6 u9 ^/ u txtHex.Text = revbuffer
$ o+ D2 K& Y. R0 R/ @3 t If Mid(revbuffer, 1, 6) = "480310" Then1 W) b* q4 \( w; X+ ~
Timer1.Enabled = False: K) v/ P3 W& E
Timer1.Enabled = True" M/ m; G9 ^+ {$ f' [: U7 j' Z
End If
- H) |; r8 Z- r8 ` End Select1 b4 m# w* {4 U5 Y+ h* J
Case comEvSend \! q2 k+ l; r3 U9 ?
End Select. B9 F9 E$ ^2 v3 f7 r, l
Private Sub timer2_timer()7 ~) K. l! g0 F! c9 c
MSComm1.OutBufferCount = 0 '先清空缓存: R) `: U0 R5 N& }3 V& I
sendbuffer = "R00000016U": J, K! M+ z- Q- O M
MSComm1.Output = sendbuffer
6 a' i+ A* c+ QEnd Sub
. R% B$ R* I$ T7 Y9 g5 NPrivate Sub MSComm1_oncomm()
1 {8 R- b+ o, u+ C, R% Y4 }1 H# h Select Case MSComm1.CommEvent
! Q- c( w- s7 D8 H: l2 C \4 ^& [; | Case comEvReceive0 p3 t$ v0 K; A
inbyte = MSComm1.Input& j) {/ Z; ~5 g* C1 S6 ?8 n
For i = LBound(inbyte) To UBound(inbyte) '返回数据转换为十六进制
9 m3 E x! m2 {% h9 J* d* l7 D revbuffer = revbuffer + Hex(inbyte(i))- M1 c' P8 r* ]7 ~2 z6 y0 K4 W- A7 ?
Next i6 r P( Z. h u
txtHex.Text = revbuffer; r# h3 [" {8 Z% m
If Mid(revbuffer, 1, 4) = "6030" Then
# Q5 I/ H9 o+ c- y! ]- q Timer2.Enabled = False
* t% }( n. P4 t, e1 b Timer3.Enabled = True
4 F! K( T' w3 S' u! d6 y' [: q End If
3 n; r9 b/ T; B Case comEvSend9 f" e; ?/ g/ F6 z: p
End Select) R# I- a; y! f- n5 B
End Sub
7 z9 s. s$ @4 H, JPrivate Sub timer3_timer()
1 V$ V/ L9 T/ u1 I# a5 ~ MSComm1.OutBufferCount = 0 '先清空缓存
$ @* U$ d- ^% [" i6 Y sendbuffer = "STX"& g- \* G* s7 u2 r( g
MSComm1.Output = sendbuffer
6 h0 ~2 i& @7 V; b Select Case MSComm1.CommEvent
; _# ?; M$ S4 F0 m6 l6 g Case comEvReceive
3 T! U) S2 \6 L& ?3 y9 z- V inbyte = MSComm1.Input+ D4 I S u8 N: ^2 J. Y" O
For i = LBound(inbyte) To UBound(inbyte) '返回数据转换为十六进制
' ?) w2 ?: L3 L) G, g* _ revbuffer = revbuffer + Hex(inbyte(i))
" j. u0 i* U9 M. G# b! ~# ] Next i
. w, f5 X; E0 S( d" b% w txtHex.Text = revbuffer
4 c! V8 x4 Z! A& T7 M Case comEvSend; v% f' l( c. C2 g% [
End Select
v4 U$ I* @5 W& JEnd Sub" O1 Q8 q. \; C/ Z& H
9 `& g; E- r }9 Q% x7 F) |' n) w" N0 P! p, ]4 e s9 F2 N( h5 R
3 B4 t5 R4 y( k! n8 |' q6 f: B0 | |