Skip to content

Instantly share code, notes, and snippets.

@steelywing
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steelywing/48f965650dc3cf5030a3 to your computer and use it in GitHub Desktop.
Save steelywing/48f965650dc3cf5030a3 to your computer and use it in GitHub Desktop.
Auto detect serial port to execute putty
# Wrap putty-serial.vbs to exe
Name "PuTTY Serial"
Icon "putty.ico"
OutFile "putty-serial.exe"
InstallDir "$TEMP"
RequestExecutionLevel user
SilentInstall silent
Section
SetOutPath $INSTDIR
File "putty.exe"
File "putty-serial.vbs"
ExecShell "" '"$TEMP\putty-serial.vbs"'
SectionEnd
Const PUTTY_PATH = "putty"
' Return array of serial port number
Function getCOM()
Dim fso, com()
ReDim com(-1)
Set fso = CreateObject("Scripting.FileSystemObject")
For i = 1 To 20
Dim f, found
On Error Resume Next
Set f = fso.OpenTextFile("COM" & i & ":", 2)
' Err.Clear
found = (Err.Number = 0)
f.Close
On Error Goto 0
If found Then
ReDim Preserve com(UBound(com) + 1)
com(UBound(com)) = i
End If
Next
getCOM = com
End Function
Dim WshShell, com
Set WshShell = WScript.CreateObject("WScript.Shell")
com = getCOM()
If UBound(com) = -1 Then
WScript.Echo "No COM found"
WScript.Quit
End If
If UBound(com) = 0 Then
CreateObject("WScript.Shell").Exec(PUTTY_PATH & " -serial COM" & com(0))
WScript.Quit
End If
' COM more than 1
For i = 0 to UBound(com)
Dim answer
answer = WshShell.Popup("Use COM" & com(i) & " ?", , "COM port", 4 + 32)
If answer = 6 Then
CreateObject("WScript.Shell").Exec(PUTTY_PATH & " -serial COM" & com(i))
WScript.Quit
End If
Next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment