none
Sending/reading hex data to a serial port in Powershell

    Question

  • I have a device which responds to serial port requests. Using portmon I can see a request formatted as a six byte hex string.

    e.g. x'02031005E6 the last byte being a crc which I can work out.

    I don't seem to be able to write this data to a comm port as if I write

    [Byte[]] $request = 0x02,0x03,0x10,0x05,0xE6

    $port.Write($request)

    I see

    0.00189600 powershell_ise IRP_MJ_WRITE LSerial0 SUCCESS Length 12: 32 20 33 20 31 36 20 35 20 32 33 30  

    where I was expecting Length 6: 02 03 10 05 E6

    even if I write

    [Byte] $request = 0x02

    $port.Write($request)

    I get

    0.00188317 powershell_ise IRP_MJ_WRITE LSerial0 SUCCESS Length 1: 32  

    I have a nasty feeling I am misunderstanding how Powershell handles hex strings/arrays and will assume the sucking of air through clenched teeth on the reply.

    Friday, January 17, 2014 6:57 PM

Answers

  • Your handling of Bytes, hexadecimal literals and arrays is fine. What type of object is the $port variable referring to?

    Edit:  If it's a [System.IO.Ports.SerialPort] object, then the problem is that you're calling SerialPort.Write() with a single argument.  The only overload of that method which takes a single argument accepts a String parameter, and PowerShell is automatically turning your bytes / arrays into a string representation (which is then being converted back into a byte stream.)

    To use SerialPort.Write with a Byte[] argument, you need to pass in 3 arguments:  The array, the offset, and the byte count.  For example:

    [Byte[]] $request = 0x02,0x03,0x10,0x05,0xE6
    
    $port.Write($request, 0, $request.Count)


    See http://msdn.microsoft.com/en-us/library/System.IO.Ports.SerialPort.Write(v=vs.110).aspx for more info.
    • Edited by David Wyatt Friday, January 17, 2014 7:04 PM
    • Marked as answer by SteveAshton Friday, January 17, 2014 10:50 PM
    Friday, January 17, 2014 6:59 PM

All replies

  • Your handling of Bytes, hexadecimal literals and arrays is fine. What type of object is the $port variable referring to?

    Edit:  If it's a [System.IO.Ports.SerialPort] object, then the problem is that you're calling SerialPort.Write() with a single argument.  The only overload of that method which takes a single argument accepts a String parameter, and PowerShell is automatically turning your bytes / arrays into a string representation (which is then being converted back into a byte stream.)

    To use SerialPort.Write with a Byte[] argument, you need to pass in 3 arguments:  The array, the offset, and the byte count.  For example:

    [Byte[]] $request = 0x02,0x03,0x10,0x05,0xE6
    
    $port.Write($request, 0, $request.Count)


    See http://msdn.microsoft.com/en-us/library/System.IO.Ports.SerialPort.Write(v=vs.110).aspx for more info.
    • Edited by David Wyatt Friday, January 17, 2014 7:04 PM
    • Marked as answer by SteveAshton Friday, January 17, 2014 10:50 PM
    Friday, January 17, 2014 6:59 PM
  • Thank you for your help - worked like a dream
    Friday, January 17, 2014 10:52 PM