Sei sulla pagina 1di 18

Modbus Master (V7) ActiveX Control

Following is a concise users manual for the operation of the Modbus Master V7 ActiveX control I. II. Overview Modbus Master Control Description A. Properties B. Methods C. Events API Reference Visual Basic Example

III. IV.

Control Overview
MbMasterV7.ocx contains a 32-bit custom control which allows Visual Basic and other OLE Container applications to quickly and easily access data points contained in a modbus slave device connected to the PC. The modbus device may be connected directly to a PC COM port or accessed via a network connection using the modbus/TCP, communications protocol standard. Modem connections are also supported using the Win32 TAPI, (Telephony APplication Interface) library. Multiple connections may be made from the application to different COM ports and/or network devices and each connection operates independently, allowing asynchronous operation to multiple devices on multiple modbus networks. The mbMasterV7 Control exposes properties to define the physical connection, ( such as BaudRate, Parity, etc.), and methods to connect to a com port, network, or TAPI device. Each connection is identified by a unique handle returned from the control when the connection is established. The application initiates messages, (reads & writes), to a device using either the PollModbus and/or WriteModbus method. All messages are processed by the control in the background allowing the application logic to continue while the character string is formatted and transmitted to the selected device. The control processes all response messages, verifies the proper checksum and posts the SlaveResponse Event to the application detailing the results of the message transaction. Upon processing the SlaveResponse Event, the application can obtain the results of the read or write request. Distribution The mbMasterV7 control is distributed by WinTECH Software Design as a time-limited demo control that allows you to freely trial the operation before purchasing a developer license. The demo control may be integrated with Visual Basic to build modbus master applications that run for 30 minutes at a time. During this 30-minute trial, the control is fully functional and the application has full access to one or more modbus connections. At the end of 30 minutes, the unlicensed control will stop communicating with the modbus slaves and the application will have to be restarted to begin another 30-minute communication period. Purchasing a license for the control will remove all time restrictions and allow the control to be distributed with the application in a run-time environment with no time limits and no additional royalty or licensing payments.

RegSvr32 RegSvr32.exe is a DOS application supplied by Microsoft for making additions and changes to the Windows Registry. Before the mbMasterV7 control can be used by an application it must be properly installed into the Registry. To do this, execute RegSvr32.exe and supply the full path name to the control as an argument. The following command line shows how to register the mbMasterV7 control, (assuming the file is contained in the C:\MODBUS directory): Regsvr32 c:\modbus\mbMasterV7.ocx

Modbus Master (V7) Control Description


The mbMasterV7 Control consists of properties to define the operating characteristics of a physical connection to a modbus network and methods that allow the application to initiate and read response messages from a connected slave device. The control supports the following modbus message types: 01 02 03 04 05 06 15 16 READ COIL STATUS READ INPUT STATUS READ HOLDING REGISTERS READ INPUT REGISTERS WRITE SINGLE COIL WRITE SINGLE REGISTER WRITE MULTIPLE COILS WRITE MULTIPLE REGISTERS

Logic involved with an application accessing data from a modbus slave device using the mbMasterV7 control would be similar to the following: y y y y y Setup the control properties for a connection Issue a control method to establish the connection Issue a command using one of the above message types Wait for the control to post the SlaveResponse Event Read the message results

The following properties and methods define the operating characteristics of the mbMasterV7 Control:

Properties MODBUS Transmission Mode (0=ASCII 1=RTU). Defines the timeout associated with the protocol for this control. Specified in milliseconds, this is the amount of time the MODBUS driver waits for a slave device to respond after polling for data. Serial Baud Rate setting. (300, 600, 1200, 2400, 4800, 960014400,19200) Parity selection for COM port connections: (0=None, 1=Odd, 2=Even) Number of Stop Bits, (0=1, 1=1.5, 2=2). Number of data bits, (7 or 8) Defines the modbus/TCP server for network connections. This may be in the form of an IP Address or Machine Name. This property will be used by the next call to the ConnectModbusTCP() method. Defines phone number to dial for a modem, (TAPI), connection

TransmissionMode TimeOut

BaudRate

Parity StopBits DataBits TCPDevice

PhoneNumber

Methods HideControl() ConnectSerial() ConnectModbusTCP() DialTAPIDevice() PollModbus() ReadResults() Makes the control invisible. . Attempts a connection to the modbus via one of the PCs COM ports. . Attempts a network connection to a modbus/TCP Server. . Attempts a remote modem connection by dialing the specified TAPI device. Issues a read request, (modbus message type 01-04), to a designated slave. . Reads the results returned from the slave in response to a PollModbus() request Prepares a buffer of data to be written to a slave device. Issues a write request, (message type 05, 06, 15 or 16). Reads the results returned from the slave in response to a WriteModbus() request Cleans up the designated connection and frees up resources for use by other Windows applications.. Allows the application to abort a call in progress. . This method returns the number of TAPI devices installed on the machine. This method returns the TAPI Device name for each installed device. (GetTAPIDeviceName() MUST be called for the device prior to issuing the DialTAPIDevice() method). User Message Methods The following methods allow the application to write a custom byte string to a connected device and read the response. This provides for modbus messages not inherently supported by the control FillUserMsgBuffer() SendUserMsg() ReadUserMsgResponse() Defines the bbyte string to be transmitted to a device.. . Transmits the user-defined message to the device. . Reads the response buffer. .

FillWriteBuffer() WriteModbus() ReadResults()

Disconnect() HangupTAPI() NumberOfTAPIDevices() GetTAPIDeviceName()

Events The Modbus Master Control posts the following events: SlaveReadResponse The SlaveReadResponse Event is fired by the control to signify the completion of a message initiated by the PollModbus() method. The SlaveResponse Event is fired by the control to signify the completion of a message initiated by the WriteModbus() method. The SlaveResponse Event is fired by the control to signify the completion of a message initiated by the SendUserMsg() method. For network and TAPI connections, this event is fired to signify that the connection is complete. ConnectionEstablished is not fired for serial COM connections. Connection Dropped is fired by the control whenever a TAPI or network connection fails. This event may be fired during the initial connection attempt or anytime thereafter to signify a problem with the connection.

SlaveWriteResponse

UserMsgResponse

ConnectionEstablished

ConnectionDropped

Modbus Master (V7) API Reference


Long ConnectSerial (short Port) Example: iHandle = mbMasterV71.ConnectSerial (1) (Connect using COM Port 1) The ConnectSerial() method accepts a single parameter to identify the PC COM port to be used for the connection. The designated port is opened using the current values for the BaudRate, DataBits, StopBits, and Parity properties. The value returned from ConnectSerial contains the handle to the connection, and must be maintained by the controlling application as a parameter to be passed to PollModbus(), WriteModbus(), ReadResponse(), etc. If an error occurs duiring opening of the serial port, the ConnectSerial method returns an INVALID_HANDLE_VALUE, (-1). Long ConnectModbusTCP (short Port) Example: iHandle = mbMasterV71.ConnectModbusTCP (502) (Connect to modbus/TCP Port 502) The ConnectModbusTCP() method instructs the control to establish a modbus/TCP connection to the network device specified by the TCPDevice property using the port number supplied as an argument. (TCP port number 502 is the standard service port defined by the modbus/TCP protocol standard as published by Modicon.) ConnectModbusTCP() will return a non-zero, (positive), value if the connection was initiated, however, this DOES NOT confirm that the connection has been established. The application must process the ConnectionEstablished and ConnectionDropped events to verify that the connection is ready to accept message transactions. Long DialTAPIDevice (short DeviceIndex) Example: iHandle = mbMasterV71.DialTAPIDevice(0) (Connect using the first defined TAPI Device) The DialTAPIDevice() method instructs the control to open a remote TAPI connection by dialing the number specified by the PhoneNumber property. DialTAPIDevice() will return control immediately to the application while the dial attempt is carried out in the background. . An idiosyncrasy of the modbus_master control is that the application must call both NumberOfTAPIDevices() and GetTAPIDeviceName() prior to calling the DialTAPIDevice() method. Failure to do so will result in the TAPI library not being properly initialized for outgoing calls. DialTAPIDevice() will return a non-zero, (positive), value if the connection was initiated, however, this DOES NOT confirm that the connection has been established. The application must process the ConnectionEstablished and ConnectionDropped events to verify that the connection is ready to accept message transactions.

Boolean Disconnect (Long ConnectionHandle) Example: Status = mbMasterV71.Disconnect(iHandle) (Release all resources associated with the specified Connection Handle) Disconnect() closes the specified connection and releases all allocated resources for use by other Windows applications. The return value will be TRUE is the connection is successfully closed

void HangupCall (Long ConnectionHandle) Example: mbMasterV71.HangupCall(iHandle) (Abort a dial attempt) HangupCall() should only be used to abort a TAPI connection attempt, (Dial in Progress). After a TAPI connection has been established, it should be closed using the Disconnect() method.

Long NumberOfTAPIDevices () Example: NumberOfDevices = mbMasterV71.NumberOfTAPIDevices() (Determine the number of installed TAPI Devices) NumberOfTAPIDevices allows the application to interrogate the TAPI library to obtain the number of devices currently installed on the machine. Generally, the application will display each device name in a drop-down device list to allow the user to select the particular modem to use for the connection. An idiosyncrasy of the modbus_master control is that the application must call both NumberOfTAPIDevices() and GetTAPIDeviceName() prior to calling the DialTAPIDevice() method. Failure to do so will result in the TAPI library not being properly initialized for outgoing calls. String GetTAPIDeviceName (Long DeviceIndex) Example: FirstModem = mbMasterV71.GetTAPIDeviceName(0) (Get the name of the first TAPI Device) GetTAPIDeviceName allows the application to interrogate the TAPI library to obtain the name of an installed device. Generally, the application will display each device name in a drop-down device list to allow the user to select the particular modem to use for the connection. An idiosyncrasy of the modbus_master control is that the application must call both NumberOfTAPIDevices() and GetTAPIDeviceName() prior to calling the DialTAPIDevice() method. Failure to do so will result in the TAPI library not being properly initialized for outgoing calls.

Short PollModbus (Long ConnectionHandle, short SlaveNodeAddress, short ModbusFunctionCode, Long Address, short Length) Example: Status = mbMasterV71.PollModbus (iHandle, 1, 3, 100, 10) (Read 10 Holding Registers from Node 1 starting at address 100) The PollModbus() method initiates a read request to the defined slave. ConnectionHandle defines the connection and must be equal to the handle returned from ConnectSerial(), DialTAPIDevice() or ConnectModbusTCP(); SlaveNodeAddress defines the modbus slave. The ModbusFunctionCode must be: Function Code 01 02 03 04 Data Type COIL STATUS INPUT STATUS HOLDING REGISTERS INPUT REGISTERS

Address defines the point address and must be in the range 1-65535. The Length parameter defines the number of data points to read and must be between 1-256. If PollModbus() returns zero, the message has been transmitted out the connection and the application will receive a SlaveResponse event later. (The SlaveResponse event will be fired even if the message times-out.) If PollModbus() returns a non-zero error code, no SlaveResponse event will be fired. Short ReadResults (Long ConnectionHandle, short SlaveNodeAddress, short ModbusFunctionCode, Long Address, Long *pData) Example: Dim MyData(10) as Long For i = 0 to 10 Status = mbMasterV71.ReadResults (iHandle, 1, 3, 100+i, MyData(i)) Next i (Read the response initiated by PollModbus) After an application initiates a read message to a modbus slave, the modbus_master control will fire the SlaveResponse event when the results of the message are available to be read. ReadResults() retrieves the results of the last message transaction for the connection. The SlaveNodeAddress and ModbusFunctionCode parameters must match the last message transmitted for the connection. Results from the slave are returned one value at a time and are stored in pData. The Address parameter passed to ReadResults must be within the range as specified by the Address and Length parameters supplied with PollModbus. ReadResponse will return a zero error status if the message was successfully transmitted to the slave device and appropriate results obtained. If the message times out, (or any other error occurs during processing of the message), the appropriate error will be returned. Refer to the VB example for additional code samples.

Short FillWriteBuffer (Long ConnectionHandle, short Index, Long Data) The FillWriteBuffer() method prepares an internal buffer with data to be transmitted to a slave device. Data must be written to the buffer one value at a time and each value represents a single modbus coil or register. Once the buffer has been filled, it may be transmitted to a designated slave via teh WriteModbus method. Short WriteModbus (Long ConnectionHandle, short SlaveNodeAddress, short ModbusFunctionCode, Long Address, short Length) Example: mbMasterV71.FillWriteBuffer (iHandle, 0, 1234) Status = mbMasterV71.WriteModbus (iHandle, 1, 6, 100, 1) (Write 1234 to address 100 of Node 1) The WritelModbus() method initiates a write request to the defined slave. ConnectionHandle defines the connection and must be equal to the handle returned from ConnectSerial(), DialTAPIDevice() or ConnectModbusTCP(); SlaveNodeAddress defines the modbus slave. The ModbusFunctionCode must be: Function Code 05 06 15 16 Data Type SINGLE COIL WRITE SINGLE REGISTER WRITE MULTIPLE COIL WRITE MULTIPLE REGISTER WRITE

Address specifies the data point address and length defines the number of data values to write. The data to write must have been previously specified using the FillWriteBuffer method.. Short WriteResponse (Long ConnectionHandle, short SlaveNodeAddress, short ModbusFunctionCode, Long Address, short Length,) Example: Status = mbMasterV71.WriteResults (iHandle, 1, 6,100,1) (Read the response initiated by WriteModbus) After an application initiates a write message to a modbus slave, the modbus_master control will fire the SlaveResponse event when the results of the message are available to be read. WriteResults() retrieves the results of the last message transaction for the connection. The SlaveNodeAddress, ModbusFunctionCode, Address, and Length parameters must match the last message transmitted for the connection. WriteResults will return a zero error status if the message was successfully transmitted to the slave device and appropriate results obtained. If the message times out, (or any other error occurs during processing of the message), the appropriate error will be returned. Refer to the VB example for additional code samples.

10

Short FillUserMsgBuffer (Long ConnectionHandle, short Index, short Data) The FillUserMsgBuffer() method prepares an internal buffer with data to be transmitted to a slave device. Data must be written to the buffer one value at a time and each value represents a single byte. The user application is responsible for supplying only the data bytes composing the modbus message to transmit. The MbMasterV7 control will append the appropriate checksum to the buffer when sent. Once the buffer has been filled, it may be transmitted to a designated slave via the SendUserMsg method. Short SendUserMsg (Long ConnectionHandle, short Length) Example: mbMasterV71.FillUserMsgBuffer (iHandle, 0, 1) mbMasterV71.FillUserMsgBuffer (iHandle, 1, 3) mbMasterV71.FillUserMsgBuffer (iHandle, 2, 0) mbMasterV71.FillUserMsgBuffer (iHandle, 3, 1) mbMasterV71.FillUserMsgBuffer (iHandle, 4, 0) mbMasterV71.FillUserMsgBuffer (iHandle, 5, 10) Status = mbMasterV71.SendUserMsg (iHandle, 6) (Write the string 01030001000A followed by CRC) The SendUserMsg() method initiates a write request to the defined connection. ConnectionHandle defines the connection and must be equal to the handle returned from ConnectSerial(), DialTAPIDevice() or ConnectModbusTCP(); The MbMasterV7 control will calculate and append the appropriate checksum to the message. If the connection is using the ASCII Transmission mode, the appropriate colon character will be added to the front of the message as well as the CRLF combination at the end. Short ReadUserMsgResponse (Long ConnectionHandle, short Index, short *pData) Example: Status = mbMasterV71.ReadUserMsgResponse (iHandle, 0,Value) (Reads the first byte from the response buffer) After an application initiates a user message to a modbus slave, the MbMasterV7 control will fire the UserMsgResponse event when the results of the message are available to be read. Parameters contained within the event define the connection and the number of bytes received from the remote in response to a user defined message. ReadUserMsgResponse() allows the application to read the resulting byte string from the receive buffer.

11

Visual Basic Example


An example Visual Basic project is included with the mbMasterV7 distribution zip file. This example, (Project1), was designed using Visual Basic 6.0 and consists of a single VBA form containing standard controls to allow you to connect, read and write values from/to a modbus slave device. The example is designed to mirror the api of the mbMasterV7 control with buttons to connect serially, or via a modem, or network. Edit controls allow you to select a slave node address, read a group of data points and/or write a group of points. To use this example with VB.Net, simply open the project file, (project1.vbp), using the .NET Developer Studio and allow the project to be automatically converted to a VB.NET Project.

Default settings for the example application operate on COM Port number 1 at 9600 Baud, 8 Data bits, 1 Stop Bit and no parity, using the RTU transmission mode. The default values for these comm port settings as well as the PhoneNumber and TCPDevice properties used for TAPI and modbus/TCP connections easily customized by accessing the modbus_master property page during design time or programmatically as in the example code. Operation of the Project1 example begins by opening a connection using one of the three buttons on the left of the form. When a connection is successful, the Read and Write buttons will be activated. Pressing the Read Button initiates a PollModbus() method using the defined slave adrdess, point type, point address and length values. The results of the read request are obtained whenever the modbus_master fires the SlaveResponse event:

12

'Global Data Definitions Dim MyHandle As Long Dim MyStatus As Integer Dim Slave As Integer Dim Cmd As Integer Dim Address As Long Dim Length As Integer

'Handle to Connection 'Status returned from mbMasterV7 Control 'Slave, Cmd, Address, & Length 'represent last message sent to modbus

' Generic function to display status value returned ' from PollModbus, WriteModbus Public Sub show_status(ErrCode As Integer) If (ErrCode = 0) Then STATUS.Text = "OK" ElseIf (ErrCode < 255) Then STATUS.Text = "Slave Device Exception Response" ElseIf (ErrCode = 256) Then STATUS.Text = "Invalid Connection Handle" ElseIf (ErrCode = 257) Then STATUS.Text = "Message Overrun" ElseIf (ErrCode = 258) Then STATUS.Text = "Invalid Point Address" ElseIf (ErrCode = 259) Then STATUS.Text = "Invalid Slave Node Address" ElseIf (ErrCode = 260) Then STATUS.Text = "Invalid Length" ElseIf (ErrCode = 261) Then STATUS.Text = "Unsupported Modbus Command" ElseIf (ErrCode = 263) Then STATUS.Text = "Slave Device Time-Out" ElseIf (ErrCode = 264) Then STATUS.Text = "Invalid Transmission Mode" ElseIf (ErrCode = 265) Then STATUS.Text = "Invalid CRC In Slave Response" ElseIf (ErrCode = 266) Then STATUS.Text = "Connection Not Established" ElseIf (ErrCode = 267) Then STATUS.Text = "Invalid Slave Response" ElseIf (ErrCode = 271) Then STATUS.Text = "Demo Time Expired" ElseIf (ErrCode = 272) Then STATUS.Text = "Invalid modbus/TCP Command" End If End Sub ' Hide the contrtol when the form loads ' For VB.NET this must be moved to the ' FORM1_ACTIVATED procedure Private Sub Form_Load() MbMasterV71.HideControl End Sub

