从串口读取到 Excel [英] Read from Serial port to Excel

查看:48
本文介绍了从串口读取到 Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Excel 中创建一个按钮来从串口读取数据.我不能在 Excel 表上附加任何额外的文件.我需要将此excel文件传输到另一台计算机以读取此数据.以下是该文件的工作原理: 按 按钮选择串行端口.然后,按另一个按钮从串口读取数据到excel单元格中.有人可以告诉我怎么做吗?使用 VB 宏还是 ActiveX 宏?抱歉,这是我第一次为此使用 excel.请帮忙.同样,我不能将另一个文件附加到 Excel 工作表.谢谢!

I need to create a button in Excel to read data in from serial port. I can't have any extra files attached to the excel sheet. I need to transfer this excel file to another computer to read this data. Here is how the file is suppose to function: Press button to select the serial port. Then, press another button to read data from serial port into the excel cell. Could someone please tell me how to do this? Use VB macro or ActiveX macro? Sorry, this is the first time i'm using excel for this. Help please. Again, I can't have another file attached to the excel sheet. Thank you!

推荐答案

我在德国 microcontroler.net 论坛上找到了关于这个主题的讨论:

I found a discussion on exactly this topic in the german microcontroler.net forum here:

http://www.mikrocontroller.net/topic/64788

由于我在 Linux 上运行,我无法验证代码是否正确.无论如何,这是它的副本:

Since I am running on Linux I can not verify if the code is correct. Anyway, here is a copy of it:

Sub Send_and_Read()
  '--------------------------------------------------------
  cmnd$ = "Hello World"        'A string to send
  '--------------------------------------------------------
  Open "COM1" For Binary Access Read Write As #1
  cmnd$ = cmnd$ + Chr(13)      'add [CR] to command string
  Put #1, , cmnd$              'write string to interface
  '--------------------------------------------------------
  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
  '--------------------------------------------------------
  Cells(1, 1) = answer         'put response in cell("A1")
End Sub

这篇关于从串口读取到 Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