Jump to content

Com port Query


Go to solution Solved by orbs,

Recommended Posts

I was trying to find a way to find out via AutoIt what comm port a device was plugged into.  I am using a Silicon Labs CP210x USB to UART Bridge.  This has to be done behind the scenes so i can automatically setup the correct comm port for my communications software.

Thanks in advance

 

post-77772-0-80434600-1392404213_thumb.j

Link to comment
Share on other sites

  • Solution

alternative, using WMI:

code adapted from: http://artisgeek.com/weblog/scripts/-getdevices-autoit/

#include <Array.au3>
$aDeviceList=_getDevices('Silicon Labs CP210x USB to UART Bridge',1)
_ArrayDisplay($aDeviceList)

;Function Name: Get (Connected) Devices
;Written By: Amin Babaeipanah
;Usage: _getDevices(1,'')
;_getDevices("search for", flag)
;"search for": can be set to empty to list everything
;flag:
;    1 = List Device Name(s)
;    2 = List Device ID(s)
;    3 = List Device Name(s) @ Device ID(s)
;Example 1:
;    Code below will list all the connected devices by name
;        _getDevices('',1)
;    Code below will list all the connected devices by ID
;        _getDevices('',2)
;    Code below will list all the connected devices by name that has the word "COM"
;        _getDevices('COM',1)

; adaptation: replace ConsoleWrite with array return
; adaptation by: orbs
; original source at: http://artisgeek.com/weblog/scripts/-getdevices-autoit/

Func _getDevices($name,$type)
    Dim $aDeviceList[1]=[0]
    Local $objWMIService = ObjGet('winmgmts:\\localhost\root\CIMV2')
    Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%"&$name&"%'", "WQL", 48)
    If IsObj($colItems) Then
        If $type = 1 Then
            For $objItem In $colItems
                $aDeviceList[0]+=1
                ReDim $aDeviceList[$aDeviceList[0]+1]
                $aDeviceList[$aDeviceList[0]]=$objItem.Name
                ;ConsoleWrite($objItem.Name&@LF)
            Next
        ElseIf $type = 2 Then
            For $objItem In $colItems
                $aDeviceList[0]+=1
                ReDim $aDeviceList[$aDeviceList[0]+1]
                $aDeviceList[$aDeviceList[0]]=$objItem.PNPDeviceID
                ;ConsoleWrite($objItem.PNPDeviceID&@LF)
            Next
        ElseIf $type = 3 Then
            For $objItem In $colItems
                $aDeviceList[0]+=1
                ReDim $aDeviceList[$aDeviceList[0]+1]
                $aDeviceList[$aDeviceList[0]]=$objItem.Name&'@'&$objItem.PNPDeviceID
                ;ConsoleWrite($objItem.Name&'@'&$objItem.PNPDeviceID&@LF)
            Next
        EndIf
    EndIf
    Return $aDeviceList
EndFunc

the function returns an array of all devices by criteria (name or type).

calling he function with your device name will return (hopefully) only one result, then use string manipulation - like _StringBetween() - to parse the port.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

  • 4 years later...

 

On 2/14/2014 at 2:28 PM, goodmanjl531 said:

Thanks, That did the trick!!!

Hi goodmanjl531,

I recently bought an ICOM 7300 (Amateur radio Transceiver) which uses  the Silicon Labs CP210x USB to UART Bridge. Finding the COM port with ORBS function works great.
Now I have a question. How do you send date to the port? I used Martin's CommMG.au3 UDF for Yaesu radios with RS-232 ports without problems but  it looks like it does not wort with the UART Bridge. Code so far for testing is:

#include <CommMG.au3>
#include <MsgBoxConstants.au3>

Global $CMPort = 6
Global $CmBoBaud = 115200
Global $sportSetError = ''
Global $CmboDataBits = 8
Global $CmBoParity = "none"
Global $CmBoStop =1
Global $setflow = 1
Global $freq, $TXon

_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow)

If @error Then
    MsgBox(16,"Error!","Can't connect to ICOM 7300 on port - "&$CMPort)
    Exit
EndIf

