微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 基于RS-485总线的温湿度监测系统——(RS485总线系统应用之5)

基于RS-485总线的温湿度监测系统——(RS485总线系统应用之5)

时间:12-15 来源:互联网 点击:

.InBufferCount = 0’清空接收缓冲区

.OutBufferCount = 0’清空发送缓冲区

.SThreshold = 8’设置成发送8个字节就产生OnComm事件

.RThreshold = 5 + 2 * 2’设置成接收9个字节就产生OnComm事件

.InBufferSize = 1024

.OutBufferSize = 1024

.PortOpen = True

Else

MsgBox "串口已经打开"

End If

End With

Timer1.Interval = 1000

Timer1.Enabled = True

End Sub

Private Sub Timer1_Timer()’定时读各温湿度表数据

Timer1.Enabled = False

Dim tbisend(7) As Byte

Dim strTempFile As String, m As Integer

Dim conn As New ADODB.Connection

If MSComm1.PortOpen = True Then

For k = 1 To Rnumber

ii = k

tbisend(0) = "&h" + Hex(k)’被呼叫子机的地址码

tbisend(1) = "&h" + Hex(4)’4是读寄存器的功能码

tbisend(2) = "&h" + Hex(0)’被读寄存器的起始地址高字节

tbisend(3) = "&h" + Hex(0)’被读寄存器的起始地址低字节

tbisend(4) = "&h" + Hex(0)’一次读寄存器的个数的高字节

tbisend(5) = "&h" + Hex(2)’一次读寄存器的个数的低字节

Crc = CRC16(tbisend, 6, btLoCRC, btHiCRC)

tbisend(6) = "&h" + Hex(btLoCRC)’CRC低位

tbisend(7) = "&h" + Hex(btHiCRC)’CRC高位

If MSComm1.PortOpen = False Then MSComm1.PortOpen = True

MSComm1.Output = tbisend’发送数据

t1 = timeGetTime

While timeGetTime < t1 + 40’延时的循环等待(延时40毫秒,控制精度1毫秒)

DoEvents’以便接收到被呼叫的从机发来的数据

Wend

MorphDisplay1(k - 1).Value = TemperatureData(k)

MorphDisplay2(k - 1).Value = HumidityData(k)

Next k

IntMinute2 = Val(Right(Time, 2))

If IntMinute2 < IntMinute1 Then

WriteTval = WriteTval – 1

If WriteTval = 0 Then

Data1.Refresh

If Data1.Recordset.EOF = False Then Data1.Recordset.MoveLast

If conn.State = adStateOpen Then conn.Close’向温湿度数据库文件写入测量数据

conn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & WPath1 & WFilename1 & WFilename2 & ".mdb; Mode = ReadWrite; Persist Security Info = False"

conn.Open

s1 = "Insert Into温湿度数据(日期,时间,1温,1湿,2温,2湿,3温,3湿,4温,4湿,5温,5湿,"

s2 = "6温,6湿,7温,7湿,8温,8湿,9温,9湿,10温,10湿) Values "

s3 = "(#" & Date & "# ,#" & Time & "#," & TemperatureData(1) & "," & HumidityData(1) & ","

s4 = "" & TemperatureData(2) & "," & HumidityData(2) & "," & TemperatureData(3) & ","

s5 = "" & HumidityData(3) & "," & TemperatureData(4) & "," & HumidityData(4) & ","

s6 = "" & TemperatureData(5) & "," & HumidityData(5) & "," & TemperatureData(6) & ","

s7 = "" & HumidityData(6) & "," & TemperatureData(7) & "," & HumidityData(7) & ","

s8 = "" & TemperatureData(8) & "," & HumidityData(8) & "," & TemperatureData(9) & ","

s9 = "" & HumidityData(9) & "," & TemperatureData(10) & "," & HumidityData(10) & ")"

conn.Execute s1 & s2 & s3 & s4 & s5 & s6 & s7 & s8 & s9

conn.Close

End If

WriteTval = Val(WriteT)

End If

End If

End If

Timer1.Interval = ReadTval

Timer1.Enabled = True

End Sub

Private Sub MSComm1_OnComm()’接收各温湿度表数据程序

Dim TemperatureData6 As String, HumidityData6 As String

Dim INByte() As Byte

If MSComm1.CommEvent = comEvReceive Then’如有接收事件发生则......

INByte = MSComm1.Input

If INByte(0) = ii And INByte(1) = 4 Then’如果收到的地址码=被叫从机并且功能码=读寄存器,

’则将收到的CRC码与收到数据计算出的CRC码比较

Crc = CRC16(INByte, UBound(INByte) - LBound(INByte) - 1, btLoCRC, btHiCRC)

If INByte(UBound(INByte) - 1) = btLoCRC And INByte(UBound(INByte)) = btHiCRC Then ’如校验正确则转换

TemperatureData6 = Hex(INByte(3)) & Format(Hex(INByte(4)), "00")’先转换成十六进制

HumidityData6 = Hex(INByte(5)) & Format(Hex(INByte(6)), "00")

TemperatureData(ii) = Format(Val("&H" & TemperatureData6) / 10, "##0.0")’再转换为十进制

HumidityData(ii) = Format(Val("&H" & HumidityData6) / 10, "##0.0")

End If

End If

MSComm1.InBufferCount = 0’清接收缓存

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top