单片机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

VB串口自动读取ds18b20温度

已有 647 次阅读2016-2-4 03:54

Dim flag As Integer
Function ByteArrayToHexString(ABuffer As Variant) As String
Dim iLoop As Long
ByteArrayToHexString = " "
For iLoop = LBound(ABuffer) To UBound(ABuffer)
ByteArrayToHexString = ByteArrayToHexString & Right("0 " & Hex(ABuffer(iLoop)), 2) & " "
Next iLoop
ByteArrayToHexString = Trim(ByteArrayToHexString)
End Function

Private Sub Command1_Click()
If Command1.Caption = "接收温度" Then
Command1.Caption = "停止接受"
Timer1.Enabled = True
Else
Command1.Caption = "接收温度"
Timer1.Enabled = False
End If
End Sub

Private Sub Command2_Click()
Dim Temp(0) As Byte
Dim strBuff As String
Temp(0) = &H55
MSComm1.Output = Temp '发送一个16进制
End Sub

Private Sub Command3_Click()
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
Text1.Text = ""
End Sub

Private Sub Form_Load()
MSComm1.CommPort = 3
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeBinary '采用二进制传输
MSComm1.InBufferCount = 0 '清空接受缓冲区
MSComm1.OutBufferCount = 0 '清空传输缓冲区
MSComm1.RThreshold = 1 '产生MSComm事件
MSComm1.PortOpen = True '打开端口
End Sub

Private Sub MSComm1_OnComm()
Dim ABuffer() As Byte
MSComm1.InputMode = comInputModeBinary
If MSComm1.InBufferCount > 0 Then
ABuffer = MSComm1.Input
Text1 = Text1 & Val("&h" & ByteArrayToHexString(ABuffer)) & " "


End If

End Sub

Private Sub Timer1_Timer()
Call Command2_Click '定时调用手动发送
End Sub


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

QQ|手机版|小黑屋|单片机论坛 |51hei电子论坛2群 联系QQ:125739409;技术交流QQ群219535678

Powered by 单片机教程网

返回顶部