Jump to content

Serial Port Find (port check)


Recommended Posts

Hell all,

I need your help for something that is stucking me since some days (since I have no clue how to handle this)

I am using a the dial code from Dale and customized by a member to get an answer from the modem when command is sent. In this case, the expected answer is "OK".

$Time2Wait = 5000
dim $FromModem = ""
$ModemString = "ATH" & ";" & @CR
$com = ObjCreate ("NETCommOCX.NETComm")
With $com
.CommPort = 9
.PortOpen = True
.Settings = "9600,N,8,1"
.InBufferCount = 0
.Output = $ModemString
EndWith
$StopWatch = TimerInit()
While 1
If $com.InBufferCount Then
$FromModem = $FromModem & $com.InputData
If StringInStr($FromModem, "OK") Then
    MsgBox (0,"","OK")
ExitLoop
EndIf
EndIf
If (TimerDiff($StopWatch) > $Time2Wait) Then
ExitLoop
EndIf
WEnd

1st - Problem is that I don't receive OK from the modem.

2nd problem is that the port comm is always changing after rebooting or unplugging the phone.

I would like to know is a charitable soul could help me to find the right serial port the phone is connected to.

The phone is a Sony or a Samsung. My wish would be to plug the phone launch the script so that it scans the serial port for the right one and then lauch my phone command.

Thank you all for reading and helping me out.

Link to comment
Share on other sites

  • 1 year later...
  • 2 weeks later...

I wrote this a while ago for getting the COM port for a Diabetic Meter connected via USB serial cable

Opt("TrayMenuMode", 1)
$DEBUG = FALSE
$ERROR_COM_PORT_NOT_FOUND = -1

$Port = _FindCOM("Prolific USB-to-Serial Comm Port", "USB")

If $Port = $ERROR_COM_PORT_NOT_FOUND Then
 MsgBox(16, "COM Port Finder", "No Data Cable Detected")
Else
 MsgBox(64, "COM Port Finder", "Data Cable Found on Port " & $Port)
EndIf

#cs
##############################################################################################################
# Function: _FindCOM($inDeviceName, $inClassKey)
#
# $inDeviceName  - is the registry 'FriendlyName' of the device you want to find
# $inClassKey  - is the Bass class under HKLMSystemEnum where you want to search, eg USB, UMPPORT
#
# Function searches through all the device GUID's looking for a match and then compares the 'PortName' from
# 'Device Parameters' with the open COM ports in HKLMSystemHardwareSerialCOMM
#
# Function returns the COM port name if an active COM port is found for the device, eg 'COM1'
# or -1 if an active COM port is NOT found
#
##############################################################################################################
#ce
Func _FindCOM($inDeviceName, $inClassKey)
 
 If $DEBUG Then ConsoleWrite("*** Getting COM Port for: " & $inDeviceName & @CRLF)
 
 Local $i = 1
 
 $ClassKey = "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetEnum" & $inClassKey
 
 ; DEVICE LOOP - Loop through all DEVICE GUID's
 While 1
  
  ; Get the Device GUID for this loop
  $DeviceKey = RegEnumKey($ClassKey, $i)
  If @error Then ExitLoop
   
  Local $j = 1
  
  ; INSTANCE LOOP - Loop through all instances of the device (if it has been connected to different ports)
  While 1
   
   ; Get the Device Instance for this loop
   $InstanceKey = RegEnumKey($ClassKey & "" & $DeviceKey, $j)
   If @error Then ExitLoop
   
   ; Read the friendly name
   $FriendlyName = RegRead($ClassKey & "" & $DeviceKey & "" & $InstanceKey, "FriendlyName")
   If @error Then ExitLoop
  
   ; The registry FriendlyName has the COM Port in brackets so need to remove this before trying the match
   If StringLeft($FriendlyName, StringInStr($FriendlyName, " (COM")-1) = $inDeviceName Then
    ; We have a match so get the COM port for this instance
    $COMtoCheck = RegRead($ClassKey & "" & $DeviceKey & "" & $InstanceKey & "Device Parameters", "PortName")
    If $DEBUG Then ConsoleWrite(@TAB & "Device Found at: " & $ClassKey & "" & $DeviceKey & "" & $InstanceKey & " using port " & $COMtoCheck & @CRLF)
         
    ; Check if this is an Active COM Port
    Local $k = 1
    ; COM PORT LOOP - Loop through all the open COM ports
    While 1
     
     ; Get the Active COM port for this loop
     $ActiveCOMPort = RegRead("HKEY_LOCAL_MACHINEHARDWAREDEVICEMAPSERIALCOMM", RegEnumVal("HKEY_LOCAL_MACHINEHARDWAREDEVICEMAPSERIALCOMM", $k))
     If @Error then ExitLoop
     
     
     If $ActiveComPort = $COMtoCheck Then
      ; If the Active COM port for this loop equals our Device COM port then we have found the correct port
      $Active = True
      ExitLoop
     Else
      ; Otherwise keep checking the other Active COM ports
      $Active = False
     EndIf
          
     $k += 1
    WEND
    
    If $Active Then
     ; If we set active variable to true then return the COM port
     If $DEBUG Then ConsoleWrite(@TAB & "Port " & $COMtoCheck & " is ACTIVE" & @CRLF)
     If $DEBUG Then ConsoleWrite("*** RETURN: " & $COMtoCheck & @CRLF)
     Return $COMtoCheck
    Else
     ; The COM port on this instance is not active
     If $DEBUG Then ConsoleWrite(@TAB & "Port " & $COMtoCheck & " is NOT ACTIVE" & @CRLF)
    EndIf
   
   EndIf
    
   $j += 1
  
  WEnd
 
  $i += 1
 
 WEnd
 
 ; Could Not Find Active COM Port for matching device
 If $DEBUG Then ConsoleWrite("*** RETURN: -1" & @CRLF)
 Return $ERROR_COM_PORT_NOT_FOUND
 
EndFunc
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...