cvdubbs
cvdubbs

Reputation: 86

VBA Serial Port Communication

I'm tasked with opening, writing, reading the response and then closing the com port. So in order

I'm using a 32-bit win OS and I've tried a couple of different methods-

https://gist.github.com/heiswayi/f47dfd8dc38955322bef

which involves integrating the win32.dll and I wasn't able to figure out how to call win32 in VBA excel

What is the best way to access a serial port from VBA?

this just caused excel to crash every time I tried to modify and run the code...not sure again if this is related to the sleep function or not.

The other thing I found was a bunch of SDK's the cost way out of my budget libraries for VBA serial communication.

Upvotes: 2

Views: 19671

Answers (2)

Richard Slatter
Richard Slatter

Reputation: 9

A query. In the above solution the string “COM1:9600,N,8,1,X” opens a serial line with XON/XOFF flow control.

What is the replacement for X that gives No flow control?

Many thanks

Richard

Upvotes: -1

cvdubbs
cvdubbs

Reputation: 86

Similar to Read from Serial port to Excel I modified it to answer this.

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

Upvotes: 4

Related Questions