Skip to content

Commit

Permalink
Initial Upload Extracted from Programming Assistant
Browse files Browse the repository at this point in the history
  • Loading branch information
W1BTR committed Sep 6, 2024
1 parent e5f6c6b commit 46363e7
Show file tree
Hide file tree
Showing 15 changed files with 1,329 additions and 0 deletions.
498 changes: 498 additions & 0 deletions Bin/CMDS.bat

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Bin/CMS.vbs
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
113 changes: 113 additions & 0 deletions Bin/COMCOMP.ps1
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
}
3 changes: 3 additions & 0 deletions Bin/COMLOG.log
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"
3 changes: 3 additions & 0 deletions Bin/COMLog.diff
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"
7 changes: 7 additions & 0 deletions Bin/COMSCOMP.bat
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 added Bin/COMpipe.exe
Binary file not shown.
Loading

0 comments on commit 46363e7

Please sign in to comment.