function asciiToHex($a) { $b = $a.ToCharArray(); Foreach ($element in $b) {$c = $c + [System.String]::Format("{0:X2}", [System.Convert]::ToUInt32($element)) + ";"} # [System.Convert]::ToUInt32($element)) + ";"} $c } $port= new-Object System.IO.Ports.SerialPort COM7,9600,None,8,1 $port.Encoding = [System.Text.Encoding]::GetEncoding(28591) $port.open() # Kommando für Paket 3 schicken $port.Readtimeout = 10000 $port.Write([byte[]] (221, 165, 3, 0, 255, 253, 119), 0, 7) Start-Sleep -Milliseconds 2000 $text = $port.ReadExisting() $hex = asciiToHex($text) $hex_array = $hex.split(";") # Spannung $hex_volt = $hex_array[4]+$hex_array[5] $volt = ([uint32]"0x$hex_volt" -as [double]) / 100 # Strom: $hex_curr = $hex_array[6]+$hex_array[7] $current_real_bms = ([int16]"0x$hex_curr" -as [double]) / 100 #SOC $hex_soc = $hex_array[23] $soc_bms = [uint32]"0x$hex_soc" Write-host "Paket 3: Spannung" $volt"V Strom" $current_real_bms"A SOC" $soc_bms"%" # Kommando für Paket 4 schicken $port.Write([byte[]] (221, 165, 4, 0, 255, 252, 119), 0, 7) Start-Sleep -Milliseconds 2000 $text = $port.ReadExisting() $hex = asciiToHex($text) $hex_array = $hex.split(";") # Zelle 1 $hex_volt = $hex_array[4]+$hex_array[5] $volt_1 = ([uint32]"0x$hex_volt" -as [double]) / 1000 # Zelle 2 $hex_volt = $hex_array[6]+$hex_array[7] $volt_2 = ([uint32]"0x$hex_volt" -as [double]) / 1000 # Zelle 3 $hex_volt = $hex_array[8]+$hex_array[9] $volt_3 = ([uint32]"0x$hex_volt" -as [double]) / 1000 # Zelle 4 $hex_volt = $hex_array[10]+$hex_array[11] $volt_4 = ([uint32]"0x$hex_volt" -as [double]) / 1000 # Zelle 5 $hex_volt = $hex_array[12]+$hex_array[13] $volt_5 = ([uint32]"0x$hex_volt" -as [double]) / 1000 # Zelle 6 $hex_volt = $hex_array[14]+$hex_array[15] $volt_6 = ([uint32]"0x$hex_volt" -as [double]) / 1000 # Zelle 7 $hex_volt = $hex_array[16]+$hex_array[17] $volt_7 = ([uint32]"0x$hex_volt" -as [double]) / 1000 # Zelle 8 $hex_volt = $hex_array[18]+$hex_array[19] $volt_8 = ([uint32]"0x$hex_volt" -as [double]) / 1000 # delta V berechnen $list_cell_volt = $volt_1, $volt_2, $volt_3, $volt_4, $volt_5, $volt_6, $volt_7, $volt_8 $result = $list_cell_volt | Measure-Object -Minimum -Maximum $cell_min = $result.Minimum $cell_max = $result.Maximum $cell_delta = $cell_max - $cell_min $cell_delta = [math]::round($cell_delta,3) * 1000 write-host "Paket 4: " $volt_1 $volt_2 $volt_3 $volt_4 $volt_5 $volt_6 $volt_7 $volt_8 "deltaV:" $cell_delta"mV" $port.Close()