Using Liberty Basic to Interface a PC (Windows) with a TM #128 Temperature Module

copyright, Peter H Anderson, Baltimore, MD, Feb, '05


(Feb 17, '05). This is a work in progress.

Introduction

This application note discusses Serial RS232 communication between a PC running Windows and a TM #128 temperature module. In fact, this could be extended to virtually any of my temperature and control modules, or indeed to any processor. Typical applications include measurement and perhaps in the case of a thermostat or alarm, acting on the measurement, performing calculations and logging data.

I opted to develop this material using Liberty Basic for Windows. It is relatively inexpensive and appears exceptionally well supported by many dedicated people. It appears well structured and is close enough to C / C++ for me to at least make a good stab at writing simple programs. No knowledge of Windows is required.

A demo copy of Liberty Basic is free but there is an annoying time delay each time you desire to compile. There is a $29 version which removes this delay and a $49 version which provides for making executables. Factors in selecting Liberty Basic were ease of use in implementing the serial communications, it widely used and well supported and relatively inexpensive.

I will confess that my knowledge of programming in Basic is limited and it seems as if each day I make some new discovery. However, if I wait to post material I am proud to post, I fear nothing will ever be posted. So, please take this as a possible starting point for your own creative being.

Please note that Liberty Basic is one approach. I make no claim that it is better or worse than any other approach. There are simply so many platforms that I decided to simply implement some samples using Liberty. I decided some time ago that I could not be good at all things and decided to strive to be good in developing the interfacing processors. Programming on the PC side is not one of my peaks of excellence.

The following Liberty Basic programs were tested using Windows 98SE.


Format of the TM #128 Measurement Data

Recall that when the TM #128 unit receives any character, returns the state of the single TTL input and then performs a measurement sequence on each Dallas 1-W sensor found on one run and then on the second run. A sample;

0
00 10 9DD7 26.12
01 10 BD53 26.12
02 28 3C2C 26.32
03 22 5BFC 26.43
10 10 0C26 27.18
11 10 0AD7 27.25
12 26 09CE 27.18 2.55 5.03
>
The first line is the state of the single input and is either 0 or 1.

This is then followed by the measurement results from the Dallas devices.

The first digit of the first field identifies the run. The second digit of field 1 is a sequential number which is assigned as each Dallas device is found. Field two identifies the device type. The device code 10 indicates the device is a DS18S20, code 28 is a DS18B20, code 22 is a DS1822 and code 26 is a DS2438. Field three is an abbreviation of the unique serial number assigned by Dallas to each device.

Note that the prompt > may be interpreted as the end of the sequence.


Program TM128_1.Bas.

This program initiates a sequence of temperature by the TM #128 each minute. The results are fetched and displayed and also logged to a file. When the user enters any character in the text window, the measurement is halted and the content of the log file is displayed to the window text box.

This program is nearly the same as TM125_1.Bas which was developed for the TM #125. The TM #125 is similar to the TM #128, except it provides only a single run, there is no TTL input, the Dallas measurement data is formatted somewhat differently and it will not accommodate the DS2438 battery monitor. Howver, the discussion associated with the program TM125_1.Bas is equally applicable to Program TM128_1.Bas.

' TM128_1.Bas (Liberty Basic)
'
' Illustrates an interface with temperature module TM #128
'
' Opens a text box.  Opens a logging file d:\data.txt
'
' Every minute sends a character to the TM #128, causing the TM #128 to perform
' measurements on each of the Dallas devices on both Dallas runs.  Note that valid
' Dallas devices include the DS18S20 / DS18B20 / DS1822 temperature sensors and the
' Dallas DS2438 battery monitor.
'
' This program fetches each string from the TM #128 using function GetLine$.
'
' The string is then displayed on the terminal.  In addition, the file is briefly opened, the
' string is written to the file and the file is closed.
'
' This continues indefinitely, until any character is entered from the terminal.  The file is
' then opened and diplsyaed to the terminal.
'
' Illustrates developing a text box, and the use of scan to read a charcter from the keyboard.
'
' Useful for developing data logger applications.
'
' Uses Com2, 9600 baud.
'
' copyright, Peter H Anderson, Baltimore, MD, Feb, '05


    Com = 16384     ' size of buffer

    ' define a box
    nomainwin

    WindowWidth = 400
    WindowHeight = 300
    texteditor #window.te, 0, 0, 391, 254     'The handle for our texteditor is #window.te
    graphicbox #window.gb, 800, 1, 10, 10
    open "kb" for window as #window           'The handle for our window is #window
    print #window.gb, "when characterInput [getChar]"   'When the user presses a key go to [getChar]
    print #window, "trapclose [quit]"    'When the user closes our terminal window, go to [quit]
    print #window.te, "!autoresize";   'Tell the texteditor to resize with the terminal window
    print #window, "font courier_new 9";

[top]
    oncomerror [closecomm]  ' not sure if this works
    open "com2:9600,n,8,1, cs0, ds0" for random as #commhandle

    call pause 500  ' wait for unit to settle

    open "d:\data.txt" for output as #log ' open the file
    close #log

    TimeOutTime = time$("seconds")
    TimeOutDate = date$("days")

[again]

    TimeOutTime = TimeOutTime + 60 ' every minute

    if (TimeOutTime >= 86400000) then
        TimeOutDate = TimeOutDate + 1
        TimeOutTime = TimeOutTime - 86400000
    end if

    print #commhandle, "!"  ' send any character

    x = 0
    while x = 0       ' loop until either a string is received or timeout

       DataLine$ = GetLine$(NumChars) ' NumChars is passed by reference

       select case

          case NumChars = -30000
              print #window.te, "timeout"
              exit while

          case (NumChars = 1)
              if DataLine$ = ">" then
                  print #window.te, DataLine$
                  exit while
              end if

              if (DataLine$ = "0" OR DataLine$ = "1") Then
                  print #window.te, DataLine$
                  open "d:\data.txt" for append as #log
                  print #log, DataLine$       ' and write it to the file
                  close #log
              else
                  print #window.te, "Unknown "; DataLine$
              end if

          case (NumChars > 2)        ' probably a vaild string
              print DataLine$

              open "d:\data.txt" for append as #log
              print #window.te, DataLine$ ' display the string
              print #log, DataLine$       ' and write it to the file
              close #log

          case else
              print "Unknown error"   ' probably an overkill
              exit while
        end select

    wend
    do
        print #window.gb, "setfocus"
        scan ' used for check if activity from the keyboard
        CurrentDate = date$("days")
        CurrentTime = time$("seconds")
    loop until (CurrentTime >= TimeOutTime) AND (CurrentDate >= TimeOutDate)

    goto [again]

[display]
    ' now open the file display the data
    open "d:\data.txt" for input as #log

    while eof(#log) = 0
       line input #log, datain$
       print #window.te, datain$
    wend

    close #log

[Done]
    Goto [Done]

[getChar]

    'Whenever the user presses a key, we go here to process it.
    c$ = Inkey$
    ' print #window.te, "at getChar"
    goto [display]

[closecomm]
     print "Error code: ";ComErrorNumber
     close #commhandle
     goto [top]

function GetLine$(ByRef StatusFlag)

    TimeOutTime = time$("milliseconds")
    TimeOutDate = date$("days")

    TimeOutTime = TimeOutTime + 3000 ' three seconds

    if (TimeOutTime >= 86400000) then
        TimeOutDate = TimeOutDate + 1
        TimeOutTime = TimeOutTime - 86400000
    end if


     DataLine$ = ""
     StatusFlag = 0
     NumChars = 0

     x=0
     while x=0
        if lof(#commhandle) <>0 then
           c$ = input$(#commhandle, 1)
           select case
              case  c$ = ">"
                 DataLine$ = c$
                 StatusFlag = 1 ' prompt
                 exit while

              case c$ = chr$(13)
                 StatusFlag = NumChars
                 exit while

              case c$ = chr$(10)    ' ignore it

              case else
                 DataLine$ = DataLine$ + c$
                 NumChars = NumChars + 1

            end select
        else
           CurrentDate = date$("days")
           CurrentTime = time$("milliseconds")
           If (CurrentTime >= TimeOutTime) AND (CurrentDate >= TimeOutDate) Then
              StatusFlag = -30000
              exit while
           end if
        end if
     wend
     GetLine$ = DataLine$
End Function

sub pause mil
    tcurrent = time$("milliseconds")
    timeout = tcurrent + mil
    if timeout > 86400000 then  ' roll over at midnight
       timeout = timeout - 86400000
       do
           tcurrent = time$("milliseconds")
       loop until (86400000 - tcurrent) > 1000000
    end if

    do
       tcurrent = time$("milliseconds")
    loop until (tcurrent >= timeout)

    end sub

    end