VB读取智能温湿度显示不变化
本帖最后由 bcrun 于 2012-08-20 19:58:25 编辑 小弟学编了一个VB显示存储温湿度的程序,能读,也能写入ACCESS表中,每当温湿度计送过来信息时,能读进表中,但数值永远是第一次的,用串口助手读看出,每次送过来的是不一样的值。
附上程序请高手指点:
'串口模块
Public Sub MyCom(MSComm1 As MSComm)
On Error GoTo ErrHandle
With MSComm1
.CommPort = 3
'使用COM3
.Settings = "9600,N,8,1" '设置通信口参数
.InBufferSize = 60 '设置MSComm1接收缓冲区为40字节
.OutBufferSize = 2 '设置MSComm1发送缓冲区为2字节
.InputMode = comInputModeBinary '设置接收数据模式为文本式
.InputLen = 1 '设置Input 一次从接收缓冲读取字节数为1
.SThreshold = 1 '设置Output 一次从发送缓冲读取字节数为1
.RThreshold = 1 '接收缓冲区每收到一个,触发
.InBufferCount = 0 '清除接收缓冲区
.OutBufferCount = 0 '清除发送缓冲区
If .PortOpen = False Then
.PortOpen = True
End If
End With
Exit Sub
ErrHandle:
MsgBox "通讯口打不开,请检查"
End Sub
'这是向温度计发送读的命令
Public Sub mySend(bytHex() As Byte)
ReDim bytHex(7) As Byte
ReDim bytHex(7)
bytHex(0) = &H1
bytHex(1) = &H4
bytHex(2) = &H0
bytHex(3) = &H0
bytHex(4) = &H0
bytHex(5) = &H2
bytHex(6) = &H71
bytHex(7) = &HFB
End Sub
Dim datatempA As Single '这是采样温湿度值
Dim datatempB As Single '这是采样温湿度值
Dim num As Integer '这是采样温湿度个数
Dim bytHex() As Byte
Dim InByte(1000) As Byte '串口接收数据用
Dim buffer As String
Dim Varp As Variant
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long) '-----Sleep函数 延时函数
'将收到的内容串联出来,因为COM口是接到一个触发一次
Private Sub SwichVar(ByVal nNum As Integer)
Sleep (10)
Varp = Null
Varp = MSComm1.Input
InByte(nNum) = Varp(0)
End Sub
Private Sub Form_Load()
'这是调用串口
MyCom MSComm1
'使时钟有效
Timer3.Enabled = True
End Sub
Private Sub MSComm1_OnComm()
Dim datatemp1, datatemp2 As String
Call Sleep(50)
If MSComm1.CommEvent = 2 Then
MSComm1.RThreshold = 0
Dim i
For i = 1 To MSComm1.InBufferCount
SwichVar i
buffer = buffer & Right("00" & Hex(InByte(i)), 2) & Chr(32)
Next i
Text1.Text=buffer '在这能看到接到的数据
datatemp1 = Mid(buffer, 10, 5)
datatempA = Val("&H" & datatemp1)
TempText.Text = (datatempA - 4000) / 100
datatemp2 = Mid(buffer, 16, 5)
datatempB = Val("&H" & datatemp2)
AverText.Text = datatempB / 100
Adodc3.ConnectionString = cnn.ConnectionString
Adodc3.RecordSource = "select * from Table_temp"
Adodc3.Refresh
If Adodc3.Recordset.RecordCount > 0 Then
Adodc3.Recordset.MoveLast
End If
cnn.Execute ("insert into Table_temp (温度值,湿度值)values('" & TempText & "','" & AverText & "')")
MSComm1.RThreshold = 1
End If
End Sub