關(guān)于VB的MSComm串口接收兩次,Time事件 10分
如下:(1)和(2)單獨(dú)用按鈕命令可以正常執(zhí)行..用Timer1執(zhí)行1和Timer2執(zhí)行2,收回來的數(shù)據(jù)就很亂..把(1)(2)都寫到Timer1下面下,收回來的數(shù)據(jù)也亂..是不是輸入輸出緩沖區(qū)兩個混亂
Private Sub Timer1_Timer() '時鐘事件,間隔設(shè)定Interval毫秒就執(zhí)行一次
(1)DIM SENTDATA AS STRING,,Dim getdata
MSComm1.InBufferCount = 0 '清空接收緩沖區(qū)
MSComm1.OutBufferCount = 0 '清空發(fā)送緩沖區(qū)
MSComm1.Output = 讀取數(shù)據(jù)指令
Do
DoEvents
Loop Until MSComm1.InBufferCount >= 20 'PLC應(yīng)該返回8個字符 固定的4+2*N
'一個起始符+一個寄存器(有兩個字節(jié))(每個字節(jié)=2字符)+一個結(jié)束符+兩個校驗(yàn)和
getdata = MSComm1.Input
Text1.Text = getdata ''返回的低字節(jié)在前,高在后
(2)Dim sendat As String,,Dim getdat
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.Output =讀取數(shù)據(jù)指令
Do
DoEvents
Loop Until MSComm1.InBufferCount >= 8
getdat = MSComm1.Input
Text4.Text = getdat ''返回的低字節(jié)在前,高在后