1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
   | function Get-PLCData {     [CmdletBinding()]     param(         [Parameter(Mandatory = $true)]         [string]$PLCAddress,                  [Parameter()]         [int]$Port = 502,                  [Parameter()]         [string[]]$Tags,                  [Parameter()]         [int]$PollingInterval = 1000,                  [Parameter()]         [string]$Protocol = "ModbusTCP"     )          try {         $plcData = [PSCustomObject]@{             PLCAddress = $PLCAddress             Port = $Port             Protocol = $Protocol             Tags = $Tags             LastUpdate = Get-Date             Values = @{}         }                           switch ($Protocol) {             "ModbusTCP" {                 $data = Get-ModbusData -Address $PLCAddress `                     -Port $Port `                     -Tags $Tags             }             "SiemensS7" {                 $data = Get-SiemensS7Data -Address $PLCAddress `                     -Port $Port `                     -Tags $Tags             }             "AllenBradley" {                 $data = Get-AllenBradleyData -Address $PLCAddress `                     -Port $Port `                     -Tags $Tags             }         }                           foreach ($tag in $Tags) {             if ($data.ContainsKey($tag)) {                 $plcData.Values[$tag] = [PSCustomObject]@{                     Value = $data[$tag]                     Timestamp = Get-Date                     Quality = "Good"                 }             }             else {                 $plcData.Values[$tag] = [PSCustomObject]@{                     Value = $null                     Timestamp = Get-Date                     Quality = "Bad"                 }             }         }                  return $plcData     }     catch {         Write-Error "PLC数据采集失败:$_"         return $null     } }
  function Start-PLCDataCollection {     [CmdletBinding()]     param(         [Parameter(Mandatory = $true)]         [string]$PLCAddress,                  [Parameter()]         [string[]]$Tags,                  [Parameter()]         [int]$Interval = 1000,                  [Parameter()]         [string]$LogPath,                  [Parameter()]         [scriptblock]$OnDataReceived     )          try {         $job = Start-Job -ScriptBlock {             param($PLCAddress, $Tags, $Interval, $LogPath, $OnDataReceived)                          while ($true) {                 $data = Get-PLCData -PLCAddress $PLCAddress -Tags $Tags                                  if ($LogPath) {                     $data | ConvertTo-Json | Out-File -FilePath $LogPath -Append                 }                                  if ($OnDataReceived) {                     $OnDataReceived.Invoke($data)                 }                                  Start-Sleep -Milliseconds $Interval             }         } -ArgumentList $PLCAddress, $Tags, $Interval, $LogPath, $OnDataReceived                  return $job     }     catch {         Write-Error "PLC数据采集任务启动失败:$_"         return $null     } }
  |