1

I'm trying to communicate an Excel Sheet with a device that communcates using a Serial port. I have mode some research and found a lot of paid software that does this for you. However, I really want to make it by myself and do it directly from Excel using VBA. The problem is, I'm not good enough at VBA and finding tutorials or example has been difficult. Maybe my question is off the topic but I really need help. I tried this Macro code but Excel crashes.

Sub Read_Serial()
  Open "COM3" For Binary Access Read Write As #1
  '--------------------------------------------------------
  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

Thanks for reading and sorry if I made a mistake typing my question, english is not my native language.

  • http://www.mrexcel.com/forum/excel-questions/488335-receive-data-com-port.html – J Reid Oct 20 '16 at 14:02
  • @JReid Is that code running in a loop? It says is running but won't stop unless I force the application to close. – Alejandro Harris Bonet Oct 20 '16 at 14:15
  • 1
    See [What is the best way to access a serial port from VBA?](http://stackoverflow.com/questions/569698/what-is-the-best-way-to-access-a-serial-port-from-vba) – Comintern Oct 20 '16 at 14:28
  • Hi @Comintern, I've been working with that code a couple of weeks ago. I finally got information from the device. I was having issues with the code but I made some changes and made it better. Thanks a lot. – Alejandro Harris Bonet Oct 20 '16 at 16:59

0 Answers0