Liberty BASIC Community Forum
« Comm Port Selection Dialog »

Welcome Guest. Please Login or Register.
Mar 24th, 2017, 4:48pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Comm Port Selection Dialog  (Read 147 times)
Dan Teel
Guru
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1116
xx Comm Port Selection Dialog
« Thread started on: Nov 14th, 2016, 9:32pm »

something I made for a project Im working, thought it might be useful to others

It'll iterate through the commports 1-100 seeing if there valid, and adds it to a list, allowing the user to select one, that way you dont have to hard code a comm port number.

Code:
print userSelectCommPort$()

end

function userSelectCommPort$()
    WindowWidth = 280
    WindowHeight = 95

    ComboboxColor$ = "white"
    stylebits #commPortSelect.ports,_CBS_DROPDOWNLIST,0,0,0
    combobox #commPortSelect.ports, portArray$(), [nothin],   10,  7, 180,  95
    button #commPortSelect.button2,"Refresh List",[refresh], UL,  10,  37, 180,  25
    button #commPortSelect.button3,"Ok",[ok], UL, 195,   7,  70,  25
    button #commPortSelect.button4,"Cancel",[closeSelect], UL, 195,  37,  70,  25

    stylebits #commPortSelect,_DS_CENTER,0,0,0
    open "Select a Comm Port" for dialog_modal as #commPortSelect
    print #commPortSelect, "font ms_sans_serif 10"
    print #commPortSelect, "trapclose [closeSelect]"

    [refresh]
        redim portArray$(100)
        j=1
        for i = 1 to 100
            portbuff$="\\.\COM";i
            calldll #kernel32,"CreateFileA",portbuff$ as ptr,3221225472 as ulong, 0 as ulong, 0 as ulong,_OPEN_EXISTING as ulong,0 as ulong, 0 as ulong, hComm as ulong
            if hComm<>_INVALID_HANDLE_VALUE then
                portArray$(j)="COM";i
                j=j+1
                calldll #kernel32,"CloseHandle",hComm as ulong,ret as void
            end if

        next i
        print #commPortSelect.ports,"reload"
        print #commPortSelect.ports,"selectindex 1"

    [nothin]
        wait

    [ok]
        print #commPortSelect.ports, "selectionindex? index"
        if index<>0 then
            userSelectCommPort$=portArray$(index)
        end if
    [closeSelect]
    redim portArray$(1)
    close #commPortSelect
end function
 
« Last Edit: Nov 14th, 2016, 9:34pm by Dan Teel » User IP Logged

ZPtr.net
Brandon Parker
Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1067
xx Re: Comm Port Selection Dialog
« Reply #1 on: Nov 15th, 2016, 11:02am »

You can also get away without using API's for this if you are willing to only be able to test COM Ports 1 - 99. This is something I have used on occasion at work while troubleshooting an inverter's control board. It has worked well for me in the past and you could modify it for use in Dan's window if you wanted to. The function traps the COM Error that occurs when you attempt to open a COM Port that doesn't exist and returns 0 to indicate failure to open, but returns the COM Port numbers that succeed.

Code:
For i = 1 To 99
    If ComPortAvailable(i) Then
        Print "COM Port ";i;" is available."
    End If
Next i
End


Function ComPortAvailable(port)
    OnCOMError [COMOpenError]
    Open "Com";str$(port);":9600,n,8,1,ds0,cs0,rs0" For random As #COM
        ComPortAvailable = port
    Close #COM
    [COMOpenError]
End Function 
« Last Edit: Nov 15th, 2016, 11:03am by Brandon Parker » User IP Logged

Windows 7 Home Premium 64-bit Intel(R) Quad Core(TM) i5 CPU M 430 @ 2.27GHz 4GB DDR3 RAM
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5215
xx Re: Comm Port Selection Dialog
« Reply #2 on: Nov 15th, 2016, 12:10pm »

The thing that was not immediately obvious to me is that you need to set and reset the oncomerror command. It fires once, you need to reset it with oncomerror [blank] which sets it again to catch the next error with oncomerror [branch]

[blank] means "", zilch, nothing. Just reset.
« Last Edit: Nov 15th, 2016, 12:19pm by Rod » User IP Logged

Brandon Parker
Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1067
xx Re: Comm Port Selection Dialog
« Reply #3 on: Nov 15th, 2016, 12:39pm »

Rod,
Is that directed towards my example?


{:0)

Brandon
User IP Logged

Windows 7 Home Premium 64-bit Intel(R) Quad Core(TM) i5 CPU M 430 @ 2.27GHz 4GB DDR3 RAM
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5215
xx Re: Comm Port Selection Dialog
« Reply #4 on: Nov 15th, 2016, 2:08pm »

No, sorry, the very first time I came across the command.

A recent example http://libertybasic.conforums.com/index.cgi?board=comport&action=display&num=1455035994

The sub getports
« Last Edit: Nov 15th, 2016, 3:28pm by Rod » User IP Logged

Dan Teel
Guru
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1116
xx Re: Comm Port Selection Dialog
« Reply #5 on: Nov 15th, 2016, 7:23pm »

on Nov 15th, 2016, 11:02am, Brandon Parker wrote:
You can also get away without using API's for this if you are willing to only be able to test COM Ports 1 - 99. This is something I have used on occasion at work while troubleshooting an inverter's control board. It has worked well for me in the past and you could modify it for use in Dan's window if you wanted to. The function traps the COM Error that occurs when you attempt to open a COM Port that doesn't exist and returns 0 to indicate failure to open, but returns the COM Port numbers that succeed.

Code:
For i = 1 To 99
    If ComPortAvailable(i) Then
        Print "COM Port ";i;" is available."
    End If
Next i
End


Function ComPortAvailable(port)
    OnCOMError [COMOpenError]
    Open "Com";str$(port);":9600,n,8,1,ds0,cs0,rs0" For random As #COM
        ComPortAvailable = port
    Close #COM
    [COMOpenError]
End Function 


Ah, yah, much simpler. I was worried that if I didnt have all the control lines and baud rate setup to match the device, it wouldn't work, but why wouldn't it? You're not talking to it, just opening it.

Lines of code wise, its about the same, but being native makes it more correct in the sense of portability (I imagine), if LB5 would happen to incorporate serial comms in Mac/Linux, and if it would be the same for each system.
« Last Edit: Nov 15th, 2016, 7:27pm by Dan Teel » User IP Logged

ZPtr.net
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls