Skip to content

Instantly share code, notes, and snippets.

@flybayer
Last active August 29, 2015 14:17
Show Gist options
  • Save flybayer/ee6ea9e8b72d1c6d9b7e to your computer and use it in GitHub Desktop.
Save flybayer/ee6ea9e8b72d1c6d9b7e to your computer and use it in GitHub Desktop.
; -------------------------------------------------------------------------------
; This script automatically disconnects and/or connects Tera Term when the
; availability of a specified USB COM port changes.
;
; CONFIGURATION: Set the com ports you want this script to monitor in the
; ProcessUSBchangeEvent function below
;
; USAGE: This script can be ran by itself or included in another script
; with this line: #Include auto_connect_teraterm_usb_com_ports.ahk
;
; Tera Term MUST BE OPEN for this script to connect or disconnect.
;
; OPTIONAL: You can uncomment the line: DisplayConnectedTrayTip(monitor_port)
; if you want to show a tool tip when a specified com port becomes
; becomes available (even when Tera Term is not open).
; -------------------------------------------------------------------------------
; Message 0x219 is triggered by Windows when a USB device is connected or disconnected
OnMessage(0x219, "ProcessUSBchangeEvent")
ProcessUSBchangeEvent(wParam, lParam, msg, hwnd)
{
global checkTime
; Devices with multiple ports may send multiple 0x219 messages, checkTime helps filter these
if (A_TickCount > checkTime)
{
SetTitleMatchMode 2
PortsToMonitor := Object()
checkTime := A_TickCount + 2000
; -----------------------------
; SET COM PORTS TO MONITOR HERE
; -----------------------------
PortsToMonitor.Insert("COM47")
PortsToMonitor.Insert("COM52")
for index, monitor_port in PortsToMonitor
{
; If TeraTerm is still connected to the COM port, Windows will report that it's connected
; Therefore we must first disconnect it to really know if the port is present or not
DisconnectTeraTermIfNeeded(monitor_port)
; Allow time for the COM port to be released by TeraTerm
Sleep 50
If ComPortIsConnected(monitor_port)
{
ConnectTeraTerm(monitor_port)
; DisplayConnectedTrayTip(monitor_port)
}
; Give TeraTerm time to finish connecting before looping to the next port
Sleep 50
}
}
}
DisconnectTeraTermIfNeeded(com_port)
{
IfWinExist, %com_port% ahk_class VTWin32
{
IfWinNotActive, %com_port% ahk_class VTWin32, , WinActivate, %com_port% ahk_class VTWin32,
WinWaitActive, %com_port% ahk_class VTWin32,
WinMenuSelectItem, %com_port% ahk_class VTWin32,, File, Disconnect
}
}
ConnectTeraTerm(com_port)
{
IfWinExist, [disconnected] ahk_class VTWin32
{
IfWinNotActive, [disconnected] ahk_class VTWin32, , WinActivate, [disconnected] ahk_class VTWin32,
WinWaitActive, [disconnected] ahk_class VTWin32,
WinMenuSelectItem, [disconnected],, File, New connection
WinActivate, Tera Term: New connection
WinWaitActive, Tera Term: New connection
Control, ChooseString, %com_port%, ComboBox4
ControlSend, OK, {enter}
}
}
DisplayConnectedTrayTip(com_port)
{
; Only display TrayTip for 1.5 seconds
TrayTip, %com_port% Loaded, %A_Space%
Sleep 1500
TrayTip
}
ComPortIsConnected(TargetPort)
{
FoundPort := 0
; Loop through connected COM ports until target is found
Loop, HKLM, HARDWARE\DEVICEMAP\SERIALCOMM\
{
RegRead, PortName
IfInString, PortName, %TargetPort%
{
FoundPort := PortName
break
}
}
return FoundPort
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment