基于vb和单片机的多点温度监测系统
下面是vb界面,如果需要详细的资料请加QQ354634241
工程文件
Dim senddat As Variant
Dim receivedat As Variant '用于存放接收到的温度数据
Dim receivecom As Variant '用于比较接收到的温度数据的中间变量
Dim js As Integer '定义计算接收数据字节数 Dim fs As Integer '定义计算发送数据字节数 Dim a As Integer Dim B As Integer Dim c As Integer
Private Sub Command1_Click()
If Command1.Caption = \ 连接\
On Error GoTo blak
receivedat = \初始化变量 receivecom = \ senddat = \ js = 0 fs = 0 a = 1 B = 1 c = 1
MSComm1.Settings = Combo2.Text & \& Left(Combo4.Text, 1) & \& Left(Combo5.Text, Len(Combo5.Text) - 1) '设置波特率9600bit/s,无校验,8位数据,1位停止位
MSComm1.CommPort = Right(Combo1.Text, Len(Combo1.Text) - 3) '设置串口号
MSComm1.InBufferSize = 1024 '设置接收缓冲区的大小 MSComm1.OutBufferSize = 4096
MSComm1.RThreshold = 8 '每接收到8个字符发生一次MSComm1_OnComm事件 MSComm1.SThreshold = 1
MSComm1.InputLen = 8 '设置从接收缓冲区读取的字数,为0读取整个接收缓冲区 MSComm1.InputMode = comInputModeText '以文本方式接收 MSComm1.InBufferCount = 0 '清空接收缓冲区 If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True '开串口,当打开串口时,若没有该串口,软件会报错,下面的程序将无法执行,即Command1.Caption = \断开\不会被执行,而直接跳出,执行blak下语句 End If
Command1.Caption = \ 断开\当串口连接成功后,连接按钮变为断开按钮 Set Command1.PictureNormal = LoadPicture(App.Path & \
Label15.Caption = \状态:\& \& MSComm1.CommPort & \\& MSComm1.Settings '显示当前的串口参数状态
MSComm1.Output = Str(7) '向单片机发送“1”,表示请求单片机下一帧数据 Exit Sub '如果串口设备出错,则执行blak下面的语句 blak:
MsgBox \串口不存在或被占用\提示信息\ Else
Command1.Caption = \ 连接\
Set Command1.PictureNormal = LoadPicture(App.Path & \ If MSComm1.PortOpen = True Then MSComm1.PortOpen = False '关闭串口 End If
Label15.Caption = \状态:\未连接\
End If End Sub
Private Sub Command2_Click() If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False '关串口 End If End End Sub
Private Sub Form_Load()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False '关串口 End If
WindowsMediaPlayer1.URL = App.Path & \启动.wav\ '播放软件启动音乐 End Sub
Private Sub MSComm1_OnComm()
Dim pjz As Double '用于存储三个采集点的平均温度值 Select Case MSComm1.CommEvent
Case comEvReceive '说明收到8个字符
receivedat = MSComm1.Input '读出接收缓冲区中的数据
' 为保证后面asc函数的参数不出现空字节,接收到数据后首先就进行数据字节数的判断,满足8位则执行下面操作
If Left(receivedat, 1) = \And Right(receivedat, 1) = \And Len(receivedat) = 8 And receivecom1 <> receivedat Then
'***出现一个及其不容易发现的错误:当产生comm事件,但是由于机器卡原因,接收缓冲区中未收到数据,即空字符,asc(a)的参数不能为空,则出现实时错误'5',无效的过程调用和参数。
'若本次收到数据与上次数据一样,那么不执行循环,直接清空接收缓冲区,准备下一次接收
receivecom = receivedat
Text1.Text = \每收到8个字节数据将清空text1的内容
Text1.Text = Text1.Text & Asc(Mid(receivecom, 2, 1)) '将收到数据最高位转换成ascii码数值,并加入到text1中
Text1.Text = Text1.Text & \
Text1.Text = Text1.Text & Asc(Mid(receivecom, 3, 1)) '将收到数据第二位转换成ascii码数值,并加入到text1
'Asc(a)函数功能:将ascii码a转换成ascii码所对应的十进制整数 Text2.Text = \每收到8个字节数据将清空text1的内容
Text2.Text = Text2.Text & Asc(Mid(receivecom, 4, 1)) '将收到数据最高位转换成ascii码数值,并加入到text1中
Text2.Text = Text2.Text & \
Text2.Text = Text2.Text & Asc(Mid(receivecom, 5, 1)) '将收到数据第二位转换成ascii码数值,并加入到text1
Text3.Text = \每收到8个字节数据将清空text1的内容
Text3.Text = Text3.Text & Asc(Mid(receivecom, 6, 1)) '将收到数据最高位转换成ascii码数值,并加入到text1中
Text3.Text = Text3.Text & \
Text3.Text = Text3.Text & Asc(Mid(receivecom, 7, 1)) '将收到数据第二位转换成ascii码数值,并加入到text1
If (Val(Text1.Text) < Val(Text8.Text)) Then '判断采集点1温度是否超出设定范围 Image2.Picture = LoadPicture(App.Path & \ a = 0
ElseIf Val(Text1.Text) > Val(Text11.Text) Then
Image2.Picture = LoadPicture(App.Path & \ a = 0 Else
Image2.Picture = LoadPicture(App.Path & \ a = 1 End If
If Val(Text2.Text) < Val(Text9.Text) Then '判断采集点2温度是否超出设定范围 Image3.Picture = LoadPicture(App.Path & \ B = 0
ElseIf Val(Text2.Text) > Val(Text12.Text) Then
Image3.Picture = LoadPicture(App.Path & \ B = 0 Else
Image3.Picture = LoadPicture(App.Path & \ B = 1 End If
If Val(Text3.Text) < Val(Text10.Text) Then '判断采集点3温度是否超出设定范围 Image4.Picture = LoadPicture(App.Path & \ c = 0
ElseIf Val(Text3.Text) > Val(Text13.Text) Then
Image4.Picture = LoadPicture(App.Path & \ c = 0 Else
Image4.Picture = LoadPicture(App.Path & \ c = 1 End If
'*************等待每次采集完3个采集点数据后再计算平均温度,并比较*************
pjz = (Val(Text1.Text) + Val(Text2.Text) + Val(Text3.Text)) / 3 Text6.Text = Int(pjz * 1000) / 1000 '将平均值取小数点3位 If pjz > Val(Text5.Text) Then
Image1.Picture = LoadPicture(App.Path & \ Label19.Caption = \警告:当前环境温度过高\
WindowsMediaPlayer1.URL = App.Path & \警报音效.wav\ '当温度过高时播放警报音
ElseIf pjz < Val(Text4.Text) Then
Image1.Picture = LoadPicture(App.Path & \ Label19.Caption = \警告:当前环境温度过低\
WindowsMediaPlayer1.URL = App.Path & \警报音效.wav\ '当温度过低时播放警报音 Else
Image1.Picture = LoadPicture(App.Path & \ Label19.Caption = \提示:当前环境温度适宜\ End If
'*************等待每次采集完3个采集点数据后再计算平均温度,并比较************* End If
MSComm1.InBufferCount = 0 '清空接收缓冲区
'*************程序到此已经将收到的数据处理完毕***************** js = js + 1 '接收帧数计算 If (js > 65000) Then js = 1 End If
Label27.Caption = Str(js) & \帧\ '显示接收的帧数 fs = fs + 1 '发送字节数计算 If (fs > 65000) Then fs = 1 End If
Label26.Caption = Str(fs) & \字节\
'*********************************待发送数据判断 If a = 0 And B = 0 And c = 0 Then '3个都不适宜 senddat = Str(0) End If
If a = 0 And B = 0 And c = 1 Then '12不适宜,3适宜 senddat = Str(1) End If
If a = 0 And B = 1 And c = 0 Then '13不适宜,2适宜 senddat = Str(2) End If
If a = 0 And B = 1 And c = 1 Then '1不适宜,23适宜 senddat = Str(3) End If
If a = 1 And B = 0 And c = 0 Then '1适宜,23不适宜 senddat = Str(4) End If
If a = 1 And B = 0 And c = 1 Then '13适宜,2不适宜 senddat = Str(5) End If
If a = 1 And B = 1 And c = 0 Then '12适宜,3不适宜 senddat = Str(6) End If
If a = 1 And B = 1 And c = 1 Then '123适宜 senddat = Str(7) End If
'***********************************待发送数据判断 Timer1.Enabled = True '开定时器
End Select End Sub
Private Sub Text7_Change()
Timer1.Interval = Val(Text7.Text) End Sub
Private Sub Timer1_Timer()
If MSComm1.PortOpen = True Then
MSComm1.Output = senddat '向单片机发送“t”,表示请求单片机下一帧数据 End If
Timer1.Enabled = False '关定时器 End Sub
下面是C程序,如果需要详细的资料请加QQ354634241
搜索“diyifanwen.net”或“第一范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,第一范文网,提供最新高中教育基于VB和51单片机的多点温度监测系统(毕业设计) 全文阅读和word下载服务。
相关推荐: