项目需要用MSComm控件实现射频设备与PC的串口通信。先说一下设备发送命令后会返回的相应的数据:3 M4 j9 u9 j' s
8 W* D6 W3 L# I+ h5 c' Q- k
当发送命令"H!i"时,为寻找芯片。& Z7 P" l) k5 J+ y- G# w5 \
当设备连接正确又无芯片在感应区时,设备循环返回:0630 和 1537
# ]. d; x5 o/ q# y/ s& r4 C当设备当设备连接正确而芯片又在感应区时,设备返回0630,稍有停顿,继续返回4831+其他信息
) l2 t' P& y, q5 j
0 ~3 `" X3 B! o% Z* Q当芯片在感应区时,发送命令“R00000016U”,读取芯片前十六字节数据* Q- Y# a) Z9 N4 o: r
没有错误时,设备返回:06307 y$ w. ^2 N( I& T. F5 E$ A
此时,发送命令“STX”,或十六进制命令“02”+ p. y" Z1 C! }1 m9 k1 x2 m9 U
设备返回前十六字节数据7 M! g7 Q4 ]; M" E! e
; ^8 d1 R& X l) |
之后继续发送命令“H!i”,寻找下一个芯片
. g: [$ y2 f/ n% A k1 T--------------------------------------------------------------------
/ g3 Z0 M' q$ H1 X" R项目用MSComm进行串口通信
3 T6 c2 l0 d8 ?% J需要将上述几条命令循环起来。我用的是比较MSComm1.Input是否为争取的返回值“4831”和“0630”。* M4 W, N' ?$ h' J* b7 q$ U
用了三个timer来控制三条命令“H!i”、“R00000016U”和“STX”的发送。把发送命令都放在了timer_timer下。
' y: Z1 u c' Q但是这样的话,就用到了三个Private Sub MSComm1_oncomm()。系统报错,“发现二义性名称”。意思应该是不能重复定义Private Sub MSComm1_oncomm()吧?/ a7 x! c/ b; F' p# x. H* S/ t
----------------------------------------------------------------------------
1 k( Z. M4 l& m0 |其实程序说简单些就是:
2 K2 s4 m+ b% ]/ H/ f' q* F发送命令—>从数据返回区判定->发送命令->数据返回区判定->再发送命令->接收数据" F$ X I5 Q8 q' S& d
这个循环要怎么实现呢? s# @ H$ P* p \0 `
先谢谢各位!
. a1 o3 S7 Q7 O9 d7 X
% C, Y% j" u* k' T--------------------------------------------------------------------
+ e- y# ?/ l) N5 _8 \! l0 @+ d2 B下面是循环部分的代码。系统报错
' M8 M) ]8 i8 J( l7 B fPrivate Sub cmdStart_click()) w4 K. y& G( T/ W. Z8 U& G/ r
Timer1.Enabled = True
: a* n0 Q- J3 s) C. Y
' L4 Y1 N- U' A1 | Y( w( y ; p6 `9 c% T* X" K
5 ?" f9 X! Q) D! a8 k; ]
End Sub2 y3 J+ @/ U. l+ v5 n
Private Sub timer1_timer()
5 i: l% s$ v$ m% i& P
" `6 ]+ T. b* j" @8 l- P MSComm1.OutBufferCount = 0 '先清空缓存" Q R* t2 m) x! {
sendbuffer = "H!i"
/ Z! F5 [ ~ N MSComm1.Output = sendbuffer _: L. m: A# ` e: P1 w
End Sub7 c$ W) m% b# _' h- J
'结束
4 k6 @9 g9 L# e5 H; o( ?6 }6 mPrivate Sub cmdStop_click()" M0 P$ b M7 ^# |; l( B0 g+ h
Timer1.Enabled = False
: E+ O$ |5 i! s" q Timer2.Enabled = False7 ^. p6 _3 A u q0 ~
Timer3.Enabled = False/ L' B' ^4 W0 r; ?% a: W
End Sub, ~" h0 A6 q! l4 \
' f! w' R4 s* l/ ^; a'接收数据: W# o: `2 {- y' l9 t1 t0 J" X
Private Sub MSComm1_oncomm()
, i( T6 ^4 u" {8 s+ E. ? Dim inbyte() As Byte1 a) |" P; G( u7 i
Dim revbuffer As String7 R5 x: z, u- B! I1 n
Dim a, i, b
- N, ^2 L( b% F' `) F Select Case MSComm1.CommEvent
, _' \! K# R t4 @* \ '如果有CT在感应区
2 d. q1 q- m; k g 4 i6 p5 D2 G: i! A8 `5 |2 ?: i$ C
Case comEvReceive
, ?2 z: Z i1 k inbyte = MSComm1.Input
0 H5 X1 G/ a9 y: t For i = LBound(inbyte) To UBound(inbyte) '返回数据转换为十六进制8 M/ P# u3 U1 A4 J
revbuffer = revbuffer + Hex(inbyte(i))" c+ M7 b: `5 y6 h# Y
Next i* ?; a, K2 @& N- i% ^+ S% d
txtHex.Text = revbuffer
# d+ O, y) |: Z: o. X If Mid(revbuffer, 1, 6) = "480310" Then
! s& O0 {9 n9 I6 u Timer1.Enabled = False% |' b# H* W" x. P, s/ Z
Timer1.Enabled = True
* C7 W) q5 ]) d End If! a" m0 G! Z( s1 B0 W1 Y7 M$ N
End Select% ]8 C2 X6 P/ q
Case comEvSend
1 o) v; N; U- c7 K$ ?* k End Select' P9 h9 u C7 R! A$ g n3 _
Private Sub timer2_timer()! W4 Y: [# n, l' F5 i8 G1 A1 H+ P/ M: P
MSComm1.OutBufferCount = 0 '先清空缓存1 C! ^3 R0 Z* v0 j
sendbuffer = "R00000016U"
; Y4 Z" E# c7 J( [. S$ j: k, w4 D MSComm1.Output = sendbuffer
3 c i/ h/ H6 [* C6 k$ E( S* ^End Sub
& D* l+ E9 p$ _+ C/ qPrivate Sub MSComm1_oncomm(), Y* E5 p' { t+ g2 h3 X- \
Select Case MSComm1.CommEvent9 |* V8 @8 m; m( o6 D+ R m
Case comEvReceive
! _* w4 }( N7 x5 q2 z% Q0 C inbyte = MSComm1.Input
0 w& F6 ?1 S/ i, S! Y For i = LBound(inbyte) To UBound(inbyte) '返回数据转换为十六进制0 I2 h; b) H7 A
revbuffer = revbuffer + Hex(inbyte(i))& h8 \4 _) Z) L7 T4 l
Next i
- h) ~! [' F; _9 \4 M$ B( U* S, s txtHex.Text = revbuffer( S7 y8 g4 U5 g9 ], M" f
If Mid(revbuffer, 1, 4) = "6030" Then
! V+ m# T4 h! w Timer2.Enabled = False
) ]$ Q4 ~6 ~" R8 i' u% e Timer3.Enabled = True" G; B8 P k7 f* X
End If* E- _, B# U3 j; c& b( J4 k f X$ O/ v
Case comEvSend: b4 C' \/ B* G- V, }, U
End Select+ w! r& U5 D7 A- v" n3 Y( l& z
End Sub
7 @! d, Y# m5 }8 w) r# nPrivate Sub timer3_timer()& ?5 G J+ X1 M B' Q9 Y( h0 s
MSComm1.OutBufferCount = 0 '先清空缓存
; U" _2 u; t" O/ Q+ W: Q2 f0 ^ sendbuffer = "STX"- `) J- e( |! ^! x
MSComm1.Output = sendbuffer, U6 M9 [, e& W, S" c
Select Case MSComm1.CommEvent
/ o0 {; V |+ H# F; a/ j s& ~ Case comEvReceive6 {: I, y% T; i: q# s; O
inbyte = MSComm1.Input
4 x: t& {" g7 R2 X For i = LBound(inbyte) To UBound(inbyte) '返回数据转换为十六进制
+ c0 T" h( m* h& a* u; z revbuffer = revbuffer + Hex(inbyte(i))
, ^* Q' p* m' p5 g Next i3 z& K8 w9 v9 ~6 c8 K' w
txtHex.Text = revbuffer# e6 {1 ~$ j4 [5 h
Case comEvSend
- x/ H$ o( C. f End Select! c; W. X' a: f( h
End Sub
2 S1 I; d2 c' @" ^# V. e! ~# H1 [0 ]
& x% {* k* f3 @& Z+ w8 P; Z {3 _, S2 z6 ?: J( P3 i% B2 S
. `0 Y% C; ^5 V$ f
|