首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

串口发送一个特别的有关问题

2012-09-20 
串口发送一个特别的问题我想的是:首先发Y,看是否读到P,若成功,则后面发P、U、F,分别将值读出。然后,再发Y,周

串口发送一个特别的问题
我想的是:首先发Y,看是否读到P,若成功,则后面发P、U、F,分别将值读出。然后,再发Y,周而复始,
若中途或一开始发Y,读不到P,则连续发十次,再收不到则报警。
还请指点。

VB code
Dim binP As Boolean '电源通信是否握手成功,则接着发后面的..Dim Varp2, Varp3 As String '判断串口能否收到S  电源是否在线'我用了二个定时器,一个发“Y”,另一个,初始关闭,待确认电源在线后,再打开,发送后面的.Private Sub Form_Load() MyCom1 MSComm2  ‘调用串口 timer2.enabled=flase  '2号定时器不能用 Varp2 = "S"End Sub'向电源发送握手信息Private Sub Timer5_Timer()     MSComm2.Output = "Y"End Sub'串口事件Private Sub MSComm2_OnComm()   Call Sleep(50)   '延时    If MSComm2.CommEvent = 2 Then     MSComm2.RThreshold = 20     Dim i     For i = 1 To MSComm2.InBufferCount       SwichVar1 i   ’将不定长的数据一个个接起来       buffer1 = buffer1 & Hex(InByte(i))     Next i        Varp3 = HexToStr(buffer1)'将收到的转成STR        Text1.Text = Varp3   '此处是监测点  End If     If StrComp(Varp2, Varp3, vbBinaryCompare) = 0 Then  '若相同           POWER1Alarm.FillColor = QBColor(10)        ‘灯变绿            binP = True                                  '变量为真            Timer1.Enabled = False                   '关闭1号定时器            Timer6.Enabled = True                      '打开2号定时器     Else          UPS1Alarm.FillColor = QBColor(12)            binADDRS = False            Timer6.Enabled = False     End If    若不加这一段,则发送没有问题,若我后面发“P”,读出的值也就是“150”功率,此值也就不等于“S”。这时,程序就停了,好像这里有点乱。    ( If temp = 1 Then        Varp3 = HexToStr(buffer1)        Text2.Text = Varp3      '监测点二     End If  )            buffer1 = ""            MSComm2.RThreshold = 1     End Sub'向电源发送读命令Private Sub Timer6_Timer()    If binADDRS = True Then          temp = temp + 1      If temp = 1 Then            MSComm2.Output = "P"  '读功率值      ElseIf temp = 2 Then            MSComm2.Output = "U"   '读电压值      ElseIf temp = 3 Then            MSComm2.Output = "f"   '读频率值      End If         If temp > 3 Then temp = 0    End IfEnd Sub


[解决办法]
VB code
Public Status As BooleanDim Smsg(2) As StringDim msgNext As BytePrivate Sub Form_Load()MSComm1.PortOpen = TrueStatus = FalseSmsg(0) = "P"Smsg(1) = "U"Smsg(2) = "F"'msgNext = 0End SubPrivate Sub MSComm1_OnComm()    Dim tmp    Dim curr As Byte    tmp = MSComm1.Input    If tmp = "S" Then        Status = True        msgNext = 0        'Exit Sub    Else 'If Status Then            Select Case msgNext            Case 0                Text1.Text = Text1.Text & Smsg(msgNext) & " = " & (tmp)            Case 1                Text1.Text = Text1.Text & ", " & Smsg(msgNext) & " = " & (tmp)            Case 2                Text1.Text = Text1.Text & ", " & Smsg(msgNext) & " = " & (tmp) & vbCrLf        End Select        msgNext = msgNext + 1        If msgNext = 3 Then            msgNext = 0            Status = False        End If    End IfEnd SubPrivate Sub Timer1_Timer()    If Not Status Then        MSComm1.Output = "Y"        msgNext = 0    Else        Select Case msgNext            Case 0                MSComm1.Output = Smsg(msgNext)            Case 1                MSComm1.Output = Smsg(msgNext)            Case 2                MSComm1.Output = Smsg(msgNext)                'Status = False        End Select    End If    'Timer1.Enabled = FalseEnd Sub 

热点排行