UBW Liberty Basic Code Snippits
Back to main UBW page

Summary:
This page contains a number of PC application software routines I've written that may be usefull to anybody who programs for the UBW board from Liberty Basic. These are not complete programs, but are routines you could put in your programs to make things easier.

Bit Flip and Test Bit:
These are things that would be really nice to have included in the language itself, but I've been using these functions instead and they appear to work just fine. Each one takes a data byte and a bit number as input. The BitFlip routine inverts the bit at position 'bit' within 'byte' and returns the new value of 'byte'.  TstBit returns a 1 or 0 based upon the value of the bit at location 'bit' in byte 'byte'. Note that for both 'bit' is zero indexed (i.e. goes from 0 to 7 for a byte).
' Flip the bit 'Bit' in byte 'Byte' and return the new byte.
function BitFlip(Byte, Bit)
    if  TstBit(Byte, Bit) = 1 then
        BitFlip = (Byte AND (255 XOR (2^Bit)))
    else
        BitFlip = (Byte OR (2^Bit))
    end if
end function

' Test bit "Bit" in variable "Byte" and return it's value (1 or 0)
function TstBit(Byte, Bit)
    TstBit = 0
    if ((Byte AND (2^Bit)) > 0) then
        TstBit = 1
    end if
end function
Finding Available COM ports:
This is a set of routines and some main code that will populate a drop down list with all of the currently available COM ports on a PC.

' This code section goes at the very top of your LB program
Global  kMaxCOMPorts            ' Maximum number (COMxx) and largest number _of_ ports to store
kMaxCOMPorts = 40
DIM CombComPort$(kMaxCOMPorts)  ' Holds strings for COM port numbers (COM1, COM2, etc.)  
' This code section goes in your GUI definition - before you open the window that contains the pull down list.
' The pull down list (Combo Box) is called CombComPort, and the array that fills it is called CombComPort$
combobox #main.CombComPort, CombComPort$(), [CombComPortDoubleClick],   75, 372, 100, 100
' This code section goes in the code that you use to intialize the stuff in the window.
' It does the work of populating the drop down list if all available COM ports
' Populate the drop down list of available COM ports
gosub [GetCOMPorts]
' This code section is what gets called when the user pulls down the combot box and selects one of the items
[CombComPortDoubleClick]  
' Perform action for the combobox named 'CombComPort'
' You should probably do other things here like close any other open ports, etc.
print #main.CombComPort, "contents? ComPort$"
open ComPort$;":9600,8,N,1,RS,DS0,CS0" for random as #commHandle
wait
' This section of code is the real meat of this example. This subroutine and function do the hard
' work of figuring out which com ports are available.
' This subroutine will populate the drop-down list called #main.CombComPort
' with all of the currently available COM ports. You can change #main.CombComPort
' to whatever you use in your program.
[GetCOMPorts]
' First clear out #main.CombComPort
for Port = 0 to kMaxCOMPorts
CombComPort$(Port) = ""
next Port

CurrentComboIndex = 0

' We're going to loop from 1 to MaxCOMPorts and see if each port is available
for TestPort = 1 to kMaxCOMPorts
if IsCOMPortAvailable(TestPort) = 1 then
' Add this port into #main.CombComPort
            CombComPort$(CurrentComboIndex) = "COM" + str$(TestPort)
            CurrentComboIndex = CurrentComboIndex + 1
        end if
    next TestPort

    ' Now reload the combbox to get the new array values
    print #main.CombComPort, "reload"

    return

' Helper function for [GetCOMPorts]
' Returns 0 if the Port is not available on this system (either already opened or doesn't
' exist) and 1 if the port is available.
function IsCOMPortAvailable(Port)
    struct lpSecurityAttributes, _
        nLength as long, _
        lpSecurityDescriptor as long, _
        bInheritHandle as long

    lpFileName$ = "\\.\COM" + str$(Port)

    dwDesiredAccess = 0
    dwShareMode = 1 + 2
    dwCreationDisposition = 3
    dwFlagsAndAttributes = 128
    hTemplateFile = 0

    calldll #kernel32, "CreateFileA", _
        lpFileName$ as ptr, _
        dwDesiredAccess as long, _
        dwShareMode as long, _
        lpSecurityAttributes as struct, _
        dwCreationDisposition as long, _
        dwFlagsAndAttributes as long, _
        hTemplateFile as long, _
        retcode as long
    hObject = retcode

    ' Test the return code to see if the port is available and free
    if retcode = -1 then
        IsCOMPortAvailable = 0
    else
        ' If we were able to open it, then close it and return TRUE
        calldll #kernel32, "CloseHandle", _
            hObject as long, _
            retcode as long

        IsCOMPortAvailable = 1
    end if
end function

Other:


Back to main UBW page
Questions? E-mail me at my e-mail address