_CommSetRTS(1)
_CommSetDTR(1)

$sfreq = _CommSendString("FEFE94E003FD")        ;Read frequency
$TXon =  _CommSendString("FEFE94E01C0001FD")    ;TX on

MsgBox(64,"IC-7300 sending..", "Transmit on: " & $TXon & @crlf & "Frequency: " & $sfreq)

#cs
here goes some stuff to send data over the air
#ce

$TXon = _CommSendString("FEFE94E01C0000FD") ; TX off

I tried this but without success. Is there another UDF for sending DATA to the UART Bridge?

Thanks for any help.

KF5WGB

 

Edited by KF5WGB
Typo
Link to comment
Share on other sites

Looking at bottom of page 19-7 of the ICOM-7300 manual I read that you must transmit a given number of 0xFE bytes depending on the baudrate: e.g. 150 0xFE bytes for 115kBd. I can't tell if that applies to your setup.

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

11 hours ago, jchd said:

Looking at bottom of page 19-7 of the ICOM-7300 manual I read that you must transmit a given number of 0xFE bytes depending on the baudrate: e.g. 150 0xFE bytes for 115kBd. I can't tell if that applies to your setup.

 

Thanks for the hint. I added _StringRepeat to the code to get 150 FE's but I think this is only needed if you want to remotely power on the radio.
Added it anyway for testing.

#include <CommMG.au3>
#include <MsgBoxConstants.au3>
#include <String.au3>

Global $CMPort = 6
Global $CmBoBaud = 115200
Global $sportSetError = ''
Global $CmboDataBits = 8
Global $CmBoParity = "none"
Global $CmBoStop = 2
Global $setflow = 2
Global $FE, $sfreq

_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow)

If @error Then
    MsgBox(16,"Error!","Can't connect to ICOM 7300 on port - "&$CMPort & @CRLF & @error)
   Exit
EndIf

_CommSetRTS(1)
_CommSetDTR(1)

$FE = _StringRepeat("FE",150)                   ;add 150 FE's
_CommSendString($FE & "FEFE94E01801FD",1)       ;power on

_CommSendString("FEFE94E003FD")                 ;Read frequency
$sfreq = _CommGetstring()                       ;Read buffer

_CommSendString("FEFE94E01C0001FD")             ;TX on

MsgBox(64,"IC-7300 sending..", "Frequency: " & $sfreq)

#cs
here goes some stuff to send data over the air
#ce

_CommSendString("FEFE94E01C0001FD")             ;TX off

Adding 150 FE's does not send the command to the radio.
Maybe commMG.au3 does not work with USB/UART

 

Edited by KF5WGB
Modified code
Link to comment
Share on other sites

As I said I don't know what is required in your specific setup. The UDF should work fine with all USB-RS232 adapters.

Can't you just use a direct USB link with the vendor's driver?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

6 hours ago, jchd said:

~~ The UDF should work fine with all USB-RS232 adapters. ~~

jchd,

It is not a USB-RS232 adapter. Its on board the IC-7300 radio. I tried all settings for port speed, parity etc. Oh well it is one of the things that do not work.
Thanks for your help anyway. It just boggles my mind that all other radios with 'real' serial adapters work just fine using CommMG.au3.

I have to find a USB port sniffer and see what comes back from the radio or bite the bullet and find some info about native windooze DLL calls for USB port prigramming. If I ever find out what causes the problem, I let you know.

Thanks again and Merry Christmas and a Happy New Year

Link to comment
Share on other sites

@jchd,

Found the problem. The ICOM 7300 requires DATA send in binary  instead of ASCII. Well, ICOM does not mention that once in the manual.

Searching the forum pointed me to this post:

rover had the answer. I changed the code and tadaaaa.

#include <CommMG.au3>
#include <MsgBoxConstants.au3>
#include <String.au3>

Global $CMPort = 10
Global $CmBoBaud = 115200
Global $sportSetError = ''
Global $CmboDataBits = 8
Global $CmBoParity = "none"
Global $CmBoStop = 2
Global $setflow = 2
Global $FE, $sfreq, $errormsg

_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow)

