excel - 如何将串行端口(例如 COM 3)的输入以十六进制形式读取到 Excel 文件中?

标签 excel vba hex

我正在将一组字节从外部设备推送到 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

实际结果:ª "Ö $$

最佳答案

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

您将获得一个字节数组,就 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 ToHexString(buf),这应该会产生预期的输出。

如果您希望将输出输出到单元格,则需要从特定的 Worksheet 获取 Range 对象。例如,如果您想写入事件工作表的单元格 A1:

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

或者

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

关于excel - 如何将串行端口(例如 COM 3)的输入以十六进制形式读取到 Excel 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56893519/

相关文章:

python - Python 中的异或错误 (2.7)

excel - 如何找到每天的最小值/最大值?

javascript - 如何从vba中查找和调​​用javascript方法

excel - 使用cmd运行vbs脚本

vba - 宏在打开时不自动运行

javascript - Javascript 中是否有像 VB 中那样的 With 语句

java - 将十进制/十六进制写入文件,而不是字符串

binary - 十六进制中的 n 位数字可以表示多少个值?

c# - 从 .Net 中的 Excel 导入时的科学记数法

c# - 以编程方式更改 Excel 中的宏文本