PDA

View Full Version : MSComm RTU serial communication



Casimir
01-18-2015, 09:20 PM
I have started a project in which i have to write a VBA script to read out a flow computer. This will be done by a RS-323 RTU protocol. All the internet help I could find for the protocol was for modems. I understand, at least i think, all the commands. When I send out a request using MSComm1.output the transmission buffer stays empty.



Private Sub CommandButton1_Click()

' Buffer to hold error and output
Dim data As String
Dim error1 As Integer
Dim error2 As Integer
' Use COM5.
MSComm1.CommPort = 5
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read entire buffer when Input
' is used.
MSComm1.InputLen = 0
' Open the port.
MSComm1.PortOpen = True
' Send the attention command to the module.
'data = Chr$(&H6C) + Chr$(&HA2) + Chr$(&H1) + Chr$(&H0) + Chr$(&H86) + Chr$(&H0) + Chr$(&H3) + Chr$(&H1) '4321
'data = Chr$(&H1) + Chr$(&H3) + Chr$(&H0) + Chr$(&H86) + Chr$(&H0) + Chr$(&H1) + Chr$(&HA2) + Chr$(&H6C) '1234
data = Chr(&H6C) + Chr(&HA2) + Chr(&H1) + Chr(&H0) + Chr(&H86) + Chr(&H0) + Chr(&H3) + Chr(&H1) '4321 -$
'data = Chr(&H1) + Chr(&H3) + Chr(&H0) + Chr(&H86) + Chr(&H0) + Chr(&H1) + Chr(&HA2) + Chr(&H6C) '1234 -$

MSComm1.Output = data & Chr(13)
'foutmelding
error1 = MSComm1.CommEvent
'read output buffer
Range("B2") = MSComm1.OutBufferCount
'pauze the script
Application.Wait (Now + TimeValue("00:00:05"))
'read the input
'Range("B1") = MSComm1.Input
'foutmelding
error2 = MSComm1.CommEvent
'read input buffer
Range("B3") = MSComm1.InBufferCount
' Close the serial port.
MSComm1.PortOpen = False
'Display errors
Range("B4") = error1
Range("B5") = error2
End Sub

Private Sub CommandButton2_Click()
Range("B1", "B5") = ""
'empty buffers
MSComm1.OutBufferCount = 0
End Sub

Private Sub MSComm1_OnComm()
Dim InputData As String

Select Case MSComm1.CommEvent

' Errors
Case comEventBreak ' A Break was received.
MsgBox "Break received"
Case comEventFrame ' Framing Error
MsgBox "Framing error"
Case comEventOverrun ' Data Lost.
MsgBox "Overrun error"
Case comEventRxOver ' Receive buffer overflow.
MsgBox "Receive Buffer overflow"
Case comEventRxParity ' Parity Error.
MsgBox "Parity Error"
Case comEventTxFull ' Transmit buffer full.
MsgBox "Transmit Buffer full"
Case comEventDCB ' Unexpected error retrieving DCB
MsgBox "DCB error"

' Events
Case comEvCD ' Change in the CD line.
MsgBox "Carrier Detect changed state"

Case comEvDSR ' Change in the DSR line.
MsgBox "Data Set Ready (DSR) changed state"
Case comEvRing ' Change in the Ring Indicator.
MsgBox "Ring Indicator (RI) changed state"
Case comEvReceive ' Received RThreshold # of characters
' MsgBox Str$(MSComm1.RThreshold) & " Characters received"
' While (MSComm1.InBufferCount > 0)
' txtReceive = txtReceive & MSComm1.Input
' only keep the last 5,000 characters -- throw away any characters older than that
' txtReceive = Right$(txtReceive, 5000)
' Wend

InputData = InputData + MSComm1.Input
Case comEvCTS ' Change in the CTS line.
MsgBox "Clear To Send (CTS) changed state"
TextBox1.Text = InputData
InputData = ""

Case comEvSend ' There are SThreshold number of characters in the transmit buffer
MsgBox Str$(MSComm1.SThreshold) & " Characters remaining in transmit buffer"
Case comEvEOF ' An EOF charater was found in the input stream
MsgBox "EOF received"
End Select
End Sub


I do not know in what order the characters need to be send so I tried all sorts of combinations without success. The adress is (hexadecimal):
[slaveID] [Funciton] [Adress] [registers] [CRC]
[01] [03] [00 86] [00 01] [A2 6C]
I haven't even tried to calculate the CRC myself because I could send a signal.
Can anyone give me some tips for my code?
Thank you

SamT
01-19-2015, 07:32 AM
httCasimir. Please read A message to forum cross posters (http://www.excelguru.ca/node/7).

Crossposted:
ps://social.msdn.microsoft.com/Forums/en-US/3f2e1f28-ae66-4136-a17a-8e8cdd5d9495/mscomm-rtu-not-a-modem?forum=vbgeneral

Assuming you meant the RS-232 protocol, try these two sources

Introduction : http://www.rtaautomation.com/technologies/modbus-rtu/

Specifications : http://www.modbus.org/docs/Modbus_over_serial_line_V1.pdf

It looks like your VBA skills are up to the task, I would suggest that yhou need an RS-232 forum at this point.

I googled and found this RS-232 Forum: http://www.lammertbies.nl/forum/viewforum.php?f=2

Casimir
01-19-2015, 08:59 PM
Thank you very much for your reply