-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Upload Extracted from Programming Assistant
- Loading branch information
Showing
15 changed files
with
1,329 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Set WshShell = CreateObject("WScript.Shell") | ||
WshShell.Run "cmd /c ""Bin\COMSCOMP.bat""", 0, False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
$counter = 0 | ||
# Define the path for the list file where COM port information is stored | ||
$listFilePath = "Bin\COMLOG.log" | ||
# Define the path for storing the name of the most recently added COM port | ||
$recentPortFilePath = "Bin\RecentCOM.log" | ||
|
||
# Clear the RecentCOM.log and COMLOG.log files at the start of the script | ||
if (Test-Path $recentPortFilePath) { | ||
Remove-Item $recentPortFilePath -Force | ||
} | ||
if (Test-Path $listFilePath) { | ||
Remove-Item $listFilePath -Force | ||
} | ||
Write-Host "First Run" | ||
$FirstRun = $true | ||
|
||
# Retrieves the current COM ports and their descriptions from the system | ||
function Get-ComPortsWithDescriptions { | ||
$comPorts = @{} | ||
$currentPorts = [System.IO.Ports.SerialPort]::GetPortNames() | ||
if ($currentPorts.Count -gt 0) { | ||
$wmiPorts = Get-WmiObject Win32_PnPEntity | Where-Object { $_.Name -match '^(.*\((COM\d+)\))$' } | ||
foreach ($port in $wmiPorts) { | ||
if ($port.Name -match '^(.*\((COM\d+)\))$') { | ||
$name = $matches[2] | ||
$description = $matches[1] | ||
$comPorts[$name] = $description | ||
} | ||
} | ||
} | ||
return $comPorts | ||
} | ||
|
||
# Logs the current COM ports and their descriptions to a file, marking the most recently added port | ||
function LogComPortsToFile { | ||
$currentPortsInfo = Get-ComPortsWithDescriptions | ||
$previousPorts = @{} | ||
if (Test-Path $listFilePath) { | ||
# Import previous ports and convert to a hashtable for easier lookup | ||
$importedPorts = Import-Csv $listFilePath | ||
foreach ($port in $importedPorts) { | ||
$previousPorts[$port.Name] = $port | ||
} | ||
} | ||
|
||
$mostRecentPort = if (Test-Path $recentPortFilePath) { Get-Content $recentPortFilePath } else { $null } | ||
|
||
$listUpdated = $false | ||
# Determine if there's a change in the COM ports list | ||
if ($currentPortsInfo.Count -ne $previousPorts.Count) { | ||
$listUpdated = $true | ||
Write-Host "List Updated because count has changed" | ||
} else { | ||
foreach ($port in $currentPortsInfo.Keys) { | ||
if (-not $previousPorts.ContainsKey($port) -or $previousPorts[$port].Description -ne $currentPortsInfo[$port]) { | ||
$listUpdated = $true | ||
break | ||
} | ||
} | ||
} | ||
|
||
if ($listUpdated) { | ||
$newPortName = $null | ||
$currentPortsInfo.Keys | ForEach-Object { | ||
if (-not $previousPorts.ContainsKey($_)) { | ||
$newPortName = $_ | ||
$mostRecentPort = $newPortName | ||
Set-Content -Path $recentPortFilePath -Value $mostRecentPort | ||
if ($FirstRun) { | ||
Set-Content -Path $recentPortFilePath -Value "Null" | ||
$mostRecentPort = "NULL" | ||
Write-Host "Not setting to true because FirstRun is $FirstRun" | ||
} | ||
} | ||
} | ||
|
||
$portList = $currentPortsInfo.GetEnumerator() | ForEach-Object { | ||
$name = $_.Key | ||
$description = $_.Value | ||
$isNew = $name -eq $mostRecentPort | ||
[PSCustomObject]@{ | ||
Name = $name | ||
Description = $description | ||
IsNew = $isNew | ||
} | ||
} | ||
|
||
$portList | Export-Csv $listFilePath -NoTypeInformation -Force | ||
Write-Host "COM port list updated." | ||
} | ||
} | ||
|
||
function CheckAndExit { | ||
Write-Host "$counter" | ||
if ($counter % 10 -eq 0) { | ||
$cmdProcesses = Get-Process cmd -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowTitle -like "*Programming Assistant*" } | ||
if ($cmdProcesses) { | ||
Write-Host "Programming Assistant is running." | ||
} else { | ||
Write-Host "Programming Assistant is not running. Closing..." | ||
Exit | ||
} | ||
} | ||
} | ||
|
||
# Infinite loop to continuously monitor COM ports and log changes | ||
while ($true) { | ||
Start-Sleep -Seconds 3 # Adjust the frequency as needed | ||
LogComPortsToFile | ||
$FirstRun = $false | ||
$counter++ | ||
CheckAndExit | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"Name","Description","IsNew" | ||
"COM40","Intel(R) Active Management Technology - SOL (COM40)","False" | ||
"COM16","Communications Port (COM16)","False" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"Name","Description","IsNew" | ||
"COM40","Intel(R) Active Management Technology - SOL (COM40)","False" | ||
"COM16","Communications Port (COM16)","False" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@echo off | ||
title Prog Assistant COM Companion | ||
echo COM Companion is running... | ||
echo Close this window to stop. | ||
powershell -ExecutionPolicy Bypass -File "Bin\COMCOMP.ps1">nul | ||
timeout /t 3 | ||
exit |
Binary file not shown.
Oops, something went wrong.