Skip to content
Permalink
Browse files

AttachedSerialDevices initial commit

  • Loading branch information...
aderusha committed Jun 13, 2019
1 parent 799f0f1 commit 1597cfbf7e3129b241ef318fa27e8007c20e36a9
@@ -0,0 +1,39 @@
# Attached Serial Devices Dynamic Folder

This script utilizes PowerShell to generate a list of available serial devices, along with RTS Custom Properties defining speeds and framing settings, to generate a set of Terminal connections for each combination of port/speed/framing.

The resulting folder structure will look something like the following:

```
Attached Serial Devices
|-- USB Serial Device (COM12)
| |-- COM12 9600 8N1
| |-- COM12 9600 7E1
| |-- COM12 19200 8N1
| `-- COM12 19200 7E1
`-- USB-SERIAL CH340 (COM34)
|-- COM34 9600 8N1
|-- COM34 9600 7E1
|-- COM34 19200 8N1
`-- COM34 19200 7E1
```

## Requirements

This solution only works under Windows due to its use of WMI to identify available COM ports.

## Custom Properties

### Port Speeds

This field must contain comma-separated list of serial port speeds in numeric format.

* **Example 1**: `9600`
* **Example 2**: `9600,19200,115200`

### Frame Settings

This field must contain comma-separated list of serial port framing standards (8N1, 7E1, etc) in alphanumeric format, and each entry must be in double quotes.

* **Example 1**: `"8N1"`
* **Example 2**: `"8N1","7E1"`
@@ -0,0 +1 @@
{"Name":"Dynamic Folder Export","Objects":[{"Type":"DynamicFolder","Name":"Attached Serial Devices","Description":"Connect a serial terminal to currently-attached serial devices, see Custom Properties to set speeds and framing options","Notes":"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n\t<head>\r\n\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>\r\n\t\t</title>\r\n\t\t<style type=\"text/css\">\r\n\t\t\t.cs8D73F250{text-align:left;text-indent:0pt;margin:12pt 0pt 12pt 0pt;line-height:1.2}\r\n\t\t\t.cs7C1541EC{color:#000000;background-color:transparent;font-family:'Microsoft Sans Serif';font-size:24pt;font-weight:normal;font-style:normal;}\r\n\t\t\t.csAD7A2888{text-align:left;text-indent:0pt;margin:12pt 0pt 12pt 0pt}\r\n\t\t\t.csEE0F4FBE{color:#000000;background-color:transparent;font-family:'Microsoft Sans Serif';font-size:10.5pt;font-weight:normal;font-style:normal;}\r\n\t\t\t.cs24EF061{text-align:left;text-indent:0pt;margin:0pt 0pt 0pt 0pt;line-height:13.57pt}\r\n\t\t\t.cs317908D7{color:#000000;background-color:transparent;font-family:'Microsoft Sans Serif';font-size:18pt;font-weight:normal;font-style:normal;}\r\n\t\t\t.cs183562EF{color:#000000;background-color:transparent;font-family:'Microsoft Sans Serif';font-size:13.5pt;font-weight:normal;font-style:normal;}\r\n\t\t\t.csA606D5E5{text-align:left;margin:0pt 0pt 0pt 0pt;list-style-type:disc;color:#000000;background-color:transparent;font-family:Arial;font-size:10.5pt;font-weight:normal;font-style:normal}\r\n\t\t\t.csACBE093A{color:#000000;background-color:transparent;font-family:'Microsoft Sans Serif';font-size:10.5pt;font-weight:bold;font-style:normal;}\r\n\t\t\t.cs2654AE3A{text-align:left;text-indent:0pt;margin:0pt 0pt 0pt 0pt}\r\n\t\t\t.cs23FB0664{color:#000000;background-color:transparent;font-family:'Times New Roman';font-size:12pt;font-weight:normal;font-style:normal;}\r\n\t\t</style>\r\n\t</head>\r\n\t<body>\r\n\t\t<h1 class=\"cs8D73F250\">\r\n\t\t\t<a name=\"_dx_frag_StartFragment\"></a><a name=\"ATTACHED-SERIAL-DEVICES-DYNAMIC-FOLDER\"></a><a name=\"ATTACHED-SERIAL-DEVICES-DYNAMIC-FOLDER\"></a><a name=\"_dx_frag_StartFragment\"></a><a name=\"_dx_frag_EndFragment\"></a><span class=\"cs7C1541EC\">Attached Serial Devices Dynamic Folder</span></h1>\r\n\t\t<p class=\"csAD7A2888\"><span class=\"csEE0F4FBE\">This script utilizes PowerShell to generate a list of available serial devices, along with RTS Custom Properties defining speeds and framing settings, to generate a set of Terminal connections for each combination of port/speed/framing.</span></p><p class=\"csAD7A2888\"><span class=\"csEE0F4FBE\">The resulting folder structure will look something like the following:</span></p><p class=\"cs24EF061\" style=\"tab-stops:left 45.8pt left 91.6pt left 137.4pt left 183.2pt left 229pt left 274.8pt left 320.6pt left 366.4pt left 412.2pt left 458pt left 503.8pt left 595.4pt left 641.2pt left 687pt left 732.8pt;\"><span class=\"csEE0F4FBE\">Attached Serial Devices</span></p><p class=\"cs24EF061\" style=\"tab-stops:left 45.8pt left 91.6pt left 137.4pt left 183.2pt left 229pt left 274.8pt left 320.6pt left 366.4pt left 412.2pt left 458pt left 503.8pt left 595.4pt left 641.2pt left 687pt left 732.8pt;\"><span class=\"csEE0F4FBE\">|-- USB Serial Device (COM12)</span></p><p class=\"cs24EF061\" style=\"tab-stops:left 45.8pt left 91.6pt left 137.4pt left 183.2pt left 229pt left 274.8pt left 320.6pt left 366.4pt left 412.2pt left 458pt left 503.8pt left 595.4pt left 641.2pt left 687pt left 732.8pt;\"><span class=\"csEE0F4FBE\">| &nbsp;&nbsp;|-- COM12 9600 8N1</span></p><p class=\"cs24EF061\" style=\"tab-stops:left 45.8pt left 91.6pt left 137.4pt left 183.2pt left 229pt left 274.8pt left 320.6pt left 366.4pt left 412.2pt left 458pt left 503.8pt left 595.4pt left 641.2pt left 687pt left 732.8pt;\"><span class=\"csEE0F4FBE\">| &nbsp;&nbsp;|-- COM12 9600 7E1</span></p><p class=\"cs24EF061\" style=\"tab-stops:left 45.8pt left 91.6pt left 137.4pt left 183.2pt left 229pt left 274.8pt left 320.6pt left 366.4pt left 412.2pt left 458pt left 503.8pt left 595.4pt left 641.2pt left 687pt left 732.8pt;\"><span class=\"csEE0F4FBE\">| &nbsp;&nbsp;|-- COM12 19200 8N1</span></p><p class=\"cs24EF061\" style=\"tab-stops:left 45.8pt left 91.6pt left 137.4pt left 183.2pt left 229pt left 274.8pt left 320.6pt left 366.4pt left 412.2pt left 458pt left 503.8pt left 595.4pt left 641.2pt left 687pt left 732.8pt;\"><span class=\"csEE0F4FBE\">| &nbsp;&nbsp;`-- COM12 19200 7E1</span></p><p class=\"cs24EF061\" style=\"tab-stops:left 45.8pt left 91.6pt left 137.4pt left 183.2pt left 229pt left 274.8pt left 320.6pt left 366.4pt left 412.2pt left 458pt left 503.8pt left 595.4pt left 641.2pt left 687pt left 732.8pt;\"><span class=\"csEE0F4FBE\">`-- USB-SERIAL CH340 (COM34)</span></p><p class=\"cs24EF061\" style=\"tab-stops:left 45.8pt left 91.6pt left 137.4pt left 183.2pt left 229pt left 274.8pt left 320.6pt left 366.4pt left 412.2pt left 458pt left 503.8pt left 595.4pt left 641.2pt left 687pt left 732.8pt;\"><span class=\"csEE0F4FBE\">&nbsp; &nbsp;&nbsp;|-- COM34 9600 8N1</span></p><p class=\"cs24EF061\" style=\"tab-stops:left 45.8pt left 91.6pt left 137.4pt left 183.2pt left 229pt left 274.8pt left 320.6pt left 366.4pt left 412.2pt left 458pt left 503.8pt left 595.4pt left 641.2pt left 687pt left 732.8pt;\"><span class=\"csEE0F4FBE\">&nbsp; &nbsp;&nbsp;|-- COM34 9600 7E1</span></p><p class=\"cs24EF061\" style=\"tab-stops:left 45.8pt left 91.6pt left 137.4pt left 183.2pt left 229pt left 274.8pt left 320.6pt left 366.4pt left 412.2pt left 458pt left 503.8pt left 595.4pt left 641.2pt left 687pt left 732.8pt;\"><span class=\"csEE0F4FBE\">&nbsp; &nbsp;&nbsp;|-- COM34 19200 8N1</span></p><p class=\"cs24EF061\" style=\"tab-stops:left 45.8pt left 91.6pt left 137.4pt left 183.2pt left 229pt left 274.8pt left 320.6pt left 366.4pt left 412.2pt left 458pt left 503.8pt left 595.4pt left 641.2pt left 687pt left 732.8pt;\"><span class=\"csEE0F4FBE\">&nbsp; &nbsp;&nbsp;`-- COM34 19200 7E1</span></p><h2 class=\"csAD7A2888\" style=\"tab-stops:left 45.8pt left 91.6pt left 137.4pt left 183.2pt left 229pt left 274.8pt left 320.6pt left 366.4pt left 412.2pt left 458pt left 503.8pt left 595.4pt left 641.2pt left 687pt left 732.8pt;\">\r\n\t\t\t<a name=\"REQUIREMENTS\"></a><span class=\"cs317908D7\">Requirements</span></h2>\r\n\t\t<p class=\"csAD7A2888\"><span class=\"csEE0F4FBE\">This solution only works under Windows due to its use of WMI to identify available COM ports.</span></p><h2 class=\"csAD7A2888\">\r\n\t\t\t<a name=\"CUSTOM-PROPERTIES\"></a><span class=\"cs317908D7\">Custom Properties</span></h2>\r\n\t\t<h3 class=\"csAD7A2888\">\r\n\t\t\t<a name=\"PORT-SPEEDS\"></a><span class=\"cs183562EF\">Port Speeds</span></h3>\r\n\t\t<p class=\"csAD7A2888\"><span class=\"csEE0F4FBE\">This field must contain comma-separated list of serial port speeds in numeric format.</span></p><ul style=\"margin-top:0;margin-bottom:0;\">\r\n\t\t\t<li class=\"csA606D5E5\"><span class=\"csACBE093A\">Example 1</span><span class=\"csEE0F4FBE\">:&nbsp;</span><span class=\"csEE0F4FBE\">9600</span></li><li class=\"csA606D5E5\"><span class=\"csACBE093A\">Example 2</span><span class=\"csEE0F4FBE\">:&nbsp;</span><span class=\"csEE0F4FBE\">9600,19200,115200</span></li></ul>\r\n\t\t<h3 class=\"csAD7A2888\">\r\n\t\t\t<a name=\"FRAME-SETTINGS\"></a><span class=\"cs183562EF\">Frame Settings</span></h3>\r\n\t\t<p class=\"csAD7A2888\"><span class=\"csEE0F4FBE\">This field must contain comma-separated list of serial port framing standards (8N1, 7E1, etc) in alphanumeric format, and each entry must be in double quotes.</span></p><ul style=\"margin-top:0;margin-bottom:0;\">\r\n\t\t\t<li class=\"csA606D5E5\"><span class=\"csACBE093A\">Example 1</span><span class=\"csEE0F4FBE\">:&nbsp;</span><span class=\"csEE0F4FBE\">&quot;8N1&quot;</span></li><li class=\"csA606D5E5\"><span class=\"csACBE093A\">Example 2</span><span class=\"csEE0F4FBE\">:&nbsp;</span><span class=\"csEE0F4FBE\">&quot;8N1&quot;,&quot;7E1&quot;</span><a name=\"_dx_frag_EndFragment\"></a></li></ul>\r\n\t\t<p class=\"cs2654AE3A\"><span class=\"cs23FB0664\">&nbsp;</span></p></body>\r\n</html>\r\n","CustomProperties":[{"Name":"Port Speeds","Type":"Text","Value":"9600,19200,115200"},{"Name":"Frame Settings","Type":"Text","Value":"\"8N1\",\"7E1\""}],"ScriptInterpreter":"powershell","DynamicCredentialScriptInterpreter":"json","Script":"$ErrorActionPreference = \"Stop\"\r\n$comPortSpeeds = @($CustomProperty.PortSpeeds$)\r\n$comPortFrameSettings = @($CustomProperty.FrameSettings$)\r\n# Collect a list of avilable serial ports from WMI. See here for discussion: https://stackoverflow.com/questions/19840811/list-of-serialports-queried-using-wmi-differs-from-devicemanager\r\n$comPorts = Get-WmiObject -query 'SELECT * FROM Win32_PnPEntity WHERE ClassGuid=\"{4d36e978-e325-11ce-bfc1-08002be10318}\"'\r\n$folders = @()\r\nForEach ($comPort in $comPorts) {\r\n $connections = @()\r\n $comPortCaption = $comPort.Caption;\r\n if ($comPortCaption -match '.*\\((COM\\d+)\\)') {\r\n $comPortDevice = $Matches[1]\r\n }\r\n else {\r\n continue\r\n }\r\n ForEach ($comPortSpeed in $comPortSpeeds) {\r\n ForEach ($comPortFrameSetting in $comPortFrameSettings) {\r\n if ($comPortFrameSetting -match '^(\\d)([NOEMSnoems])(\\d)$') {\r\n [int]$comPortDataBits = $Matches[1]\r\n $comPortParityAlpha = $Matches[2].ToUpper()\r\n [int]$comPortStopBits = $Matches[3]\r\n }\r\n else {\r\n continue\r\n }\r\n if ($comPortParityAlpha -eq \"N\") {\r\n [int]$comPortParity = \"0\"\r\n }\r\n elseif ($comPortParityAlpha -eq \"O\") {\r\n [int]$comPortParity = \"1\"\r\n }\r\n elseif ($comPortParityAlpha -eq \"E\") {\r\n [int]$comPortParity = \"2\"\r\n }\r\n elseif ($comPortParityAlpha -eq \"M\") {\r\n [int]$comPortParity = \"3\"\r\n }\r\n elseif ($comPortParityAlpha -eq \"S\") {\r\n [int]$comPortParity = \"4\"\r\n }\r\n else {\r\n continue\r\n }\r\n $connectionProperties = New-Object PSCustomObject -Property @{\r\n \"BaudRate\" = $comPortSpeed;\r\n \"DataBits\" = $comPortDataBits;\r\n \"Parity\" = $comPortParity;\r\n \"StopBits\" = $comPortStopBits;\r\n }\r\n $connection = New-Object PSCustomObject -Property @{\r\n \"Type\" = \"TerminalConnection\";\r\n \"TerminalConnectionType\" = \"SerialPort\";\r\n \"Name\" = \"$comPortDevice $comPortSpeed $comPortFrameSetting\";\r\n \"SerialPortName\" = $comPortDevice;\r\n \"Properties\" = $connectionProperties;\r\n }\r\n $connections += $connection\r\n }\r\n }\r\n $folder = New-Object PSCustomObject -Property @{\r\n \"Type\" = \"Folder\";\r\n \"Name\" = $comPortCaption;\r\n \"Objects\" = $connections;\r\n }\r\n $folders += $folder\r\n}\r\n@{ Objects = $folders } | ConvertTo-Json -Depth 5"}]}

0 comments on commit 1597cfb

Please sign in to comment.
You can’t perform that action at this time.