Jump to content
mikkokh

How found COM port number using USB COM adapter details

Recommended Posts

mikkokh

Hi.

 

I have several different devices connected to my desktop PC with USB COM adapters.

 

Sometimes there is some error that will cut my USB COM device communication (usualy caused by lose USB connector)... After reconnecting that USB COM adapter, by some reason, Windows will give it different COM port number.

I know there are some devices with integrated COM port circuit and they are used by client programs that can found them and only them by some USB hardware relate details and then use their COM port to communicate with correct COM device.

It would be better to define some known and static hardware related string from device managers USB COM adapter details to my script config file, than always redefine different COM port to it.

 

How can I do this trick with autoit?

 

I communicate COM devices with cfxUDF.au3 AutoIt UDF.


- miXza-81 -

Share this post


Link to post
Share on other sites
funkey

 


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
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

  • Similar Content

    • JRSmile
      By JRSmile
      Hi,
      i searched the forum to communicate with a logitech G600 without using Shortcuts or Default Mouse Keys.
      the code below lets u use your sidekeys whenever you press down the ring key.
      before using the code you will have to unassign the keys in the logitech mouse driver software.
      The source of my code is a wacom usb script, wich was heavily modified to work.
      you might find some parts wierd, thats because this is my first attempt to communicate via raw hid. 
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Change2CUI=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <APISysConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WinAPIGdi.au3> #include <WinAPIMisc.au3> #include <WinAPISys.au3> #include <WindowsConstants.au3> #cs ; #include "CommMG.au3" $mgdebug = True Global $sportSetError $tset = _ComGetPortNames() for $i = 0 to UBound($tset) -1 if $tset[$i][1] = "Serielles USB-Gerät" Then $COMPort = StringTrimLeft($tset[$i][0],3) ConsoleWrite("Teensy an Port COM" & $COMPort & " gefunden!" & @CRLF) EndIf Next $resOpen = _CommSetPort($COMPort,$sportSetError,31250, 8,0,1,0,0,0) if $resOpen = 0 then ConsoleWrite($sportSetError & @LF) ;~ Exit EndIf #ce $atagRID_DEVICE_INFO_HID = 'struct;dword VendorId;dword ProductId;dword VersionNumber;ushort UsagePage;ushort Usage;endstruct' $atagRID_INFO_HID = 'dword Size;dword Type;' & $atagRID_DEVICE_INFO_HID & ';dword Unused[2]' Opt('TrayAutoPause', 0) ; Create GUI Global $g_hForm = GUICreate('G600', 100, 25, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_SYSMENU)) Local $tRID = DllStructCreate($tagRAWINPUTDEVICE) DllStructSetData($tRID, 'Flags', $RIDEV_INPUTSINK) DllStructSetData($tRID, 'hTarget', $g_hForm) DllStructSetData($tRID, 'UsagePage', 0x80) ; Logitech G600 DllStructSetData($tRID, 'Usage', 0x0A) ; _WinAPI_RegisterRawInputDevices($tRID) ; Now iterate to find other devices Local $tText, $aData = _WinAPI_EnumRawInputDevices() If IsArray($aData) Then ReDim $aData[$aData[0][0] + 1][3] $tText = DllStructCreate('wchar[256]') For $i = 1 To $aData[0][0] If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $tText, 256, $RIDI_DEVICENAME) Then $aData[$i][2] = DllStructGetData($tText, 1) Else $aData[$i][2] = '' EndIf If $aData[$i][1] = $RIM_TYPEHID Then $devInf = DllStructCreate($atagRID_INFO_HID) If _WinAPI_GetRawInputDeviceInfo($aData[$i][0], $devInf, DllStructGetSize($devInf), $RIDI_DEVICEINFO ) Then If DllStructGetData($devInf, 'VendorId') = 0x046D And DllStructGetData($devInf, 'ProductId') = 0xC24A then ; G600 VID & PID ConsoleWrite ("Device Info:-" & @CRLF) ConsoleWrite ('VendorId: ' & Hex(DllStructGetData($devInf, 'VendorId'),4) & @CRLF) ConsoleWrite ('ProductId: ' & Hex(DllStructGetData($devInf, 'ProductId'),4) & @CRLF) ConsoleWrite ('VersionNumber: ' & DllStructGetData($devInf, 'VersionNumber') & @CRLF) ConsoleWrite ('UsagePage: ' & Hex(DllStructGetData($devInf, 'UsagePage'),2) & @CRLF) ConsoleWrite ('Usage: ' & Hex(DllStructGetData($devInf, 'Usage'),2) & @CRLF) DllStructSetData($tRID, 'UsagePage', DllStructGetData($devInf, 'UsagePage')) DllStructSetData($tRID, 'Usage', DllStructGetData($devInf, 'Usage')) _WinAPI_RegisterRawInputDevices($tRID) EndIf EndIf EndIf Next EndIf ; Register WM_INPUT message GUIRegisterMsg($WM_INPUT, 'WM_INPUT') GUISetState(@SW_SHOW) Global $structHID_DATA = "struct;" & _ "dword Type;" & _ "dword Size;" & _ "handle hDevice;" & _ "wparam wParam;" & _ "dword dwSizeHid;" & _ "dword dwCount;" & _ "endstruct;" Global $structWACOM_PEN_DATA = "struct;" & _ "dword Type;" & _ "dword Size;" & _ "handle hDevice;" & _ "wparam wParam;" & _ "dword dwSizeHid;" & _ "dword dwCount;" & _ "ubyte bRawData00;" & _ "ubyte penvsEraser;" & _ "word x;" & _ "word y;" & _ "word proximity;" & _ "word pressure;" & _ "ubyte bRawData09;" & _ "ubyte bRawData10;" & _ "ubyte bRawData11;" & _ "ubyte bRawData12;" & _ "ubyte bRawData13;" & _ "ubyte bRawData14;" & _ "ubyte bRawData15;" & _ "ubyte bRawData16;" & _ "ubyte bRawData17;" & _ "ubyte bRawData18;" & _ "ubyte bRawData19;" & _ "ubyte bRawData20;" & _ "ubyte bRawData21;" & _ "ubyte bRawData22;" & _ "ubyte bRawData23;" & _ "ubyte bRawData24;" & _ "ubyte bRawData25;" & _ "ubyte bRawData26;" & _ "ubyte bRawData27;" & _ "ubyte bRawData28;" & _ "ubyte bRawData29;" & _ "ubyte bRawData30;" & _ "ubyte bRawData31;" & _ "ubyte bRawData32;" & _ "ubyte bRawData33;" & _ "ubyte tilt_ba;" & _ "ubyte bRawData35;" & _ "ubyte tilt_na;" & _ "ubyte bRawData37;" & _ "endstruct;" Do Until GUIGetMsg() = $GUI_EVENT_CLOSE Func WM_INPUT($hWnd, $iMsg, $wParam, $lParam) #forceref $iMsg, $wParam Switch $hWnd Case $g_hForm Local $tRIM = DllStructCreate($tagRAWINPUTHEADER) If _WinAPI_GetRawInputData($lParam, $tRIM, DllStructGetSize($tRIM), $RID_HEADER) Then ; Retrieves the raw input from the specified device $devType = DllStructGetData($tRIM, 'Type') $devSize = DllStructGetData($tRIM, 'Size') Else ConsoleWrite ("Device Header Retrieval Failed" & @CRLF) Return EndIf ; Now use the handle to the device to get it's name Local $tText = DllStructCreate('wchar[256]') If _WinAPI_GetRawInputDeviceInfo(DllStructGetData($tRIM, 'hDevice'), $tText, 256, $RIDI_DEVICENAME) Then $devName = DllStructGetData($tText, 1) Else ConsoleWrite ("Device Name Retrieval Failed" & @CRLF) EndIf if $devType = $RIM_TYPEHID Then $tRIM = DllStructCreate($structWACOM_PEN_DATA) If _WinAPI_GetRawInputData($lParam, $tRIM, DllStructGetSize($tRIM), $RID_INPUT) Then if DllStructGetData($tRIM, 8) = 32 Then ; filter for ring key down (G-Mode) Switch DllStructGetData($tRIM, 9) ; if in g mode assign the Keys G9 to G20 to autoit functions. case 0 Return ; key released case 1 g1() case 2 g2() case 4 g3() case 8 g4() case 16 g5() case 32 g6() case 64 g7() case 128 g8() case 256 g9() case 512 g10() case 1024 g11() case 2048 g12() Case Else ;;; ConsoleWrite(DllStructGetData($tRIM, 9) & @CRLF) EndSwitch EndIf EndIf EndIf EndSwitch sleep(50) Return $GUI_RUNDEFMSG EndFunc ;==>WM_INPUT Func g1() ;~ Send("2") ConsoleWrite("g1" & @CRLF) ;~ _CommSendString("KEY_TAB" & @LF) ;~ Sleep(random(50,100,1)) ;~ _CommSendString("KEY_RELEASE_ALL" & @LF) ;~ Sleep(random(50,100,1)) ;~ _CommSendString("KEY_2" & @LF) ;~ Sleep(random(50,100,1)) ;~ _CommSendString("KEY_RELEASE_ALL" & @LF) EndFunc Func g2() ConsoleWrite("g2" & @CRLF) EndFunc Func g3() ConsoleWrite("g3" & @CRLF) EndFunc Func g4() ConsoleWrite("g4" & @CRLF) EndFunc Func g5() ConsoleWrite("g5" & @CRLF) EndFunc Func g6() ConsoleWrite("g6" & @CRLF) EndFunc Func g7() ConsoleWrite("g7" & @CRLF) EndFunc Func g8() ConsoleWrite("g8" & @CRLF) EndFunc Func g9() ConsoleWrite("g9" & @CRLF) EndFunc Func g10() ConsoleWrite("g10" & @CRLF) EndFunc Func g11() ConsoleWrite("g11" & @CRLF) EndFunc Func g12() ConsoleWrite("g12" & @CRLF) EndFunc  
    • BigMac121263
      By BigMac121263
      I know that this theme was discussed several times but I read all These threats with no helping result.
      I have two pieces of TEMPer1F USB Thermometer.
      It is a Standard Windows HID device with no add. Driver needed.
      With the device I got an App reading from it and showing Graphs and much more and which can handle several devices.
      With the Software came also a RDingUSB.dll file handling the communication with the device.
      I can call the dll without any error. But the Problem is how to Access the device using the DLL and Access more than one of same VID/PID
      by Serial or anything else useful.
      I have also sources in c# doing the Job, but no experience in C# to successfuly port it to AutoIT.
       
      So my question: Can anyone help me to realize the simple pull of the temperature value from the devices based on C# source?
      The rest to write it to MySQL DB and repeat it etc. is no Problem for me.
      ***************************************
      The C# part with the DLL functions class:
      ***************************************
      using System;
      using System.Runtime.InteropServices;
      namespace TEMPer
      {
        internal class RDing
        {
          [DllImport("RDingUSB.dll")]
          public static extern IntPtr OpenUSBDevice(int VID, int PID);
          [DllImport("RDingUSB.dll")]
          public static extern IntPtr CloseUSBDevice(IntPtr hDevice);
          [DllImport("RDingUSB.dll")]
          public static extern bool WriteUSB(IntPtr hDevice, byte[] pBuffer, uint dwBytesToWrite, ref ulong lpNumberOfBytesWritten);
          [DllImport("RDingUSB.dll")]
          public static extern bool ReadUSB(IntPtr hDevice, byte[] pBuffer, uint dwBytesToRead, ref ulong lpNumberOfBytesRead);
          [DllImport("RDingUSB.dll")]
          public static extern ushort GetInputLength(IntPtr hDevice);
          [DllImport("RDingUSB.dll")]
          public static extern ushort GetOutputLength(IntPtr hDevice);
          [DllImport("RDingUSB.dll")]
          public static extern uint GetErrorMsg(ref string[] lpErrorMsg, uint dwErrorMsgSize);
        }
      }
      *********************************
      The C# code snippet working with the functions of the class above is in the attachment
      this is the interesting part where the data is sent and pulled which I
      do not understand to transcode to AutoIT
      Important my devices are the type2 (TEMPer1F) :
      *********************************
      So in the end I Need: Call DLL, read all devices in array
      Loop the Array until ubound(Array) and for each device in Array read the temperature value and prepare the data to
      decimal 6,1 (SQL Format) and °C
      So how to pull the value from the devices?
      Maybe somwbody finds the important part in the C#  (the code above is for different models, mine is TEMPer1F
      Or maybe someone has solved this and has maybe a uDF for this
       
       
      MainForm.cs
    • Corleone221
      By Corleone221
       
      Hello All,
       
      How can I load  actual connect  Com Ports to combobox?
       
      Thanks.
       
      for example  this 3....(6,7,8)

       
       
    • cheeroke
      By cheeroke
      Hi all,
      I got this code and would like to be able to change Baud Rate and instead of sending character by character i would like to be able (if possible) to send whole string. But i don't know how to change it.
      I am taking input from file and processing whole line (this is done in FilesHandling.au3).
      To execute this i am just calling SendData("FileName", int) in "main" script.
      Any help very appreciated.
      #include <WinAPI.au3> #include <Array.au3> #include "FilesHandling.au3" ;init DLL function, we need handle to call the function $h = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", "\\.\COM19", "int", BitOR($GENERIC_READ,$GENERIC_WRITE), "int", 0, "ptr", 0, "int", $OPEN_EXISTING, "int", $FILE_ATTRIBUTE_NORMAL, "int", 0) $handle=$h[0] Func SendData($FileName, $LineNumber) ;string to be send $c = readFile($FileName, $LineNumber) $cLenght = StringLen($c) $aArray = StringSplit($c, "") ;_ArrayDisplay($aArray, "", Default, 64) For $i = 1 To $cLenght writeChar($handle, $aArray[$i], $cLenght) Next ;move to next line writeChar($handle, @CR,1) EndFunc ;write a single char func writeChar($handle,$c,) $stString = DLLStructCreate("char str") $lpNumberOfBytesWritten = 0 DllStructSetData($stString, 1, $c) $res = _WinAPI_WriteFile($handle, DllStructGetPtr($stString, "str"), 1,$lpNumberOfBytesWritten) if ($res<>true) then ConsoleWrite ( _WinAPI_GetLastErrorMessage() & @LF) EndIf EndFunc  
    • SAM1386
      By SAM1386
      Hello,
      I can easily find my modem comport number using the code below:-
       
      #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3>    Global $key = RegRead("HKLM\HARDWARE\DEVICEMAP\SERIALCOMM","\Device\ssudmdm0000")     Local $iError = 0     If @error Then        ; The server is probably offline/port is not opened on the server.        $iError = @error         MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not connect, Error code: " & $iError)         Else         MsgBox($MB_ SYSTEMMODAL, "MODEM:", $key)         EndIf  
      but the thing is I am planning to design a small tool by which it can read the modem COM port and set the COM number and send AT command to modem port using UDF CommMG.au3 created by Martin. I added some below code to martins file :-
      ;====================================================================================================================== ;CommSendStandard,a new function for sending information to a serial port. ;====================================================================================================================== Func _CommSendStandard($port,$error,$command)    _CommSetPort($port,$error,9600,8,0,1,2)  if $error <> '' Then      MsgBox(262144,'Port Error = ',$error)  EndIf  _CommSendString($command & @CR) EndFunc   ;==>_CommGetLineStates ;=============================================================================================== After adding I wrote the below code for sending AT command to modem port but its not working.
      Please check the below code and let me know for the issue.
      ;===================================================================================== #include "C:\Program Files (x86)\AutoIt3\SciTE\CommMG.au3" #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3>    Global $key = RegRead("HKLM\HARDWARE\DEVICEMAP\SERIALCOMM","\Device\ssudmdm0000")     Local $iError = 0     If @error Then        ; The server is probably offline/port is not opened on the server.        $iError = @error         MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Client:" & @CRLF & "Could not connect, Error code: " & $iError)         Else         MsgBox($MB_SYSTEMMODAL, "MODEM:", $key)         EndIf Local $port=$key Local $porterror _CommSendStandard($port,$portError,"AT+CFUN=1,1") ;================================================================================================ but If I set the port number by my own in the code (for eg :- port number 8 or any value  instead of $key) it works fine and the modem gets reset.I want to know if there is any way to get the port number to a variable $port and discarding so that my above code works fine...In that case user donot have to take care of entering the port manually everytime and the script will automatically read the port number and set itself.
       
      Please help
       
      Regards,
      SAM
       
       
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.