I want to know how will I display the reading of a sensor on the PC using VB6? I used the mscomm32.ocx. Well I have a module witch is connected to the PC thruogh serial interface. The measured data measured by a sensor(temperature sensor) must be desplayed on the pc. My problem is I cannot display readings of the sensor even if the module and the PC are communicating.
here is a code I made in vb6.
CODE
Option Explicit
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMillisecond As
Long)
Dim serialdata As String
Dim mode As Integer
Dim T_P, C_F As Boolean
Const read_T = &H31
Const read_P = &H32
Const read_TP = &H33
Const cal_T = &H34
Const led_off = &H35
Const led_on = &H36
Const mode_1 = 1
Const mode_2 = 2
Const mode_3 = 3
Const mode_4 = 4
Private Sub cmdCalibrateT_Click()
Timer1.Enabled = True
MSComm1.RThreshold = 1
MSComm1.InputLen = 1
mode = mode_4
lblcalibinfo.Caption = " "
End Sub
Private Sub cmdDegC_Click()
C_F = True
End Sub
Private Sub cmdDegF_Click()
C_F = False
End Sub
Private Sub cmdErase_Click()
On Error GoTo nxt
With adoTP.Recordset
.MoveFirst
Do While Not .EOF
If Not (.BOF Or .EOF) Then
.Delete
.MoveFirst
End If
Loop
End With
nxt:
End Sub
Private Sub cmdLEDOff_Click()
MSComm1.Output = Chr(led_off)
End Sub
Private Sub cmdLEDOn_Click()
MSComm1.Output = Chr(led_on)
End Sub
Private Sub cmdReadP_Click()
Timer1.Enabled = True
MSComm1.RThreshold = 1
MSComm1.InputLen = 1
mode = mode_2
End Sub
Private Sub cmdReadT_Click()
Timer1.Enabled = True
MSComm1.RThreshold = 1
MSComm1.InputLen = 1
mode = mode_1
End Sub
Private Sub cmdReadTP_Click()
Timer1.Enabled = True
MSComm1.RThreshold = 2
MSComm1.InputLen = 2
mode = mode_3
End Sub
Private Sub cmdStop_Click()
Timer1.Enabled = False
End Sub
Private Sub Form_Load()
MSComm1.CommPort = "1"
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim serdata As String
If MSComm1.CommEvent = 2 Then
serialdata = MSComm1.Input
serdata = Hex(Asc(serialdata))
If mode = mode_1 Then
If Hex(Asc(serialdata)) = "FF" Then
lblcalibinfo.Caption = "Detached Temp Sensor"
txtTempValue = "0"
Else
lblcalibinfo.Caption = "Attached Temp Sensor"
If C_F = False Then
txtTempValue = Format(Asc(serialdata) * 100 / 255 +
7.4, "###0.00")
Else
txtTempValue = Format(((Asc(serialdata) * 100 / 255
+ 7.4) * 9 / 5) + 32, "###0.00")
End If
End If
Call LogT
ElseIf mode = mode_2 Then
If Hex(Asc(serialdata)) = "FF" Then
lblcalibinfo.Caption = "Detached Pressure Sensor"
txtPressValue = "0"
Else
lblcalibinfo.Caption = "Attached Pressure Sensor"
txtPressValue = Format(Asc(serialdata) * 100 / 255,
"###0.00")
End If
Call LogP
ElseIf mode = mode_3 Then
txtTempValue = Format(Asc(Mid(serialdata, 1, 1)) * 100 /
255 + 7.4, "###0.00")
txtPressValue = Format(Asc(Mid(serialdata, 2, 1)) * 100 /
255, "###0.00")
Call LogTP
ElseIf mode = mode_4 Then
If serialdata = "F" Then
lblcalibinfo.Caption = "Temp sensor calibrated."
End If
End If
End If
End Sub
Private Sub LogTP()
If chkLog.Value = 1 Then
With adoTP.Recordset
.AddNew
!date_time = Now
!temperature = txtTempValue
!unit_T = "°C"
!pressure = txtPressValue
!unit_P = "kPascal"
.Update
End With
End If
End Sub
Private Sub LogT()
If chkLog.Value = 1 Then
With adoTP.Recordset
.AddNew
!date_time = Now
!temperature = txtTempValue
!unit_T = "°C"
!pressure = " "
!unit_P = " "
.Update
End With
End If
End Sub
Private Sub LogP()
If chkLog.Value = 1 Then
With adoTP.Recordset
.AddNew
!date_time = Now
!temperature = " "
!unit_T = " "
!pressure = txtPressValue
!unit_P = "kPascal"
.Update
End With
End If
End Sub
Private Sub Timer1_Timer()
If CInt(txtSamplingInterval) < 10 Then
txtSamplingInterval = 10
Timer1.Interval = 10
Else
Timer1.Interval = txtSamplingInterval
End If
If mode = mode_1 Then
MSComm1.Output = Chr(read_T)
ElseIf mode = mode_2 Then
MSComm1.Output = Chr(read_P)
ElseIf mode = mode_3 Then
MSComm1.Output = Chr(read_TP)
ElseIf mode = mode_4 Then
MSComm1.Output = Chr(cal_T)
lblcalibinfo.Caption = "Temp sensor calibrated."
End If
End Sub