13

' Handler for the CONNECT SERIAL Button Private Sub ConnectSerial_Click() ' Connect to COMM Port MbMasterV71.BaudRate = 9600 '9600 Baud MbMasterV71.Parity = 0 '0=NOPARITY 1=ODDPARITY, 2=EVENPARITY 3=MARKPARITY 4=SPACEPARITY '8 DataBits MbMasterV71.DataBits = 8 MbMasterV71.StopBits = 0 '0=ONESTOPBIT, 1=ONE5STOPBITS, 2=TWOSTOPBITS MbMasterV71.TimeOut = 2000 '2000 msec MbMasterV71.TransmissionMode = 1 '0=ASCII, 1=RTU MyHandle = MbMasterV71.ConnectSerial(1) ' Connect to COMM Port 1 If MyHandle > 0 Then ' Connection was successful ' (This example only allows a single connection) ' Disable All Connection Buttons ' Enable the Read, Write & Disconnect Buttons ConnectSerial.Enabled = False ConnectTAPI.Enabled = False ConnectTCP.Enabled = False Disconnect.Enabled = True STATUS.Text = "Connected" READMODBUS.Enabled = True WRITEMODBUS.Enabled = True Else 'Connection Attempt Failed '(Another application must have control of the COM Port) STATUS.Text = "Not Connected" End If End Sub

14

' Handler for the CONNECT TAPI Button Private Sub ConnectTAPI_Click() Dim nTAPIDevices As Long Dim TAPIDevice As String 'Go through the motions of getting the TAPI Device List nTAPIDevices = MbMasterV71.NumberOfTAPIDevices() TAPIDevice = MbMasterV71.GetTAPIDeviceName(0) 'Setup the phone number to dial MbMasterV71.PhoneNumber = "645-5966" 'Dial the call MyHandle = MbMasterV71.DialTAPIDevice(0) If MyHandle > 0 Then 'Call should be in progress now 'Don't enable the Read & Write Buttons 'until we get the CallEstablished Event STATUS.Text = "Connecting" ConnectSerial.Enabled = False ConnectTAPI.Enabled = False ConnectTCP.Enabled = False Disconnect.Enabled = False READMODBUS.Enabled = False WRITEMODBUS.Enabled = False Else STATUS.Text = "Not Connected" End If End Sub ' Handler for the CONNECT TCP Button Private Sub ConnectTCP_Click() ' Select the Device to connec to ' In this case use the IP Loopback address to ' connect to the local machine MbMasterV71.TCPDevice = "127.0.0.1" MyHandle = MbMasterV71.ConnectModbusTCP(502) If MyHandle > 0 Then 'Connection should be in progress now 'Don't enable the Read & Write Buttons 'until we get the CallEstablished Event STATUS.Text = "Connecting" ConnectSerial.Enabled = False ConnectTAPI.Enabled = False ConnectTCP.Enabled = False Disconnect.Enabled = False READMODBUS.Enabled = False WRITEMODBUS.Enabled = False Else STATUS.Text = "Not Connected" End If End Sub

15

' ConnectionEstablished Event Handler ' Initiated from either ConnectModbusTCP() or DialTAPIDevice() Private Sub MbMasterV71_ConnectionEstablished(ByVal hConnect As Long) 'Enable the Disconnect Button 'Enable Read & Write Buttons ConnectSerial.Enabled = False ConnectTAPI.Enabled = False ConnectTCP.Enabled = False Disconnect.Enabled = True STATUS.Text = "Connected" READMODBUS.Enabled = True WRITEMODBUS.Enabled = True End Sub ' ConnectionDropped Event Handler Private Sub MbMasterV71_ConnectionDropped(ByVal hConnect As Long) 'Either the TCP or TAPI connection attempt failed 'or something has happened to abort the connection after it 'has been established for a while. 'In either case we're now disconnected so enable the 'buttons accordingly MyHandle = -1 ConnectSerial.Enabled = True ConnectTAPI.Enabled = True ConnectTCP.Enabled = True Disconnect.Enabled = False STATUS.Text = "Not Connected" READMODBUS.Enabled = False WRITEMODBUS.Enabled = False End Sub

