BECKHOFF

Application Notes www.beckhoffautomation.com

Using Microsoft's OCX Winsock Control to Develop an Application in Visual Basic for the BK9000/BC9000

BC-AppNote-006 Revision 1.0 2 October 2007

For additional documentation, please visit www.beckhoffautomation.com For further assistance, please contact Beckhoff USA support at [email protected]

This application note will describe and provide example code for interfacing to a Beckhoff BK9000 bus coupler directly by using Windows Sockets. This is an alternative to using third party software that may require licensing agreements and registration fees.

Beckhoff Automation 12150 Nicollet Avenue South Burnsville, MN 55337 Phone: + 1 952 / 890 0000 Fax: + 1 952 / 890 2888 [email protected]

Using Microsoft's OCX Winsock Control to Develop an Application in Visual Basic for the BK9000/BC9000

Overview Windows Sockets is accessed with Microsoft's Winsock OCX component that can be loaded into Visual Basic. The Winsock control file is named "mswinsck.ocx" and is included with Microsoft Visual Studio or Visual Basic Professional Edition. An example program called "ModbusTCP.vbp" is available that shows how this control is implemented in Visual Basic to send Modbus commands to a BK9000 bus coupler. To understand the sample code and example program, some knowledge of Visual Basic programming is required. The demonstration Visual Basic program referred to in this document connects to a Beckhoff BK5200. The program can be used to turn outputs on and off with command buttons, set/reset the watchdog timer, and enable an alternating write output function. The user interface for the program is shown below. Modbus/TCP Demonstration Visual Basic Program

16 April 2008

Beckhoff Application Notes

2

Using Microsoft's OCX Winsock Control to Develop an Application in Visual Basic for the BK9000/BC9000

In the screenshot above, the pointer is being used to turn off the first digital output. After the button is pushed, the response message from the coupler is returned and displayed in the upper left text box. By decoding the hex values shown in the first line of this box, it is seen that the response is what was expected for a function code 5 command. See p. 41 of the BC9000 manual for more information on function code 5. The breakdown of the response from the text box above is shown below in detail.

The demonstration program is used with a BK9000 coupler connected to the Ethernet adapter on a PC running the program. As shown in the diagram below, a digital input terminal, digital output terminal, and end terminal are connected to the left of the coupler.

Supporting Documents and References

Beckhoff Modbus BK7300 Documentation ftp://ftp.beckhoff.com/Document/BusTermi/BCoupler/BK7300e.pdf Beckhoff Ethernet Bus Terminal Controller BC9000 Documentation

16 April 2008

Beckhoff Application Notes

3

Using Microsoft's OCX Winsock Control to Develop an Application in Visual Basic for the BK9000/BC9000

ftp://ftp.beckhoff.com/Document/BusTermi/BCoupler/BC9000e.chm Open Modbus Protocol (Download the open modbus standard document, "openmbus.doc.") http://www.modicon.com/openmbus Creating a TCP Component in Visual Basic http://www.15seconds.com/issue/990408.htm

Supporting Software

1.

Microsoft Visual Basic v6.0

2.

Microsoft Winsock Control v6.0 - mswinsck.ocx

3.

Modscan32 - musbus network scanning software assists withtesting and debugging

4.

Example VB Program - ModbusTCP.vbp, available from BeckhoffSupport

16 April 2008

Beckhoff Application Notes

4

Using Microsoft's OCX Winsock Control to Develop an Application in Visual Basic for the BK9000/BC9000

Procedure 1. Load the Winsock Component into Visual Basic

Start Visual Basic and bring up the components window shown at the right by pressing Ctrl-T, or select "insert" from the menu and then "components". There should be a control in the list called "Microsoft Winsock Control". Check the box by this control and hit OK to load the control into the Visual Basic toolbox. If the control does not show up in the list of components, browse or do a search to find the file "mswinsck.ocx" and to load it into the component list.

2. Set Up the TCP Client and Server

An instance of the Winsock control will need to be added to the Visual Basic form for each client and server on the network. For details in server and client design, please refer to p. 19-21 of the Open Modbus TCP/IP Specification (openmbus.doc), which can be downloaded from: www.modicon.com/openmbus The client and server properties RemotePort, RemoreHostIP, and LocalHostIP must be set for both the client and server to communicate. After setting these, the OpenConnection() method is called to establish a connection between the client and server. For more details on setting up a TCP/IP connection, see the example code.

3. Create the Array that Holds the Modbus Command Information

Once a TCP/IP connection is established, data can be passed with the SendData() method. The SendData() method sends the string data in its argument to the coupler. Since the modbus protocol is defined in terms of bytes, it is easier to set up a byte array and then use a For loop to send the individual bytes out to the ocupler.

16 April 2008

Beckhoff Application Notes

5

Using Microsoft's OCX Winsock Control to Develop an Application in Visual Basic for the BK9000/BC9000

Page 37 of the BK9000 manual shows the table below that lists the byte structure used in a Modbus command.

16 April 2008

Beckhoff Application Notes

6

Using Microsoft's OCX Winsock Control to Develop an Application in Visual Basic for the BK9000/BC9000

This table parallels the way that an array can be created to send data out to the bus coupler. The subroutine below is used in the example program to send data to the bus coupler.

The first five bytes of this array will remain the same regardless of the function code sent to the bus coupler. Some arrays will need to be longer or shorter depending on the function code and data that needs to be sent. See p. 37-49 in the BC9000 manual for ndividual definitions of the various Modbus function codes. These tables will help you determine how to load the above array.

16 April 2008

Beckhoff Application Notes

7

Using Microsoft's OCX Winsock Control to Develop an Application in Visual Basic for the BK9000/BC9000

4. Use the SendData() Method to Send the Modbus Command to the Bus Coupler

Once the Modbus command array is loaded, it can be sent out to the coupler using the SendData() method. An example of the code to implement this method is shown here. Viewing the example Visual Basic code shows more details.

5. Watch for Data Arrival and Use the GetData() Method to Read the Data

The DataArrival() method of the Winsock control can be used to watch for incoming messages from the coupler. When a message arrives and the method executes, the bytes are read with the GetData() method in a similar manner to the way they were sent out with a For loop.

16 April 2008

Beckhoff Application Notes

8

Using Microsoft's OCX Winsock Control to Develop an Application in Visual Basic for the BK9000/BC9000

The key in reading the data is to look at byte 6, ibuf(5) in the example, to see how many bytes will be following in the Modbus response message. In this way, a second for loop can be used to read the rest of the message, which contains the data. The ibuf() array data is then decoded into useful Visual Basic HMI information with the If statements.

Watchdog Timeouts

The BK9000 and BC9000 have a built in TCP/IP timeout set at 10 seconds. If there is no TCP communication to the bus coupler, the socket connection will be closed and must be reopened to exchange data again.

16 April 2008

Beckhoff Application Notes

9

Using Microsoft's OCX Winsock Control to Develop an Application in Visual Basic for the BK9000/BC9000

In addition to this basic TCP timeout, there is a Modbus communication watchdog timeout that is built into the logic of the bus coupler. A description of the registers that hold the watchdog information is shown in the table below, and can also be found on p. 48 of the BC9000 manual.

The watchdog is active by default, but can be disabled by writing a 0 to register 0x1120. After the first write telegram the watchdog timer is initiated, and is re-triggered each time a telegram is received from that device. For example, if the watchdog timer is set to 1000 ms, the watchdog value in register 0x1020 will start at 1000 upon the first write and increment down to zero. If the counter reaches zero, the watchdog must be reset in order to restore communication. A second approach, which represents a more sensitive condition for the watchdog, is for the watchdog only to be re-triggered after each write telegram. To do this, write a zero into register 0x1122 (default value "1").

Resetting the Watchdog Timer

If the watchdog timer on the slave has elapsed, it can be reset by writing twice to register 0x1121. The following two values must be written to the register: 0xBECF 0xAFFE 16 April 2008

Beckhoff Application Notes

10

Using Microsoft's OCX Winsock Control to Develop an Application in Visual Basic for the BK9000/BC9000

This can be done either with function 6 or with function 16. The example code below uses function code 6 to send two write commands to the reset registers.

16 April 2008

Beckhoff Application Notes

11

Using Microsoft's OCX Winsock Control to Develop an Application in Visual Basic for the BK9000/BC9000

Setting the Watchdog Timer in Visual Basic

16 April 2008

Beckhoff Application Notes

12