Advertisement
Seeker

DisplayENVI.ps1

Sep 11th, 2011
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### DisplayENVI.ps1 ############################################
  2. #
  3. # Routine to display power recorded by a Current Cost Monitor.
  4. #              
  5. ################################################################
  6.  
  7.  
  8. $loop_forever = $true
  9. $CommPort_in="COM1"
  10. $BaudRate=57600
  11. $Sensor=0
  12.  
  13. #$template=[xml]("<msg><src></src><dsb></dsb><time></time><tmpr></tmpr><sensor></sensor><id></id><type></type><ch1><watts></watts></ch1><ch2><watts></watts></ch2><ch3><watts></watts></ch3></msg>")
  14. [double]$CH1 = 0.0
  15. [double]$CH2 = 0.0
  16. [double]$CH3 = 0.0
  17. [double]$Total = 0.0
  18. $TimeNow=" "
  19. #########################################3######################
  20. $port_in= new-Object System.IO.Ports.SerialPort $CommPort_in,$BaudRate,None,8,one
  21. $port_in.Open()
  22.  
  23. $data=$port_in.Readline() #Discard a possibly partial msg
  24. #
  25.  
  26. do {
  27.    $data=$port_in.Readline()
  28.    #Write-Host ($data)
  29.    #Remove any chars (probably noise) received before <msg
  30.    if ((!$data.StartsWith("<msg>")) -or (!$data.EndsWith("</msg>")))
  31.       {$Data =$data.substring($data.IndexOf("<"),$data.LastIndexOf(">") - $data.IndexOf("<")+1)
  32.       }
  33.    
  34.    [xml]$data_in=$data #may still have noise inside the message. trap error and continue
  35.    #---------------------------------------------------
  36.    if ($data_in.msg.sensor -eq $Sensor)
  37.       {$CH1=$data_in.msg.ch1.watts    
  38.        $CH2=$data_in.msg.ch2.watts
  39.        $CH3=$data_in.msg.ch3.watts
  40.        $Total = $CH1 + $CH2 + $CH3
  41.        $TimeNow=$data_in.msg.time
  42.  
  43.        Write-Host ("Sensor=$Sensor, Time=$TimeNow Total=$Total, Ch1=$CH1, Ch2=$CH2, Ch3=$CH3")  
  44.    
  45.       }
  46.        
  47.    else
  48.       {
  49.        # All other sensors ignored
  50.       }
  51.    #----------------------------------------------------
  52.    } While ($loop_forever='true')
  53. $port_in.close()
  54. Write-Host ("DisplayENVI completed")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement