MyTetra Share
Делитесь знаниями!
Modbus Poll user manual
Время создания: 16.01.2020 15:17
Текстовые метки: modbus
Раздел: !Закладки - MSO - Modbus

12. OLE/Automation

Automation (formerly known as OLE Automation) makes it possible for one application to manipulate objects implemented in another application.

An Automation client is an application that can manipulate exposed objects belonging to another application. This is also called an Automation controller.

An Automation server is an application that exposes programmable objects to other applications. Modbus Poll is an automation server.

That means you can use any program that supports VBA (Visual Basic for Applications) such as Visual Basic, Excel etc. to interpret and show the modbus data according to your specific requirements.

12.1. Excel example

You should display the Developer tab or run in developer mode when you want to write macros.

12.1.1. Excel 2007

 

  1. Click the Microsoft office button and then click Excel options.
  2. Click popular and then select the show Developers tab in the ribbon check box.
    Note the ribbon is part of the Microsoft fluent user interface.

12.1.2. Excel 2010

  1. Click on the file tab.
  2. Click options. Excel Options window will open.
  3. On the left pane click Customize Ribbon.
  4. On the right pane, under Main Tabs, check the Developer check box.
  5. Click OK. The Developer tab should now show in the ribbon (right most tab).

12.1.3. Excel sample code

This example opens two windows. One reading registers and another reading Coils.
Modbus Poll is hidden but you can show it by uncomment the "ShowWindow" line. This will show one of the windows.

An example is also included with the Modbus Poll installation.
Start → All Programs → Modbus Poll → Excel Example

Excel VBA Example

Public doc1 As Object

Public doc2 As Object

Public app As Object

Dim res As Integer

Dim n As Integer

 

Private Sub StartModbusPoll_Click()

   Set app = CreateObject("Mbpoll.Application")

   Set doc1 = CreateObject("Mbpoll.Document")

   Set doc2 = CreateObject("Mbpoll.Document")

' Read 10 Holding Registers every 1000ms

   res = doc1.ReadHoldingRegisters(1, 0, 10, 1000)

' Read 10 Coil Status every 1000ms

   res = doc2.ReadCoils(1, 0, 10, 1000)

' doc1.ShowWindow()

   app.Connection = 1 ' Modbus TCP/IP

   app.IPAddress = "127.0.0.1" ' local host

   app.ServerPort = 502

   app.ConnectTimeout = 1000

   res = app.OpenConnection()

End Sub

 

Private Sub Read_Click()

   Cells(5, 7) = doc1.ReadResult() 'Show results for the requests

   Cells(6, 7) = doc2.ReadResult()

 

   For n = 0 To 9

      Cells(5 + n, 2) = doc1.SRegisters(n)

   Next n

 

   For n = 0 To 9

      Cells(18 + n, 2) = doc2.Coils(n)

   Next n

End Sub

12.2. Connection Functions/Properties

The following properties and functions do the same as you setup in the connection dialog (F3).

12.2.1. Connection

Connection selects the desired connection. A serial port or one of the Ethernet connections can be selected.

Property Connection as Integer

Valid values

0 = Serial port
1 = Modbus TCP/IP
2 = Modbus UDP/IP
3 = Modbus ASCII/RTU over TCP/IP
4 = Modbus ASCII/RTU over UDP/IP

12.2.2. BaudRate

Applicable only for Connection = 0

Property BaudRate as Long

Valid values

300
600
1200
2400
4800
9600 (Default)
14400
19200
38400
56000
57600
115200
128000
256000
460800
921600

Example

BaudRate = 9600

12.2.3. DataBits

Applicable only for Connection = 0

Property DataBits as Integer

Valid values

7
8 (Default)

Example

DataBits = 8

12.2.4. Parity

Applicable only for Connection = 0

Property Parity as Integer

Valid values

0 = None
1 = Odd
2 = Even (Default)+

Example

Parity = 2

12.2.5. StopBits

Applicable only for Connection = 0

Property StopBits as Integer

Valid values

1 (Default)+ 2

Example

StopBits = 1

12.2.6. SerialPort

Applicable only for Connection = 0

Property SerialPort as Integer

Valid values

1…255

Default value = 1

Example

SerialPort = 1

12.2.7. ResponseTimeout

The ResponseTimeout specifies the length of time in ms that Modbus Poll should wait for a response from a slave device before giving up.

Property ResponseTimeout as Integer

Valid values

50…100000

Default value = 1000

Example:

ResponseTimeout = 1000

12.2.8. ServerPort

Applicable only for Connection = 1…4

Property ServerPort as Long

Valid values

0…65535

Default value = 502

Example

ServerPort = 502

12.2.9. ConnectTimeout

The ConnectTimeout specifies the length of time that Modbus Poll should wait for a TCP/IP connection to succeed.

Applicable only for Connection = 1…4

Property ConnectTimeout as Integer

Valid values

100…30000ms

Default value = 1000ms

Example

ConnectTimeout = 1000

12.2.10. IPVersion

Applicable only for Connection = 1…4

Property IPVersion as Integer

Valid values

4 = IP Version 4 (Default)
6 = IP Version 6

Example

IPVersion = 4

12.2.11. OpenConnection

Opens the connection selected with the Connection property.

Function OpenConnection() As Integer

Return Value:

For error 3-5: Please check if you have the latest serial port driver.

0 = SUCCESS
1 = Serial Port not available
3 = Serial port. Not possible to get current settings from port driver.
4 = Serial port. Serial port driver did not accept port settings.
5 = Serial port. Serial port driver did not accept timeout settings.
12 = TCP/UDP Connection failed. WSA start up
13 = TCP/UDP Connection failed. Connect error
14 = TCP/UDP Connection failed. Timeout
15 = TCP/UDP Connection failed. IOCTL
17 = TCP/UDP Connection failed. Socket error
21 = TCP/UDP Connection failed. Address information

Example how to open a Modbus TCP/IP connection

Public app As Object

Dim res As Integer

' Create an object to Modbus Poll

Set app = CreateObject("Mbpoll.Application")

app.Connection = 1 ' Select Modbus TCP/IP

app.IPVersion = 4

app.IPAddress = "192.168.1.27"

app.ServerPort = 502

app.ConnectTimeout = 1000

app.ResponseTimeout = 1000

res = OpenConnection()

Example how to setup a serial connection

Public app As Object

Dim status As Integer

' Create an object to Modbus Poll

Set app = CreateObject("Mbpoll.Application")

 

app.Connection = 0         ' Mode is serial port

app.SerialPort = 1         ' Com port 1

app.BaudRate = 9600        ' 9600 baud

app.Parity = 0             ' None parity

app.Mode = 0               ' RTU mode

app.ResponseTimeout = 1000 ' Wait 1000ms until give up

status = app.OpenConnection()

12.2.12. CloseConnection

Function CloseConnection() As Integer

Return Value

Zero if success. Nonzero value if failed.

12.3. Read Functions

The following functions do the same as you setup in the read/write definition dialog (F8). Read functions are associated with a Modbus Poll document. (The window with data)
.Example

' First a Modbus Poll document is needed.

Public doc As Object

 

Set doc = CreateObject("Mbpoll.Document")

res = doc.ReadCoils(1, 0, 100, 1000) ' Read 100 coils every 1000ms

You must create a Read before you can use properties to get data.

12.3.1. ReadCoils

Function ReadCoils(SlaveID As Integer, Address As Long, Quantity As Integer, ScanRate As Long) As Integer

Return Value

True if success. False if not success

Parameters

SlaveID: The slave address 1 to 255
Address: The data address (Base 0)
Quantity: The number of data. 1 to 2000
ScanRate: 0 to 3600000ms

12.3.2. ReadDiscreteInputs

Function ReadDiscreteInputs(SlaveID As Integer, Address As Long, Quantity As Integer, ScanRate As Long) As Integer

Return Value

True if success. False if not success

Parameters

SlaveID: The slave address 1 to 255
Address: The data address (Base 0)
Quantity: The number of data. 1 to 2000
ScanRate: 0 to 3600000ms

12.3.3. ReadHoldingRegisters

Function ReadHoldingRegisters(SlaveID As Integer, Address As Long, Quantity As Integer, ScanRate As Long) As Integer

Return Value

True if success. False if not success

Parameters

SlaveID: The slave address 1 to 255
Address: The data address (Base 0)
Quantity: The number of data. 1 to 125
ScanRate: 0 to 3600000ms

12.3.4. ReadInputRegisters

Function ReadInputRegisters(SlaveID As Integer, Address As Long, Quantity As Integer, ScanRate As Long) As Integer

Return Value

True if success. False if not success

Parameters

SlaveID: The slave address 1 to 255
Address: The data address (Base 0)
Quantity: The number of data. 1 to 125
ScanRate: 0 to 3600000ms

12.3.5. ShowWindow

ShowWindow()
As default Modbus document windows are hidden. The ShowWindow function makes Modbus Poll visible and show the document with data content.

12.3.6. ByteOrder

Property ByteOrder As Integer

Description

Sets the byte order used by Longs, Floats and Doubles properties.
0 = Big-endian (Default)
1 = Little-endian
2 = Big-endian byte swap
3 = Little-endian byte swap

Example for Longs:
Byte Order: Big-endian
The decimal number 123456789 or in hexadecimal 07 5B CD 15
Order as they come over the wire in a Modbus message: 07 5B CD 15

Syntax

ByteOrder [=newvalue]

12.3.7. ReadResult

Use this property to check if communication established with Read is running successful.

Property ReadResult As Integer

Return Value:
0 = SUCCESS
1 = TIMEOUT ERROR
2 = CRC ERROR
3 = RESPONSE ERROR (The response was not the expected slave id, function or address)
4 = WRITE ERROR
5 = READ ERROR
6 = PORTNOTOPEN ERROR
10 = DATA UNINITIALIZED
11 = INSUFFICIENT BYTES RECEIVED
16 = BYTE COUNT ERROR
19 = TRANSACTION ID ERROR
81h = ILLEGAL FUNCTION
82h = ILLEGAL DATA ADDRESS
83h = ILLEGAL DATA VALUE
84h = FAILURE IN ASSOCIATED DEVICE
85h = ACKNOWLEDGE
86h = BUSY, REJECTED MESSAGE
87h = NAK-NEGATIVE ACKNOWLEDGMENT

12.3.8. WriteResult

Return a write result as an integer.
Use this function to check if a write was successful. The value is DATA_UNINITIALIZED until the result from the slave is available. See
ReadResult for a list of possible values.

Property WriteResult As Integer

12.4. Automation data properties

The below properties are used to set or get values in the internal write/read arrays in Modbus Poll. The Index used is not a Modbus Address. The Index always counts from 0 no matter of the address used. The data properties are associated with a Modbus Poll document. (The window with data)

Example 1:

' doc is assumed created first. See Excel example.

doc.SRegisters(0) = 1

doc.SRegisters(1) = 10

doc.SRegisters(2) = 1234

' Write 3 registers stored in Modbus Poll internal array

' to Modbus address 100 (40101)

res = doc.WriteMultipleRegisters(1, 100, 3)

Example 2 with floating point values:

doc.Floats(0) = 1.3

doc.Floats(2) = 10.5

doc.Floats(4) = 1234.12

res = doc. WriteMultipleRegisters(1, 0, 6) "Write the 6 register stored in Modbus Poll

' 6 Registers are written as float is 32 bit wide.

12.4.1. Coils

Property Coils(Index As Integer) As Integer

Description

Sets a coil in the write array structure or return a coil from the read array.

Syntax

Coils(Index) [=newvalue]

12.4.2. SRegisters

Property SRegisters(Index As Integer) As Integer

Description

Sets a register in the write array structure or return a register from the read array.

Syntax

SRegisters (Index) [=newvalue]

12.4.3. URegisters

Property URegisters(Index As Integer) As Long

Description

Sets a register in the write array structure or return a register from the read array.

Syntax

Object. URegisters (Index) [=newvalue]

12.4.4. Ints_32

Property Ints_32(Index As Integer) As Double

Description

Sets a 32 Bit integer in the write array structure or return an integer from the read array.

Syntax

Ints_32 (Index) [=newvalue]

12.4.5. UInts_32

Property UInts_32(Index As Integer) As Double

Description

Sets a 32 Bit unsigned integer in the write array structure or return an unsigned integer from the read array.

Syntax

UInts_32 (Index) [=newvalue]

12.4.6. Ints_64

Property Ints_64(Index As Integer) As Double

Description

Sets a 64 Bit integer in the write array structure or return an integer from the read array.

Syntax

Ints_64 (Index) [=newvalue]

12.4.7. UInts_64

Property UInts_64(Index As Integer) As Double

Description

Sets a 64 Bit unsigned integer in the write array structure or return an unsigned integer from the read array.

Syntax

UInts_64 (Index) [=newvalue]

12.4.8. Floats

Property Floats(Index As Integer) As Single

Description

Sets a float in the write array structure or return a float from the read array.

Syntax

Floats* (Index) [=newvalue]

12.4.9. Doubles

Property Doubles(Index As Integer) As Double

Description

Sets a double in the write array structure or return a double from the read array.

Syntax

Doubles* (Index) [=newvalue]

12.5. Automation Write Functions

The write functions write the values stored in the array filled by the properties. The functions are associated with a Modbus Poll document. (The window with data)

12.5.1. WriteSingleCoil

Function WriteSingleCoil(SlaveID As Integer, Address As Long) As Integer

Return Value

True if write array is ready and the data are sent. False if array empty or error in parameters.
The controlling application is responsible for verifying the write operation by reading back the value written.

Parameters

SlaveID: The slave address 0 to 255
Address: The data address (Base 0)

Description

Writes the first coil stored in the write array.
Function 05 is used.

12.5.2. WriteSingleRegister

Function WriteSingleRegister (SlaveID As Integer, Address As Long) As Integer

Return Value

True if write array is ready and the data are sent. False if array empty or error in parameters.
The controlling application is responsible for verifying the write operation by reading back the value written.

Parameters

SlaveID: The slave address 0 to 255
Address: The data address (Base 0)

Description

Writes the first register stored in the write array.
Function 06 is used.

12.5.3. WriteMultipleCoils

Function WriteMultipleCoils(SlaveID As Integer, Address As Long, Quantity As Integer) As Integer

Return Value

True if write array is ready and the data are sent. False if array empty or error in parameters.
The controlling application is responsible for verifying the write operation by reading back the values written.

Parameters

SlaveID: The slave address 0 to 255
Address: The data address (Base 0)
Quantity The number of data. 1 to 1968

Description

Write the coils stored in the write array.
Function 15 is used.

12.5.4. WriteMultipleRegisters

Function WriteMultipleRegisters(SlaveID As Integer, Address As Long, Quantity As Integer) As Integer

Return Value

True if write array is ready and the data are sent. False if array empty or error in parameters.
The controlling application is responsible for verifying the write operation by reading back the value written.

Parameters

SlaveID: The slave address 0 to 255
Address: The data address (Base 0)
Quantity: The number of data. 1 to 123

Description

Write the registers stored in the write array.
Function 16 is used.

 

Так же в этом разделе:
 
MyTetra Share v.0.53
Яндекс индекс цитирования