项目需要用MSComm控件实现射频设备与PC的串口通信。先说一下设备发送命令后会返回的相应的数据:( r2 Y9 B2 y, q% [
& i# F0 y+ B% l1 u
当发送命令"H!i"时,为寻找芯片。+ R3 A5 S# D! i) q$ o6 R
当设备连接正确又无芯片在感应区时,设备循环返回:0630 和 1537; [+ c* z5 ?/ K/ l! G2 e
当设备当设备连接正确而芯片又在感应区时,设备返回0630,稍有停顿,继续返回4831+其他信息
' l; `0 h5 E9 x, ~$ x7 e& y$ ?0 D$ B6 g$ J/ M) C& m) [$ Q
当芯片在感应区时,发送命令“R00000016U”,读取芯片前十六字节数据
4 d4 M# W0 v7 ?" O没有错误时,设备返回:0630
& p1 C, [% U* K7 g1 O此时,发送命令“STX”,或十六进制命令“02”
1 B8 @7 B: y1 b( B( b设备返回前十六字节数据* K9 h0 c4 e7 c' `+ U0 f: y5 U
# | r% q# |" k* `, i/ h
之后继续发送命令“H!i”,寻找下一个芯片
0 Q3 _5 w. ~' ^8 l/ i% G--------------------------------------------------------------------3 j9 G( h% I3 U/ |2 P9 j1 v
项目用MSComm进行串口通信/ w% Y5 _8 D, l: G5 R0 t
需要将上述几条命令循环起来。我用的是比较MSComm1.Input是否为争取的返回值“4831”和“0630”。
9 e! F+ r! @, T, X用了三个timer来控制三条命令“H!i”、“R00000016U”和“STX”的发送。把发送命令都放在了timer_timer下。1 s4 l4 l! [% ?* h+ ]9 ?
但是这样的话,就用到了三个Private Sub MSComm1_oncomm()。系统报错,“发现二义性名称”。意思应该是不能重复定义Private Sub MSComm1_oncomm()吧?
6 d8 Z v! P6 o+ Q----------------------------------------------------------------------------% _- G1 L9 `2 y, G! E
其实程序说简单些就是:/ t- {! Y8 \4 K0 g
发送命令—>从数据返回区判定->发送命令->数据返回区判定->再发送命令->接收数据" _ Y" u3 U. z* o
这个循环要怎么实现呢?* J9 J+ [/ i1 h1 h: K
先谢谢各位!
; T1 s5 O. H o( K' x* Y
! ^5 O1 j4 Q0 M. L3 b5 x7 n# |--------------------------------------------------------------------
3 E. g% S* B8 {0 f& |下面是循环部分的代码。系统报错( G1 R# G8 Z Z+ H: G% G# P% D
Private Sub cmdStart_click()
8 _5 {# |5 g. V7 _% b3 h Timer1.Enabled = True
' d# E+ Y7 `) I$ O1 c
1 @+ m0 u% r4 c8 ~( V" E 8 X' [! @* S$ G; E, t3 K* f
U: E K7 r# w" {* W: i3 Q
End Sub4 J0 S0 d+ b# h& ~
Private Sub timer1_timer()4 ^& G) f6 |- d! @1 V( }* y/ _, T
' m4 w( W0 @& {
MSComm1.OutBufferCount = 0 '先清空缓存
9 ^# [, v: U) k sendbuffer = "H!i"
0 l ]; W& P8 H* K7 t6 ?: V MSComm1.Output = sendbuffer; W9 V8 M4 t4 |& G+ ~+ P
End Sub0 m9 T1 H2 E: |3 Z
'结束5 i: G7 Q( P+ ~ u, z4 r/ i/ R
Private Sub cmdStop_click()
8 C2 l7 s" e1 S1 a/ _/ D: K Timer1.Enabled = False
# B# }! P7 z& `1 U% S Timer2.Enabled = False% @# z8 z! M8 f; a. V4 T) u; N/ P0 e
Timer3.Enabled = False
5 p: l9 W9 ~& E6 k! VEnd Sub
" K/ B4 J5 ]" `; g. ?9 N4 Q) l/ I- t
'接收数据
/ a5 z/ I" P0 C$ z; i+ J( l$ q N3 tPrivate Sub MSComm1_oncomm()
1 f1 k4 Y2 r9 Z& c# B0 U8 u* q Dim inbyte() As Byte7 P& S- x" r# r. I" e( ~
Dim revbuffer As String. I3 V# a9 v! y2 {: f
Dim a, i, b) I3 C7 z9 F1 T* \& y% Q: J ?
Select Case MSComm1.CommEvent
- G( ~$ u8 V# Z+ a8 z8 O9 t '如果有CT在感应区3 H% ]/ C( q( A( ^- ]5 f3 _3 D! k2 @
! v" t$ \+ V+ y Case comEvReceive' |! o& [; U% R! {4 L
inbyte = MSComm1.Input5 y* s- ^) Q+ ?) R4 _* J' x
For i = LBound(inbyte) To UBound(inbyte) '返回数据转换为十六进制
7 X# ? O( T) x% O. I X revbuffer = revbuffer + Hex(inbyte(i))
, Y8 G2 Y \: Z E8 } Next i7 x' z' C4 J( d- j9 h
txtHex.Text = revbuffer
5 |) c* c! r7 B$ [ If Mid(revbuffer, 1, 6) = "480310" Then/ Q6 ^$ T- O S9 l% R5 D8 Y' S- m
Timer1.Enabled = False
1 y' w6 c: p, J8 ] Timer1.Enabled = True4 f5 {4 S; E: x& W0 p2 R) `: {
End If2 o' D! D! k. r: ~( {$ j, _
End Select* S" u f4 M0 u
Case comEvSend" @8 I- d' ?; H0 {6 H h( A5 M' ]- X
End Select
: n+ S7 {! ]2 N# |. i* fPrivate Sub timer2_timer()
5 Y! p5 U8 M$ U. y. i- {7 e. M MSComm1.OutBufferCount = 0 '先清空缓存% L" c! S( B8 C8 g
sendbuffer = "R00000016U"
8 W3 ?7 T8 w2 S3 Y1 d' x7 n& r MSComm1.Output = sendbuffer; n( Q- Y g4 K- Y; m ]
End Sub6 [8 i, z( y+ [% }( }
Private Sub MSComm1_oncomm()- `! I5 X: x$ B% x9 M
Select Case MSComm1.CommEvent
* E5 l7 s4 L6 a1 X5 `+ P1 u Case comEvReceive" H+ n3 l9 {: @% X4 a0 J/ G
inbyte = MSComm1.Input1 l3 Q3 W# `& G, f7 q5 t
For i = LBound(inbyte) To UBound(inbyte) '返回数据转换为十六进制$ ]7 ^ g* t. {. d: U
revbuffer = revbuffer + Hex(inbyte(i))
_5 T/ i/ \, U7 ^+ \% M2 o5 a Next i
& B$ g( u* t4 Z/ C5 ]; b1 Q4 [ txtHex.Text = revbuffer
/ p: l0 _2 o& |3 v If Mid(revbuffer, 1, 4) = "6030" Then
- v9 s1 i: k7 M- x6 F3 d3 ^ Timer2.Enabled = False/ A8 g- U) ~2 ]3 g" E9 c
Timer3.Enabled = True
" D5 A: \, I$ I7 @/ o; [ End If
4 @- c% H M% \4 d8 v/ t Case comEvSend- {- q. S# G$ l, S1 D3 |
End Select8 b! C8 k4 T \( X7 l8 ], l4 |
End Sub
3 `) o$ m+ G9 E/ f/ d) ^Private Sub timer3_timer()
+ s2 s/ n% C0 w9 O& f5 r MSComm1.OutBufferCount = 0 '先清空缓存
9 t' N- F8 l2 T+ l, S2 N9 }& o sendbuffer = "STX"
1 W' A4 P2 B2 H Z0 C; x' ? MSComm1.Output = sendbuffer$ f | |1 y( e
Select Case MSComm1.CommEvent
@! [, |0 [9 V1 [6 U4 H$ u+ x Case comEvReceive
" b/ H, R7 |! a inbyte = MSComm1.Input8 x8 y+ T$ a4 b, i
For i = LBound(inbyte) To UBound(inbyte) '返回数据转换为十六进制
( o7 y- t9 L( Z# p revbuffer = revbuffer + Hex(inbyte(i))5 l0 S; j5 ~+ L/ b
Next i- L# T# y" F4 ^6 h3 M+ o5 [* H
txtHex.Text = revbuffer
4 l+ K: s; |. C5 J! g* i- r+ ]& D Case comEvSend6 m) _6 M- i. D3 C( Z
End Select' I; O; m4 B6 y- Z1 d% C# ?
End Sub
$ o! J" S( c; ~ T. j/ q/ a$ {1 [
! P% F- a5 S+ U1 r5 j4 X, ^( |* M4 H& y ^0 Q1 b. S1 y
/ g Q3 `/ B, b; E3 s/ } |