' Handler for the DISCONNECT Button Private Sub Disconnect_Click() 'Tell the control to Disconnect MyStatus = MbMasterV71.Disconnect(MyHandle) MyHandle = -1 'ReEnable the Connect Buttons for new connection attempt ConnectSerial.Enabled = True ConnectTAPI.Enabled = True ConnectTCP.Enabled = True Disconnect.Enabled = False STATUS.Text = "Not Connected" READMODBUS.Enabled = False WRITEMODBUS.Enabled = False End Sub

16

' Handler for the READ Button Private Sub READMODBUS_Click() 'Get the Slave Node Address, Modbus Command, Address & Length ' from the appropriate Edit controls Slave = NODEADDRESS.Text Cmd = POINTTYPE.Text Address = READADDRESS.Text Length = READLENGTH.Text 'We must remember these parameters so we can use 'them in the ReadResponse method to make sure we 'get what we ask for ' 'Initiate the Read Request MyStatus = MbMasterV71.PollModbus(MyHandle, Slave, Cmd, Address, Length) 'Check the status to make sure the request went out. If MyStatus = 0 Then STATUS.Text = "Busy" Else show_status (MyStatus) End If End Sub 'Process the Slave Read Response Message 'Modbus_Master SlaveReadResponse Event Handler Private Sub MbMasterV71_SlaveReadResponse(ByVal hConnect As Long) Dim MyData As Long Dim i As Integer ' Read the data returned from the slave ' and update the text controls For i = 0 To Length - 1 MyStatus = MbMasterV71.ReadResults(hConnect, Slave, Cmd, Address + i, MyData) show_status (MyStatus) If MyStatus = 0 Then Text1(i).Text = MyData Else Text1(i).Text = "Error" End If Next i ' For display purposes, flag the unread data as ? For i = Length To 49 Text1(i).Text = "?" Next i End Sub

17

' Handler for the WRITE Button Private Sub WRITEMODBUS_Click() Dim IsRegister As Boolean Dim i, junk As Integer 'Get the Slave Node Address, Modbus Command, Address & Length ' from the appropriate Edit controls Slave = NODEADDRESS.Text Address = WRITEADDRESS.Text Length = WRITELENGTH.Text 'Make sure we do not overrun the message ' buffer allocated within the control If Length > 200 Then Length = 200 End If ' Fill the write buffer with the specified number ' of values For i = 0 To Length - 1 junk = MbMasterV71.FillWriteBuffer(MyHandle, i, PATTERN.Text) Next i ' figure out which modbus command to use If POINTTYPE.Text < 2 Then If Length = 1 Then Cmd = 5 'write single coil Else Cmd = 15 'write multiple coils End If Else If Length = 1 Then Cmd = 6 'write single register Else Cmd = 16 'write multiple registers End If End If ' Initiate the Write Request Message MyStatus = MbMasterV71.WRITEMODBUS(MyHandle, Slave, Cmd, Address, Length) ' Make sure the write request was transmitted If MyStatus = 0 Then STATUS.Text = "Busy" Else show_status (MyStatus) End If

End Sub 'Process the Slave Write Response Message 'Modbus_Master SlaveWriteResponse Event Handler Private Sub MbMasterV71_SlaveWriteResponse(ByVal hConnect As Long) 'read the results of the write request MyStatus = MbMasterV71.WriteResults(hConnect, Slave, Cmd, Address, Length) ' and update the status display show_status (MyStatus) End Sub

18

Potrebbero piacerti anche