If @error Then

;    @error             meaning error with
;       1               dll call failed
;       2               dll was not open and could not be opened
;      -1               $iBaud
;      -2               $iStop
;      -4               $iBits
;      -8               $iPort = 0 not allowed
;     -16               $iPort not found
;     -32               $iPort access denied (in use?)
;     -64               unknown error

    If @error = -32 Then
        $errormsg = "Error: Access denied (Port in use?)"
    elseif @error = -16 then
        $errormsg = "Error: Port not found"
    EndIf
    MsgBox(16,"Error!","Can't connect to ICOM 7300 on port: "&$CMPort & @CRLF & $errormsg)
   Exit
EndIf

_CommSetRTS(1)
_CommSetDTR(1)
_CommClearOutputBuffer()
_CommClearInputBuffer()

MsgBox(64,"IC-7300", "Click OK ")       ; pause to get VSPE displaying traffic

_PowerOn()                              ; Turn Radio on
_ReadFreq()                             ; Requesting Frequency setting
_sendData()                             ; Sending DATA over air
_PowerOff()
_CommClearOutputBuffer()
_CommClearInputBuffer()
_Commcloseport()

Exit                                    ; End program

Func _ReadFreq()
$bBinData = Binary("0xFEFE94E003FD")    ; code to request frequency
$iNumbytes = BinaryLen($bBinData)
$tBinData = DllStructCreate("byte["&$iNumbytes&"]")
DllStructSetData($tBinData, 1, $bBinData)
$iRet = _CommSendByteArray(DllStructGetPtr($tBinData),$iNumbytes,1)
If @error Or $iRet = -1 Then ConsoleWrite("!Error: " &  @error & @CRLF)

;here goes the code to read input buffer and change to ascii
;using BinaryToString, I guess
;$sfreq var holds frequency

EndFunc

Func _SendData()
;
MsgBox(64,"IC-7300", "Data will be sent" & @CRLF &  "using Frequency: " & $sfreq,10)        ;display Frequency
;
;and send data over the air
;
EndFunc

Func _PowerOn()
; Turn Radio on - works
MsgBox(0,"IC-7300","Click OK to turn" & @CRLF & "on the Transceiver")
$bBinData = ""
$bBinData = $bBinData & "0x" & _StringRepeat("FE",150) &"FEFE94E01801FD"
$bBinData = Binary($bBinData)
$iNumbytes = BinaryLen($bBinData)
$tBinData = DllStructCreate("byte["&$iNumbytes&"]")
DllStructSetData($tBinData, 1, $bBinData)
$iRet = _CommSendByteArray(DllStructGetPtr($tBinData),$iNumbytes,1)
If @error Or $iRet = -1 Then ConsoleWrite("!Error: " &  @error & @CRLF)
EndFunc

Func _PowerOff()
    ; Turn Radio off - works
MsgBox(0,"IC-7300","Click OK to turn" & @CRLF & "off the Transceiver")
$bBinData = ""
$bBinData = $bBinData & "0x" & _StringRepeat("FE",150) &"FEFE94E01800FD"
$bBinData = Binary($bBinData)
$iNumbytes = BinaryLen($bBinData)
$tBinData = DllStructCreate("byte["&$iNumbytes&"]")
DllStructSetData($tBinData, 1, $bBinData)
$iRet = _CommSendByteArray(DllStructGetPtr($tBinData),$iNumbytes,1)
If @error Or $iRet = -1 Then ConsoleWrite("!Error: " &  @error & @CRLF)
EndFunc

Now I just have to figure out how to read the data coming back from the radio and convert to ASCII.

Just thought you might be interested. Have a great week.

KF5WGB

Link to comment
Share on other sites

Good to know, but I'm not a radio-amateur at all.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

That's ok jchd,

nobody is perfect,lol.   Just kidding. Maybe you look into becoming a HAM op someday. Your post back up there mentioning  the 150 0xFE got me on the right track.

Anyway, it really does not matter what device is using the 210x UART Bridge, key is... how to talk to it and how to read the data coming back to the app.

Thanks again and Happy New Year

KF5WGB

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...