📄 recvandsend.vb
字号:
Module RecvAndSend
Public Function SendRecv(ByVal MCUno As Byte) As String
'Dim i As Integer
'ReDim G_RecvData(13)
'For i = 0 To 13
' G_RecvData(i) = 5 * i
'Next i
'HandleData(MCUno, G_RecvData)
Dim SendData(4) As Byte '写数据帧需要4字节,所以读数据帧后添2字节0
Dim DelayTime As Integer
Dim start As Long
Static data(3) As Integer
Dim tmp0, tmp1, tmp2, tmp3 As Integer
REM 发给1号MCU的读请求
Form1.frmMain.MSComm1.InBufferCount = 0 '清接收缓冲区
Form1.frmMain.MSComm1.OutBufferCount = 0 '清发送缓冲区
REM MCU号+130,避免与发送字母相重
SendData(0) = 130 + MCUno
SendData(1) = CByte(Asc("R"))
SendData(2) = 0
SendData(3) = 0
SendData(4) = 69 '帧尾'E'=69
Form1.frmMain.MSComm1.Output = SendData
SCISendN(Form1.frmMain.MSComm1, SendData)
If SCIRecvN(Form1.frmMain.MSComm1, 13, 2) Then
Form1.frmMain.StateTxt.Text = "接收" & MCUno & "#正常!"
Else
Form1.frmMain.StateTxt.Text = "接收" & MCUno & "#异常!"
GoTo SendRecv_Exit
End If
'REM 等待接收MCU发来的8字节数据,等1秒接收不到,再发一次请求等2秒
'DelayTime = 2
'start = Microsoft.VisualBasic.DateAndTime.Timer '获取系统当前时间
'Do
' Application.DoEvents()
' REM 防止出现接收异常后循环等待,以至于死机,所以在规定时间DelayTime内没收到则报错并退出
' If Microsoft.VisualBasic.DateAndTime.Timer - start > DelayTime Then
' Form1.frmMain.StateTxt.Text = "接收" & MCUno & "#出现异常!"
' Return "异常"
' End If
'Loop Until Form1.frmMain.MSComm1.InBufferCount >= 13
'RecvData = Form1.frmMain.MSComm1.Input '接收9字节数据
'Form1.frmMain.MSComm1.InBufferCount = 0 '清接收缓冲区
'Form1.frmMain.StateTxt.Text = "接收" & MCUno & "#正常!"
REM 如果接收到的帧是以'S'开始,'E'结束就继续操作,否则放弃此帧数据
If G_RecvData(0) = 83 And G_RecvData(12) = 69 Then '帧头:'S'=83 帧尾:'E'=69
REM 将接收到的数据显示
HandleData(MCUno, G_RecvData)
REM 将字节型数据整合成整型
' tmp0 = G_RecvData(1) * 256 + G_RecvData(2) '当前温度
tmp0 = 160
tmp1 = G_RecvData(3) * 256 + G_RecvData(4) '设定温度
tmp2 = G_RecvData(5) * 256 + G_RecvData(6) '当前时间
tmp3 = G_RecvData(7) * 256 + G_RecvData(8) '设定时间
REM 入库
'If tmp0 <> data(0) Or tmp1 <> data(1) Or tmp2 <> data(2) Or tmp3 <> data(3) Then '开始计时后每分钟都要存了
If tmp0 <> data(0) Or tmp1 <> data(1) Or tmp3 <> data(3) Then '只有当当前温度、设定温度 或设定时间有变化时才写数据库
InDB(MCUno, G_RecvData)
End If
REM 更新"上一次"数据
data(0) = tmp0
data(1) = tmp1
data(2) = tmp2
data(3) = tmp3
Else
Return "帧错误"
End If
SendRecv_Exit:
End Function
Public Sub HandleData(ByVal MCUno As Byte, ByVal data() As Byte)
Dim i, j As Integer
Dim Time1, Time2, Time3, Time4 As Integer
Dim CurrentTim1, CurrentTim2, CurrentTim3, CurrentTim4 As Integer
Dim SetTemp1, SetTemp2, SetTemp3, SetTemp4 As Integer
REM 根据MCU号在相应的picturebox中动态显示,类似示波器
Select Case MCUno
Case 1
REM 状态左移,最后一个状态更新
For i = 0 To 23
Form1.frmMain.SecondSet1(i) = Form1.frmMain.SecondSet1(i + 1)
Form1.frmMain.CurrentTmpSet1(i) = Form1.frmMain.CurrentTmpSet1(i + 1)
Next i
REM 更新X轴最后一个坐标
Form1.frmMain.SecondSet1(24) = data(9)
REM 更新Y轴最后一个坐标
Form1.frmMain.CurrentTmpSet1(24) = data(1) * 256 + data(2) ' 120 + j
' j = j + 1
REM 设定温度、当前时间、设定时间的显示
SetTemp1 = data(3) * 256 + data(4)
CurrentTim1 = data(5) * 256 + data(6)
Time1 = data(7) * 256 + data(8)
REM 坐标显示,模拟示波器
Coordinate(MCUno, Form1.frmMain.PictureBox1, Form1.frmMain.SecondSet1, Form1.frmMain.CurrentTmpSet1, Color.YellowGreen, Time1, CurrentTim1, SetTemp1) '显示1#的数据
Case 2
REM 状态左移,最后一个状态更新
For i = 0 To 23
Form1.frmMain.SecondSet2(i) = Form1.frmMain.SecondSet2(i + 1)
Next i
REM 更新X轴最后一个坐标
Form1.frmMain.SecondSet2(24) = data(9)
REM 设定温度、当前时间、设定时间的显示
SetTemp2 = data(3) * 256 + data(4)
CurrentTim2 = data(5) * 256 + data(6)
Time2 = data(7) * 256 + data(8)
REM 坐标显示,模拟示波器
Coordinate(MCUno, Form1.frmMain.PictureBox1, Form1.frmMain.SecondSet2, Form1.frmMain.CurrentTmpSet2, Color.YellowGreen, Time2, CurrentTim2, SetTemp2) '显示2#的数据
Case 3
REM 状态左移,最后一个状态更新
For i = 0 To 23
Form1.frmMain.SecondSet3(i) = Form1.frmMain.SecondSet3(i + 1)
Next i
REM 更新X轴最后一个坐标
Form1.frmMain.SecondSet3(24) = data(9)
REM 设定温度、当前时间、设定时间的显示
SetTemp3 = data(3) * 256 + data(4)
CurrentTim3 = data(5) * 256 + data(6)
Time3 = data(7) * 256 + data(8)
REM 坐标显示,模拟示波器
Coordinate(MCUno, Form1.frmMain.PictureBox1, Form1.frmMain.SecondSet3, Form1.frmMain.CurrentTmpSet3, Color.YellowGreen, Time3, CurrentTim3, SetTemp3) '显示3#的数据
Case 4
REM 状态左移,最后一个状态更新
For i = 0 To 23
Form1.frmMain.SecondSet4(i) = Form1.frmMain.SecondSet4(i + 1)
Next i
REM 更新X轴最后一个坐标
Form1.frmMain.SecondSet4(24) = data(9)
REM Y轴显示的最大值
REM 当前时间、设定时间的显示
SetTemp4 = data(3) * 256 + data(4)
CurrentTim4 = data(5) * 256 + data(6)
Time4 = data(7) * 256 + data(8)
REM 坐标显示,模拟示波器
Coordinate(MCUno, Form1.frmMain.PictureBox1, Form1.frmMain.SecondSet4, Form1.frmMain.CurrentTmpSet4, Color.YellowGreen, Time4, CurrentTim4, SetTemp4) '显示4#的数据
End Select
End Sub
Public Sub SendAdjust(ByVal MCUno As Byte, ByVal who As Byte, ByVal data As Int16)
Dim senddata(4) As Byte
Dim RecvData() As Byte
Dim DelayTime As Integer
Dim start As Long
Form1.frmMain.MSComm1.OutBufferCount = 0 '清发送缓冲区
senddata(0) = 130 + MCUno
senddata(1) = CByte(Asc("T")) '写标志
senddata(2) = Int(data / 256) '如果只是data/256就错
senddata(3) = data Mod 256
senddata(4) = 69 '帧尾'E'=69
Form1.frmMain.MSComm1.Output = senddata
REM 等待接收MCU发来的8字节数据
DelayTime = 1
start = Microsoft.VisualBasic.DateAndTime.Timer '获取系统当前时间
Do
Application.DoEvents()
REM 防止出现接收异常后循环等待,以至于死机,所以在规定时间DelayTime内没收到则报错并退出
If Microsoft.VisualBasic.DateAndTime.Timer - start > DelayTime Then
Form1.frmMain.StateTxt.Text = "接收" & MCUno & "#出现异常!"
Return
' GoTo SendRecv_Exit
End If
Loop Until Form1.frmMain.MSComm1.InBufferCount = 1
RecvData = Form1.frmMain.MSComm1.Input '接收8字节数据
If RecvData(0) = 86 Then
MsgBox("校准成功!")
Else
MsgBox("请重新校准!")
End If
Form1.frmMain.MSComm1.InBufferCount = 0 '清接收缓冲区
End Sub
Public Sub SendMCUno(ByVal MCUno As Byte, ByVal Newno As Byte)
Dim senddata(4) As Byte
Dim RecvData() As Byte
Dim DelayTime As Integer
Dim start As Long
Form1.frmMain.MSComm1.OutBufferCount = 0 '清发送缓冲区
senddata(0) = 130 + MCUno
senddata(1) = CByte(Asc("M")) '写标志
senddata(2) = 0
senddata(3) = 130 + Newno
senddata(4) = 69 '帧尾'E'=69
Form1.frmMain.MSComm1.Output = senddata
REM 等待接收MCU发来的8字节数据
DelayTime = 1
start = Microsoft.VisualBasic.DateAndTime.Timer '获取系统当前时间
Do
Application.DoEvents()
REM 防止出现接收异常后循环等待,以至于死机,所以在规定时间DelayTime内没收到则报错并退出
If Microsoft.VisualBasic.DateAndTime.Timer - start > DelayTime Then
Form1.frmMain.StateTxt.Text = "接收" & MCUno & "#出现异常!"
Return
' GoTo SendRecv_Exit
End If
Loop Until Form1.frmMain.MSComm1.InBufferCount = 1
RecvData = Form1.frmMain.MSComm1.Input '接收8字节数据
If RecvData(0) = 86 Then
MsgBox("修改成功!")
Else
MsgBox("请重新修改!")
End If
End Sub
End Module
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -