excel VBA串口通信

wgmfuz8q  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(233)

我的任务是打开、写入、阅读响应,然后关闭通信端口。所以为了

  • 打开Com端口1波特率9600 8、N、1或8无和1
  • 将字符串写入仪器“#AddressReading”+返回字符
  • 等待并逐渐阅读回应
  • 关闭com端口,几分钟后再使用它

我用的是32位的win操作系统,我试过几种不同的方法-
https://gist.github.com/heiswayi/f47dfd8dc38955322bef
这涉及到集成win32.dll,我无法弄清楚如何在VBA Excel中调用win32
What is the best way to access a serial port from VBA?
这只是导致excel崩溃,每次我试图修改和运行代码...不确定再次这是否与睡眠功能有关或没有。
我发现的另一件事是一堆SDK的成本方式出我的预算库VBA串行通信。

mspsb9vt

mspsb9vt1#

类似于Read from Serial port to Excel,我修改了它来回答这个问题。

Private Sub CommandButton1_Click()
Open "COM1:9600,N,8,1,X" For Binary Access Read Write As #1   'Opens Com Port with Baud rate and bit settings
 Cmnd = "#AddressReading" + Chr(13)    'Message assembled to be sent to device on serial port
 Put #1, , Cmnd                'Sends assembled message
  answer = ""                  'clear response string
  char = Input(1, #1)          'get first character
  While (char <> Chr(13))      'loop until [CR]
    If (char > Chr(31)) Then
      answer = answer + char   'add, if printable char
    Else
      ' Do what ever you like
    End If
    char = Input(1, #1)        'get the next character
  Wend
  Close #1
 Range("C2").Value = answer    'places response in desired cell
End Sub

相关问题