Using the DTR & RTS signal lines as outputs from Liberty BASIC

If, as with much serial communication, you do not use the handshaking lines, you can use these two lines as outputs. This gives one of the simplest ways to electrically control something, whether LED or optoisolator. Modify the COM port number to suit your set-up. (You can find it via Control Panel). The programs should work on hardware serial ports or on USB serial adaptors.

NOTE You can also use DCD & RI as inputs- search http://libertybasic.conforums.com for Rod's clear example. Thanks to him for clarifying this way to use & control the serial port pins. Note too the delays which are necessary to allow Windows to set things up.


Here is a demonstration of toggling the lines from a window with buttons.

        '   Controlling DTR & RTS- in my case on a USB serial port.
        '   Worked single-stepped but not when run- until struct line REM'd!

        nomainwin

        '    struct modem, DSRCTS as long

        portNum$               = "7"                '   NB Yours may differ... ######################
        lpFileName$            = "COM" +portNum$
        dwCreationDistribution = _OPEN_EXISTING
        hTemplateFile          = _NULL

        timer 50, [d0]
        wait
[d0]    timer 0

        calldll #kernel32, "CreateFileA", _
            lpFileName$            as ptr, _
            dwDesiredAccess        as ulong, _
            dwShareMode            as ulong, _
            lpSecurityAttributes   as ulong, _
            dwCreationDistribution as ulong, _
            dwFlagsAndAttributes   as ulong, _
            hTemplateFile          as ulong, _
            hFileHandle            as ulong

        if hFileHandle = _INVALID_HANDLE_VALUE then
            notice "Com Port Failed to open"
            end
        else
            calldll #kernel32, "CloseHandle", _
                hFileHandle as ulong, _
                result      as boolean
        end if

        open "com7:19200,n,8,1,ds0,cs0,rs" for random as #com

        WindowWidth  =400
        WindowHeight =240

        button   #1.b1,   " &Quit",     [quit],           UL, 160, 150,  80,  40

        button   #1.b2,   " Set ",      [setCheckBoxD],   UL,  10,  50,  50,  25
        button   #1.b3,   " Reset ",    [resetCheckBoxD], UL,  60,  50,  50,  25
        checkbox #1.cbD,  "DTR state",  [setD], [resetD],      50,  20, 130,  20

        button   #1.b5,   " Set ",      [setCheckBoxR],   UL, 260,  50,  50,  25
        button   #1.b6,   " Reset ",    [resetCheckBoxR], UL, 310,  50,  50,  25
        checkbox #1.cbR,  "RTS state",  [setR], [resetR],     300,  20, 130,  20

        statictext #1.st, "hFileHandle ="; hFileHandle,       155, 100, 120,  40

        open "DTR and RTS" for window as #1

        #1, "trapclose [quit]"

        '   Now make sure both are set initially.
        #1.cbD, "set"
        CALLDLL   #kernel32, "EscapeCommFunction", hFileHandle as ulong, 5 as long, result as boolean
        #1.cbR, "set"
        CALLDLL   #kernel32, "EscapeCommFunction", hFileHandle as ulong, 3 as long, result as boolean

        wait

[setCheckBoxD]   #1.cbD, "set"
[setD] CALLDLL   #kernel32, "EscapeCommFunction", hFileHandle as ulong, 5 as long, result as boolean
        wait

[resetCheckBoxD] #1.cbD, "reset"
[resetD] CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, 6 as long, result as boolean
        wait

[setCheckBoxR]   #1.cbR, "set"
[setR] CALLDLL   #kernel32, "EscapeCommFunction", hFileHandle as ulong, 3 as long, result as boolean
        wait

[resetCheckBoxR] #1.cbR, "reset"
[resetR] CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, 4 as long, result as boolean
        wait

[quit]
        close #com
        close #1
        end



Programatically flashing lines.

The next example toggles the lines increasingly rapidly.
        '   Flashinging DTR & RTS- in my case on a USB serial port.

        nomainwin

        portNum$               = "7"                '   NB Yours may differ... ######################
        lpFileName$            = "COM" +portNum$
        dwCreationDistribution = _OPEN_EXISTING
        hTemplateFile          = _NULL

        timer 50, [d0]
        wait
[d0]    timer 0

        calldll #kernel32, "CreateFileA", _
            lpFileName$            as ptr, _
            dwDesiredAccess        as ulong, _
            dwShareMode            as ulong, _
            lpSecurityAttributes   as ulong, _
            dwCreationDistribution as ulong, _
            dwFlagsAndAttributes   as ulong, _
            hTemplateFile          as ulong, _
            hFileHandle            as ulong

        if hFileHandle = _INVALID_HANDLE_VALUE then
            notice "Com Port Failed to open"
            end
        else
            calldll #kernel32, "CloseHandle", _
                hFileHandle as ulong, _
                result      as boolean
        end if

        open "com7:19200,n,8,1,ds0,cs0,rs" for random as #com

        WindowWidth  =400
        WindowHeight =240

        textbox #1.t, 20, 20, 200, 40

        open "Flash DTR and RTS" for window as #1

        #1, "trapclose [quit]"

    for i =0 to 100
        #w.t, "i ="; i
        CALLDLL   #kernel32, "EscapeCommFunction", hFileHandle as ulong, 5 as long, result as boolean
        CALLDLL   #kernel32, "EscapeCommFunction", hFileHandle as ulong, 6 as long, result as boolean

        timer 500, [MoveOn1]
        wait
        [MoveOn1]
        timer 0

        CALLDLL   #kernel32, "EscapeCommFunction", hFileHandle as ulong, 3 as long, result as boolean
        CALLDLL   #kernel32, "EscapeCommFunction", hFileHandle as ulong, 4 as long, result as boolean

        timer 500, [MoveOn2]
        wait
        [MoveOn2]
        timer 0
    next i

[quit]
        close #com
        close #1
        end



Any queries? e-Mail me changing DOT to '.'