如何将串行端口(如COM 3)的输入读入Excel文件(十六进制)?

如何将串行端口(如COM 3)的输入读入Excel文件(十六进制)?,excel,vba,hex,Excel,Vba,Hex,我正在将一组字节从外部设备推送到COM端口。结果输入需要放在Excel的单元格中,并转换为十六进制。我尝试过各种工具,但都没有在Excel中显示任何结果 我试过一些VBA扩展,但都是付费的,也试过一些终端工具。当前VBA工具代码如下所示。我也无法让它在excel单元格中显示任何内容。结果只显示在即时记录器中 Private Sub StrokeReader1_CommEvent(ByVal Evt As StrokeReaderLib.Event, ByVal data As Variant)

我正在将一组字节从外部设备推送到COM端口。结果输入需要放在Excel的单元格中,并转换为十六进制。我尝试过各种工具,但都没有在Excel中显示任何结果

我试过一些VBA扩展,但都是付费的,也试过一些终端工具。当前VBA工具代码如下所示。我也无法让它在excel单元格中显示任何内容。结果只显示在即时记录器中

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
预期结果:
AA 00 00 22 00 03 00 00 03 2B 01 E1 35

实际结果:
ª“Ö$$

您将得到一个字节数组,就VBA而言,它与字符串无法区分-这是
Debug.Print buf
没有抛出类型不匹配错误的唯一原因-因为任何其他数组都无法将自身表示为字符串,所以您无法调试打印数组

您需要迭代数组中的字节,使用
Hex
函数获取每个字节的十六进制表示形式,并将它们连接成一个清晰的字符串

大概是这样的:

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
现在您可以执行
Debug.Print to hexstring(buf)
,这将产生预期的输出

如果要将该输出写入单元格,则需要从特定的
工作表
中获取一个
范围
对象。例如,如果要写入活动工作表的单元格A1:

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


您正在调试打印字节数组。-基本上VBA正在对编码进行假设,并使用它运行,并提供您所需的:数组中字节的字符串表示形式。感谢@Mathieu分享您的知识。我正在尝试,并将报告。我现在在这一行遇到类型不匹配错误:ReDim bytes(LBound(buffer)到UBound(buffer))您需要删除
(StrokeReader1.Read(Text))
周围的括号,这是另一种类型不匹配(假设
buf
是公布的字节数组)。@Adam这意味着
buffer
不是数组。
?tohextstring(数组(128126124255,72143,65))
outputs
807e7cf488f41
正如预期的那样-如果实际声明了
buf
,这将非常有帮助…是的,你是对的。谢谢。我在你发布的时候就计算出了这个位。所以这很有效:
1buf=StrokeReader1.Read(BINARY)'使用BINARY来接收字节数组
谢谢Mathieu,你真了不起!
ActiveSheet.Range("A1").Value = ToHexString(buf)
ActiveSheet.Cells(1, 1).Value = ToHexString(buf)