Mode:  
 Search
Wednesday, March 10, 2010..:: Home » ComReader::..Register  Login
 ComReader Minimize

It took me few hours to slap together this simple VB6 test code to read DS18S20 through IOM#142A made by Peter H. Anderson. The fastest reliable reading it can do is 1 readings every 2 seconds. For slower readings increase the MEASUREMENT_INTERVAL constant (in milliseconds).

The code is using MSComm control & Timer control. The main work is done by the clsComReader class:

Option Explicit

' clsComReader
'
' 1. instantiate
' 2. set timer & (optional) Interval
' 3. set comm (that will start the first measurement)
'
' 4. handle GotData or read Measurement & MeasurementTime
'
' does one measurement only when interval not set or set to 0
' you can force next measurement calling RequestMeasurement
' use GotData callback or wait for MeasurementTime <> "" to get the first value
'

Public Event GotData()

Private Const MEASUREMENT_DELAY = 1400

Private buffer As Variant
Private mTemperature As String
Private mMeasurementTime As Date
Private mInterval As Long ' milliseconds
Private mSendRequest As Boolean

Private WithEvents oTimer As Timer
Private WithEvents oComm As MSComm


Public Property Set Timer(ByVal Value As Timer)
    Set oTimer = Value
End Property


Public Property Set CommControl(ByVal Value As MSComm)
   
    Set oComm = Value
    ' 9600 baud, no parity, 8 data, and 1 stop bit.
    oComm.Settings = "9600,N,8,1"
    ' Tell the control to read entire buffer when Input is used.
    oComm.InputLen = 0
    oComm.DTREnable = False
    oComm.PortOpen = True
    RequestMeasurement

End Property


Public Property Let Interval(ByVal Value As Long)
   
    If Value > 0 Then
        mInterval = Value - MEASUREMENT_DELAY
        If mInterval < 0 Then
            mInterval = 10
        End If
    Else
        mInterval = 0
    End If

End Property


Public Property Get Interval() As Long
   
    If mInterval > 0 Then
        Interval = mInterval + MEASUREMENT_DELAY
    Else
        Interval = 0
    End If

End Property


Public Property Get Measurement() As String
   
    Measurement = mTemperature

End Property


Public Property Get MeasurementTime() As String
   
    MeasurementTime = mMeasurementTime

End Property


Public Sub RequestMeasurement()

    oTimer.Interval = MEASUREMENT_DELAY
    oComm.Output = buffer
    mSendRequest = False

End Sub


Private Sub Class_Initialize()

    buffer = "D0" & vbCr
    mInterval = 0
    mSendRequest = False

End Sub


Private Sub oTimer_Timer()

    If mSendRequest Then
        RequestMeasurement
    Else
        oTimer.Interval = mInterval
        mMeasurementTime = Now()
        mTemperature = oComm.Input
        mSendRequest = True
        RaiseEvent GotData
    End If

End Sub


Private Sub oComm_OnComm()
   
    Select Case oComm.CommEvent
   ' Handle each event or error by placing
   ' code below each case statement

   ' Errors
      Case comEventBreak   ' A Break was received.
'        Me.Label1.Caption = "br"
      Case comEventFrame   ' Framing Error
'        Me.Label1.Caption = "fr"
      Case comEventOverrun   ' Data Lost.
'        Me.Label1.Caption = "ov"
      Case comEventRxOver   ' Receive buffer overflow.
'        Me.Label1.Caption = "rx"
      Case comEventRxParity   ' Parity Error.
'        Me.Label1.Caption = "pa"
      Case comEventTxFull   ' Transmit buffer full.
'        Me.Label1.Caption = "full"
      Case comEventDCB   ' Unexpected error retrieving DCB]
'        Me.Label1.Caption = "dcb"

   ' Events
      Case comEvCD   ' Change in the CD line.
''        Me.Label1.Caption = "cd " & MSComm1.Input
      Case comEvCTS   ' Change in the CTS line.
''        Me.Label1.Caption = "cts " & MSComm1.Input
      Case comEvDSR   ' Change in the DSR line.
''        Me.Label1.Caption = "dts"
      Case comEvRing   ' Change in the Ring Indicator.
''        Me.Label1.Caption = "ring"
      Case comEvReceive   ' Received RThreshold # of
                        ' chars.
 '       Me.Label1.Caption = "R " & MSComm1.Input
      Case comEvSend   ' There are SThreshold number of
                     ' characters in the transmit
                     ' buffer.
 '       Me.Label1.Caption = "send"
      Case comEvEOF   ' An EOF charater was found in
                     ' the input stream
 '       Me.Label1.Caption = "E " & MSComm1.Input
   End Select

End Sub


Private Sub Class_Terminate()

    oTimer.Interval = 0
    oComm.PortOpen = False
    Set oTimer = Nothing
    Set oComm = Nothing

End Sub

For whatever reason, the onComm event is invoked only when the IOM is not receiving & need to be reset. Therefore I commented it out & so I can research it later.

There is one form with one large label Label1, timer control Timer1 and communication control MSComm1 with the code:

Option Explicit

Private Const MEASUREMENT_INTERVAL = 2000 ' 2 seconds

Private WithEvents oComReader As clsComReader


Private Sub Form_Load()

    Me.Label1.Caption = ""

    Set oComReader = New clsComReader
    Set oComReader.Timer = Me.Timer1
    oComReader.Interval = MEASUREMENT_INTERVAL
    Set oComReader.CommControl = Me.MSComm1

End Sub


Private Sub oComReader_GotData()
   
    Label1.Caption = oComReader.MeasurementTime & vbCr & oComReader.Measurement

End Sub


Private Sub Form_Unload(Cancel As Integer)
   
    Set oComReader = Nothing

End Sub

The next version will separate the device number & measured value from the response & it should enable more devices on 1-Wire line. I am still waiting for additional DS1820 to try that...


 Print   
 Smart HVAC Site Feedback Minimize






Enter the code shown above in the box below
Send

 

 Print   
Copyright 2007 by BSP Software   Terms Of Use  Privacy Statement
DotNetNuke® is copyright 2002-2010 by DotNetNuke Corporation