Jump to content
SAM1386

Get port number to variable

Recommended Posts

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

 

 

Edited by JLogan3o13

Share this post


Link to post
Share on other sites
SAM1386

Someone please reply and help me out of it.

 

Regards,

SAM

Share this post


Link to post
Share on other sites
SAM1386

why there is no response from anyone...Someone please reply and help

Regards,

SAM

Share this post


Link to post
Share on other sites
Quesney

Are you getting one of your msgbox answers or is it just going through as if it worked but not changing anything?


“Courage is being scared to death, but saddling up anyway”John Wayne

Share this post


Link to post
Share on other sites
Quesney
On 4/11/2017 at 8:56 AM, SAM1386 said:

Local $port=$key

Local $porterror

_CommSendStandard($port,$portError,"AT+CFUN=1,1")

I am not 100% sure how Autoit handles Capitalization and such but if it sees e and E as different then you could have an issue there


“Courage is being scared to death, but saddling up anyway”John Wayne

Share this post


Link to post
Share on other sites
SAM1386

@Quesney :- Thanks for the reply.

#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

The above mentioned code is giving me the output as "COM5" like $key = COM5 and I want to get that port number 5 like in my case '5' to a variable named $port.

So that I can pass that $port number  to below function which will reset my UE by AT command "AT+CFUN=1,1"

_CommSendStandard($port,$portError,"AT+CFUN=1,1")

The thing here is when I write it as :-

_CommSendStandard(5,$portError,"AT+CFUN=1,1") ; it works fine.


but when I write it like below it donot work fine in run time.

_CommSendStandard($port,$portError,"AT+CFUN=1,1")

 I want $port variable to take the $key value 5 in run time and set the $port value as 5 instead of setting it manually.

 

I think I am unable to make understand my problem :-(

Please help.

Regards,

SAM

 

Edited by SAM1386
forgot to quote

Share this post


Link to post
Share on other sites
SAM1386
On 4/13/2017 at 4:00 PM, Quesney said:

I am not 100% sure how Autoit handles Capitalization and such but if it sees e and E as different then you could have an issue there

 

 

@Quesney :- Thanks for the reply.

#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

The above mentioned code is giving me the output as "COM5" like $key = COM5 and I want to get that port number 5 like in my case '5' to a variable named $port.

So that I can pass that $port number  to below function which will reset my UE by AT command "AT+CFUN=1,1"

_CommSendStandard($port,$portError,"AT+CFUN=1,1")

The thing here is when I write it as :-

_CommSendStandard(5,$portError,"AT+CFUN=1,1") ; it works fine.


but when I write it like below it donot work fine in run time.

_CommSendStandard($port,$portError,"AT+CFUN=1,1")

 I want $port variable to take the $key value 5 in run time and set the $port value as 5 instead of setting it manually.

 

I think I am unable to make understand my problem :-(

Please help.

Regards,

SAM

Share this post


Link to post
Share on other sites
SAM1386

I think the output is "COM5" but  $port is taking only integer value 5. Is there any way to separate that "COM" and "5" part from COM5 so That I can give that 5 to $ port.

Regards,

SAM 

Share this post


Link to post
Share on other sites
232showtime
$key = "COM5"
$sr = StringRight($key, 1)
ConsoleWrite($sr & @CRLF)

 

Edited by 232showtime

ill get to that... i still need to learn and understand a lot of codes graduated.gif

Correct answer, learn to walk before you take on that marathon.

Share this post


Link to post
Share on other sites
SAM1386
On 4/15/2017 at 4:10 PM, 232showtime said:
$key = "COM5"
$sr = StringRight($key, 1)
ConsoleWrite($sr & @CRLF)

 

Thanks for the reply ...It worked for me as exactly I wanted.....

Thanks a lot

 

Regards,

SAM

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

    • martin
      By martin
      Although serial ports are disappearing, they can still be useful.
      Here is a COMMs UDF. It provides an easy way to use serial ports without the restrictions and problems some methods have. USB to serial is ok, binary data is ok.

      This UDF requires my comMG.dll which can be in either the script folder or the Windows folder by default, or in the path specified using the function _CommSetDllPath.

      Note the following shortcomings:

      the dll link below is 32 bit so it will not work with a 64 bit apps, but there is a 64 bit version in my post around 25th March 2018 for people to try.
      The strings and character functions are all AnsiChar.


      Functions in the UDF are

      _CommVersion
      _CommListPorts
      _CommSetPort
      _CommPortConnection
      _CommClearOutputBuffer
      _CommClearInputBuffer
      _CommGetInputcount
      _CommGetOutputcount
      _CommSendString
      _CommGetString
      _CommGetLine
      _CommReadByte
      _CommReadChar
      _CommSendByte
      _CommSendBreak; not tested!!!!!!!!!!
      _CommCloseport
      _CommSwitch
      _CommReadByteArray
      _CommSendByteArray
      _CommsetTimeouts
      _CommSetXonXoffProperties
      _CommSetRTS (NB these will not work if Hardware handshaking is selected because
      _CommSetDTR then these lines are controlled by the data being sent.)
      _CommSetDllPath
      _CommGetLineStates
      --------------------------------------------------------------------------------------------------------------------------------
      Go to Download Page For Commgv2
      Download includes the dll and udf.
      Most recent changes
      28th March 2014 - dll V2.83
      Correct error setting 6 data bits as 7.
      11th March 2014 dll V2.82
      Allow data bits of 4 to 8 instead of only 7 and 8.
      19th August 2013
      dll v2.81 removes some unwanted eroor message popups. Might not remove popups for some Windows versions.
      27th September 2012
      Correct error closing port. New version of UDF if V2.90, new dll is commg.dll V2.79. Thanks to tfabris.
      18th January 2012
      Corrected typo in UDF V 2.87, and uploaded as V2.88
      Increased max baud allowed by the dll from 200000 to 256000. New version now V2.78
      17th January 2012
      Modified thesleep addition to _CommGetLine so that reading data is not slowed down.

      14th January 2012
      Corrected _CommReadByte in UDF.
      Added sleep(20) to while loop in _CommGetLine to reduce CPU usage
      20th December 2011
      UDF version 2.86. - Changed function GetByte so it returned the error string given by the dll.
      Dll version 2.77 - removed an unwanted erro message dialogue from GetByte function. (Thanks funkey)

      4th December 2011
      New dll and example versions. Dll function SetPort corrected because it was not using the parameters passed for DTR and RTS.
      The example was setting flow control incorrectly: the settings for hardware handshaking and XON./XOFF were reversed.

      25th August 2011 corrected function _CommClosePort.

       
      Example corrected for setting parity and flow 22nd December 2013  (thanks to MichaelXMike)



      mgrefcommg CommgExample.au3
    • 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
    • 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  
    • Corleone221
      By Corleone221
       
      Hello All,
       
      How can I load  actual connect  Com Ports to combobox?
       
      Thanks.
       
      for example  this 3....(6,7,8)

       
       
×