How can I write the value 255 to the serial port in Powershell ?
$port= new-Object System.IO.Ports.SerialPort COM6,4800,None,8,one
$port.open()
$port.Write([char]255)
$port.Close()
The output of the previous script is 63 (viewed with a serial port monitor).
However $port.Write([char]127) gives 127 as result. If the value is higher than 127 the output is always 63.
Thanks in advance for your help !
Despite your attempt to use [char], your argument is treated as [string], because PowerShell chooses the following overload of the Write method, given that you're only passing a single argument:
void Write(string text)
The documentation for this particular overload states (emphasis added):
By default, SerialPort uses ASCIIEncoding to encode the characters. ASCIIEncoding encodes all characters greater than 127 as (char)63 or '?'. To support additional characters in that range, set Encoding to UTF8Encoding, UTF32Encoding, or UnicodeEncoding.
To send byte values, you must use the following overload:
void Write(byte[] buffer, int offset, int count)
That requires you to:
use cast [byte[]] to cast your byte value(s)
and specify values for offset - the starting byte position as well as count, the number of bytes to copy from the starting byte position.
In your case:
$port.Write([byte[]] (255), 0, 1)
Note: The (...) around 255 isn't required for a single value, but would be necessary to specify multiple, ,-separated values.
Note:
If you want to send entire strings, and those strings include characters outside the ASCII range, you'll need to set the port's character encoding first, as shown in this answer, which also shows an alternative solution based on obtaining a byte-array representation of a string based on the desired encoding (which then allows you to use the same method overload as above).
Try something like this:
$port1 = new-Object System.IO.Ports.SerialPort COM1,4800,None,8,one
$port1.Open()
$data = [System.Text.Encoding]::UTF32.GetBytes([char]255)
$port1.Write( $data )
$port1.ReadExisting()
$port1
$port1.Close()
Should work.
Related
I'm trying to replicate the functionality of the following Python snippit in PowerShell:
allowed_mac_separators = [':', '-', '.']
for sep in allowed_mac_separators:
if sep in mac_address:
test = codecs.decode(mac_address.replace(sep, ''), 'hex')
b64_mac_address = codecs.encode(test, 'base64')
address = codecs.decode(b64_mac_address, 'utf-8').rstrip()
It takes a MAC address, removes the separators, converts it to hex, and then base64. (I did not write the Python function and have no control over it or how it works.)
For example, the MAC address AA:BB:CC:DD:E2:00 would be converted to AABBCCDDE200, then to b'\xaa\xbb\xcc\xdd\xe2\x00', and finally as output b'qrvM3eIA'. I tried doing something like:
$bytes = 'AABBCCDDE200' | Format-Hex
[System.BitConverter]::ToString($bytes);
but that produces MethodException: Cannot find an overload for "ToString" and the argument count: "1". and I'm not really sure what it's looking for. All the examples I've found utilizing that call only have one argument. This works:
[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('AABBCCDDE200'))
but obviously doesn't convert it to hex first and thus yields the incorrect result. Any help is appreciated.
# Remove everything except word characters from the string.
# In effect, this removes any punctuation ('-', ':', '.')
$sanitizedHexStr = 'AA:BB:CC:DD:E2:00' -replace '\W'
# Convert all hex-digit pairs in the string to an array of bytes.
$bytes = [byte[]] -split ($sanitizedHexStr -replace '..', '0x$& ')
# Get the Base64 encoding of the byte array.
[System.Convert]::ToBase64String($bytes)
For an explanation of the technique used to create the $bytes array, as well as a simpler PowerShell (Core) 7.1+ / .NET 5+ alternative (in short: [System.Convert]::FromHexString('AABBCCDDE200')), see this answer.
As for what you tried:
Format-Hex does not return an array of bytes (directly), its primary purpose is to visualize the input data in hex format for the human observer.
In general, Format-* cmdlets output objects whose sole purpose is to provide formatting instructions to PowerShell's output-formatting system - see this answer. In short: only ever use Format-* cmdlets to format data for display, never for subsequent programmatic processing.
That said, in the particular case of Format-Hex the output objects, which are of type [Microsoft.PowerShell.Commands.ByteCollection], do contain useful data, and do contain the bytes of the transcoded characters of input strings .Bytes property, as Cpt.Whale points out.
However, $bytes = ($sanitizedHexStr | Format-Hex).Bytes would not work in your case, because you'd effectively get byte values reflecting the ASCII code points of characters such as A (see below) - whereas what you need is the interpretation of these characters as hex digits.
But even in general I suggest not relying on Format-Hex for to-byte-array conversions:
On a philosophical note, as stated, the purpose of Format-* cmdlets is to produce for-display output, not data, and it's worth observing this distinction, this exception notwithstanding - the type of the output object could be considered an implementation detail.
Format-Hex converts strings to bytes based on first applying a fixed character transcoding (e.g., you couldn't get the byte representation of a .NET string as-is, based on UTF-16 code units), and that fixed transcoding differs between Windows PowerShell and PowerShell (Core):
In Windows PowerShell, the .NET string is transcoded to ASCII(!), resulting in the loss of non-ASCII-range characters - they are transcoded to literal ?
In PowerShell (Core), that problem is avoided by transcoding to UTF-8.
The System.BitConverter.ToString failed, because $bytes in your code wasn't itself a byte array ([byte[]]), only its .Bytes property value was (but didn't contain the values of interest).
That said, you're not looking to reconvert bytes to a string, you're looking to convert the bytes directly to Base64-encoding, as shown above.
Trying to export current temperature from the XML to a text file. The results are are "xx.y" but I only need xx exported to the text file. I've tried several commands but keep striking out. Any ideas?
([xml](Invoke-WebRequest -URI http://w1.weather.gov/xml/current_obs/KSJC.xml).Content).current_observation.temp_f | Out-File c:\temperature.txt
i suspect i am misunderstanding something [this seems alarmingly simple], but here are a few ways to do what it seems you want ... [grin]
$Temperature = '12.3'
$Temperature.Split('.')[0]
[int]$Temperature
[math]::Round([decimal]$Temperature, 0)
the output if each is 12.
the 1st uses the string .Split() method and takes the 1st item in the resulting array
the 2nd uses the [int] type accelerator to coerce the string into an int & rounds it in the process
the 3rd uses the [decimal] type accelerator to coerce the string to a decimal number and the [math]::Round() static method to round the number to 0 decimal places
i am facing issue while converting unicode data into national characters.
When i convert the Unicode data into national using national-of function, some junk character like # is appended after the string.
E.g
Ws-unicode pic X(200)
Ws-national pic N(600)
--let the value in Ws-Unicode is これらの変更は. getting from java end.
move function national-of ( Ws-unicode ,1208 ) to Ws-national.
--after converting value is like これらの変更は #.
i do not want the extra # character added after conversion.
please help me to find out the possible solution, i have tried to replace N'#' with space using inspect clause.
it worked well but failed in some specific scenario like if we have # in input from user end. in that case genuine # also converted to space.
Below is a snippet of code I used to convert EBCDIC to UTF. Before I was capturing string lengths, I was also getting # symbols:
STRING
FUNCTION DISPLAY-OF (
FUNCTION NATIONAL-OF (
WS-EBCDIC-STRING(1:WS-XML-EBCDIC-LENGTH)
WS-EBCDIC-CCSID
)
WS-UTF8-CCSID
)
DELIMITED BY SIZE
INTO WS-UTF8-STRING
WITH POINTER WS-XML-UTF8-LENGTH
END-STRING
SUBTRACT 1 FROM WS-XML-UTF8-LENGTH
What this code does is string the UTF8 representation of the EBCIDIC string into another variable. The WITH POINTER clause will capture the new length of the string + 1 (+ 1 because the pointer is positioned to the next position after the string ended).
Using this method, you should be able to know exactly how long second string is and use that string with the exact length.
That should remove the unwanted #s.
EDIT:
One thing I forgot to mention, in my case, the # signs were actually EBCDIC low values when viewing the actual hex on the mainframe
Use inspect with reverse and stop after first occurence of #
I'm very new to powershell and am running into walls trying to convert a string to a integer.
If I run the following command: Get-DefaultAudioDeviceVolume it often returns a number that looks something like: 50.05816%, which I have confirmed to be a string. I need to convert this to a whole number integer (50). Obviously I could hard code the integer in my script, but for the purpose of the script I need it to be flexible in it's conversion. The result of the previous test changes and I want to pass along the whole integer further down the line.
Thanks in advance!
If the string contains the % symbol you would need to remove this, then you can use the -as operator to convert to [int]
[string]$vol = "50.05816%"
$vol_int = $vol.Replace('%','') -as [int]
The -as operator is very useful and has many other uses, this article goes through a number of them: https://mcpmag.com/articles/2013/08/13/utilizing-the-as-operator.aspx
Just cast it to integer and replace the "%" with nothing:
[int]$var = (Get-DefaultAudioDeviceVolume).Replace("%","")
Powershell does automatic type casting and starts from the left. So when $var is defined as an integer, it will try to convert the right side to the same type.
I want to send chars to a serial port using powershell:
PS > [Char[]] $request = 'C','A'
PS > $port.Write($request)
But this creates extra whitespace between the characters. Any ideas why?
43 20 41
20 is the extra byte.
It appears that powershell is choosing the overload of Write which takes a single string parameter. In this case it is converting the array to a string before passing it to Write. This conversion inserts the extra space:
[string]$request //C A
specify the other two parameters to use the right overload:
$port.Write($request, 0, $request.length)