Home » excel » vba – How do I read an input from a serial port (e.g. COM 3) into an Excel file, as hexadecimal?

vba – How do I read an input from a serial port (e.g. COM 3) into an Excel file, as hexadecimal?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am pushing a set of bytes from an external device to a COM port. The resultant input needs to be put in a cell in Excel, and converted to hex. I have tried various sets of tools, but none show me any results in Excel.

I have tried some VBA extensions, but they were all paid for, have tried some terminal tools as well. Current VBA tool code is shown below. I cannot get it to show anything in an excel cell either. Results just show in immediate logger.

Private Sub StrokeReader1_CommEvent(ByVal Evt As StrokeReaderLib.Event, ByVal data As Variant)
  Select Case Evt
    Case EVT_DISCONNECT
        Debug.Print "Disconnected"

    Case EVT_CONNECT
        Debug.Print "Connected"

    Case EVT_DATA
        buf = (StrokeReader1.Read(Text))  'Use BINARY to receive a byte array
        Debug.Print buf
  End Select
End Sub

'Use this to connect and set the port properties from the code
Sub connect()
  StrokeReader1.Port = 3
  StrokeReader1.BaudRate = 19200
  StrokeReader1.PARITY = NOPARITY
  StrokeReader1.STOPBITS = ONESTOPBIT
  StrokeReader1.DsrFlow = False
  StrokeReader1.CtsFlow = False
  StrokeReader1.DTR = False
  StrokeReader1.RTS = False
  StrokeReader1.Connected = True
  If StrokeReader1.Error Then
    Debug.Print StrokeReader1.ErrorDescription
  End If
End Sub

'Use this to send data to the remote device
Sub send()
  StrokeReader1.send "ABCD"  'A text string

  Dim x(3) As Byte  'A byte array
  x(1) = 1
  x(2) = 2
  x(3) = 3
  StrokeReader1.send x
End Sub

Expected results: AA 00 00 22 00 03 00 00 03 2B 01 E1 35

Actual result: ª " Ö $$

How to&Answers:
Case EVT_DATA
    buf = (StrokeReader1.Read(Text))  'Use BINARY to receive a byte array
    Debug.Print buf

You’re getting a byte array, which as far as VBA is concerned, is indistinguishable from a string – that’s the only reason Debug.Print buf isn’t throwing a type mismatch error – because any other array simply has no way to represent itself as a string, so you can’t debug-print an array.

What you need is to iterate the bytes in the array, use the Hex function to get the hexadecimal representation of each byte, and concatenate them into a legible string.

Something like this:

Private Function ToHexString(ByRef buffer As Variant) As String
'note: buffer array is wrapped in a Variant, could be passed ByVal

    'first allocate/size a dynamic array to hold our results;
    'by sizing from LBound(buffer) to UBound(buffer), we get
    'an array that's sized exactly the same as the buffer array.
    ReDim bytes(LBound(buffer) To UBound(buffer))

    'now iterate all items in the array
    Dim i As Long
    For i = LBound(buffer) To UBound(buffer)
        'store the hex representation of the byte at index i into our hex-bytes array
        bytes(i) = Hex(buffer(i))
    Next

    'return the joined hex-bytes array, using a space to separate the individual hex-bytes
    ToHexString = Strings.Join(bytes, " ")

End Function

Now you can do Debug.Print ToHexString(buf) and that should yield the expected output.

If you want that output to a cell, you need to get a Range object from a specific Worksheet. For example if you want to write to cell A1 of whatever the active sheet is:

ActiveSheet.Range("A1").Value = ToHexString(buf)

Or

ActiveSheet.Cells(1, 1).Value = ToHexString(buf)