Anda di halaman 1dari 9

White Paper

Using the Modbus/TCP PCWorx Server function block

2008 PHOENIX CONTACT

The information given herein is based on data believed to be reliable, but Phoenix Contact makes no warranties expressed or implied as to its accuracy and assumes no liability arising out of its use by others. This publication is not to be taken as a license to operate under, or recommendation to infringe, any patent.

PHOENIX CONTACT P.O. Box 4100 Harrisburg, PA 17111-0001 717-944-1300 Fax: 717-944-1625 e-mail: info@phoenixcon.com www.phoenixcon.com 2008 PHOENIX CONTACT Paper Title: Using the Modbus/TCP PCWorx Server function block Rev 1.00

Using the Modbus/TCP PCWorx Server function block


Introduction The Modbus Client function block allows data (variables) which are being used within a PCWorx program to be accessed via Modbus/TCP commands. The information in this document was derived through the use of an ILC 150 ETH controller, PCWorx 5.20.17, and the Modbus function block free download Pc_worx_5x_Communication_V1_01.zip. This document contains a quick-start procedure for the basic operation of only the Modbus Server block. To use the function block for Modbus communication, the following operations must be performed in the following order:

Executing the self-extracting zip file, Pc_worx_5x_Communication_V1_01.zip............ Compiling the library project, Communication_V1_01.mwt. Moving the compiled library to the standard library folder...... Creating a PCWorx project.. Adding the library to a PCWorx project.. Placing the Modbus Server Function Block on the worksheet Configuring the block (assigning variables)... Reading and writing to the Function Blocks data array.. Understanding the Array Memory mapping

Page: Page: Page: Page: Page: Page: Page: Page: Page:

3 3 3 3 3 4 6 7 8

It is also suggested that you read the following pdf files, which are included as part of the self-extracting Pc_worx_5x_Communication_V1_01.zip file: MODBUS_TCP_Client_V1_1x_001.pdf MODBUS_TCP_Server_V1_1x_001.pdf

PHOENIX CONTACT P.O. Box 4100 Harrisburg, PA 17111-0001 717-944-1300 Fax: 717-944-1625 e-mail: info@phoenixcon.com www.phoenixcon.com 2008 PHOENIX CONTACT Paper Title: Using the Modbus/TCP PCWorx Server function block Rev 1.00

Extracting and Compiling 1. Locate and download the following communication library: Pc_worx_5x_Communication_V1_01.zip 2. Extract the self-install Pc_worx_5x_Communication_V1_01.zip file. By default, it should extract to the following destination folder: C:\PCWORX5.x\Libraries\Libraries 3. Open PCWorx. 4. Use the File/Open Project option to open the Pc_worx_5x_Communication_V1_01 project. This project should now be located in the default install folder: C:\PCWORX5.x\Libraries\Libraries The project name is: Communication_V1_01.mwt It will be necessary to compile this library project. 5. Once the project Communication_V1_01.mwt is loaded into PCWorx, perform a Build/Make. 6. Once the project has compiled (made) successfully, perform a File/Save All. The Communication_V1_01.mwt project should now be compiled and saved back into the C:\PCWORX5.x\Libraries\Libraries folder. Moving Files and Folders For convenience, the Communication_V1_01 library project can be moved to the PCWorx standard library folder. To perform this step, follow the procedure below: 1. Open PCWorx and click on the Extras (top menu)/Options/Directories (tab). 2. Make note of the path to the Library Directory. By default, on an XP system it should be: C:\Documents and Settings\All Users\Documents\PCWORX\Libraries If the path specified in the Library directory setting differs, use the path specified. 3. Copy the following files from the C:\PCWORX5.x\Libraries\Libraries\ Communication_1.01 to the: C:\Documents and Settings\All Users\Documents\PCWORX\Libraries (or specified path). Files and folders to copy: Folder: File: Adding the Library to a Project 1. Create a project for the controllers being used. 2. Once the project has been created, the communication path has been configured, and the bus has been configured, do the following: Click on View (top menu bar) / IEC Programming Expand all sub folders in the Project Tree Window (left screen) Right click on the Libraries folder and select Insert then select User Library Click on the Communications_V1_01.mwt file that was previously moved to the standard library folder, then click the Include button. The Communications_V1_01 file should appear as a sub file to the Libraries folder.

Communications_V1_01 Communications_V1_01.mwt

PHOENIX CONTACT P.O. Box 4100 Harrisburg, PA 17111-0001 717-944-1300 Fax: 717-944-1625 e-mail: info@phoenixcon.com www.phoenixcon.com 2008 PHOENIX CONTACT Paper Title: Using the Modbus/TCP PCWorx Server function block Rev 1.00

In addition to the Library file, you must also add a firmware library. To do this, perform the following steps: Once again, right click on the Libraries folder and select Insert then select Firmware Library Now open the Bit_Util folder. Click on the Bit_Util.fwl file and then click the Include button. In the Project Tree window you should now see the Communications_V1_01 library and the Bit_Util library shown as sub files to the Libraries folder. Placing the Modbus Server Function Block on a worksheet 1. Click on View (top menu bar) / IEC Programming 2. Open the project worksheet located under the Logical_POUs folder. (This document does not discuss in detail programming procedures in PCWorx.) 3. Place a cross on the worksheet as you normally would do to add an object. 4. Locate the Edit Wizard window (window on right of screen by default) and click on the dropdown menu from which you normally select function groups. 5. Locate and click the Communication_V1_01 group. You should now see all of the MODBUS function blocks. 6. Double click on the MODBU_TCP_ Server_V1_10 block. 7. Use the default name for the block: MODBU_TCP_ Server_V1_10_x and click OK on the Variable Properties window. The Server block should now appear in the main program sheet window as follows:

The block contains six inputs (on left) and six outputs (on right). The MODBUS_Data variable is an in/out array of words and therefore appears on both the input side and the output side. In order to compile (make) and use the block, local or global variables must be assigned to the function blocks local inputs and outputs. The variable used to gain access to the function block must be of the same type. Following is a description of each variable associated with the block.

PHOENIX CONTACT P.O. Box 4100 Harrisburg, PA 17111-0001 717-944-1300 Fax: 717-944-1625 e-mail: info@phoenixcon.com www.phoenixcon.com 2008 PHOENIX CONTACT Paper Title: Using the Modbus/TCP PCWorx Server function block Rev 1.00

Modbus Server FC Variable Descriptions Activate (input): Variable type: BOOL Setting this variable to TRUE enables the block. In the enabled state the block is capable of responding to the following Modbus/TCP request:

Quit (input) Variable type: BOOL Setting this variable to TRUE clears an error and leaves the block in a disabled mode. For basic operation, this variable can be set to a VAR, initial value = 0. AutoQuit (input) Variable type: BOOL Setting this variable to TRUE will cause an error to be acknowledged automatically. It also results in a one cycle presents of diagnostic codes. For basic operation this input can be tied to the Error output. Port (input) Variable type: INT This variable can be set to the Ethernet port required. If no initial value is assigned to this variable, the function block will set it to 502 by default. TimeOut (input) Variable type: TIME This is the blocks Watch Dog Timer. After the first Modbus write request, the block must receive an additional Modbus request within the time set here. If a request does not occur within this time, a timeout Error will occur. Setting this variable to an initial value of T#0s will disable it. Active (output) Variable type: BOOL This variable will be set to TRUE by the block when the block becomes enabled. (Activate input = 1) Connection (output) Variable type: BOOL This variable will be set to TRUE when a Modbus client (master) connects to the IP address of the controller.

PHOENIX CONTACT P.O. Box 4100 Harrisburg, PA 17111-0001 717-944-1300 Fax: 717-944-1625 e-mail: info@phoenixcon.com www.phoenixcon.com 2008 PHOENIX CONTACT Paper Title: Using the Modbus/TCP PCWorx Server function block Rev 1.00

Error (output) Variable type: BOOL This variable will be set to TRUE in the event that an error occurs. DiagCode (output) Variable type: WORD This variable will hold an error code after the occurrence of an error. AddDiagCode (output) Variable type: WORD This variable will hold additional information about the error code after the occurrence of an error. Input / Outputs Modbus_data Variable type: COM_ARR_W_0_2004 This variable is an array of 2005 words. Addressed 0 to 2004. These array cells can be written to or read from using any of the Modbus/TCP commands specified on page three. Configuring the Block Configuring the block consist of assigning variables to the inputs and outputs of the block. The information in this section is based on a basic single block application. There a number of alternatives to these suggestions: 1. Once the Modbus Server FC has been added to the worksheet, the following variables can be assigned to the inputs and outputs. The Usage of the variable; VAR or VAR_GLOBAL, depends on the way in which the block is to be used. The following are only suggestions: FC Variable on block Activate (in) Quit (in) AutoQuit (in) Name Assigned (optional) START RS_STOP Connected to the ERROR output PORT_502 Watch_DOG EN CONNECTED ERROR DIAG DIAG_EXT MB_data Type Usage Init Value 1 0 Usage

BOOL BOOL BOOL

VAR VAR VAR

The block will always be enabled Will not be used Will become TRUE on an error 502 by default Disabled Can be connected to a discrete out point Can be connected to a discrete out point Linked to the AutoQuit Can be displayed Can be displayed Can be accessed with Modbus

Port (in) TimeOut (in) Active (out) Connection(out) Error (out) DiagCode AddDiagCode MODBUS_Data

INT TIME BOOL BOOL BOOL WORD WORD See page : 4

VAR VAR VAR_GLOBAL VAR_GLOBAL VAR VAR_GLOBAL VAR_GLOBAL VAR_GLOBAL

T#0s

Table T-1
PHOENIX CONTACT P.O. Box 4100 Harrisburg, PA 17111-0001 717-944-1300 Fax: 717-944-1625 e-mail: info@phoenixcon.com www.phoenixcon.com 2008 PHOENIX CONTACT Paper Title: Using the Modbus/TCP PCWorx Server function block Rev 1.00

2.

Once all the variables have been assigned, a compile (make) should be performed to confirm that the variables were added correctly.

Reading and Writing Below is an explanation of how a Modbus read and a Modbus write can be performed. This example assumes that the variable names in the previous table (T-1) are being used: Placing data in the MODBUS_Data array: In table T-1, the MODBUS_Data array was given the variable name MB_data. This caused a variable array with the name MB_data to be created in the image of COM_ARR_W_0_2004. If an array cell address is used as the name of a variable, data written to that variable will be placed in the cell addressed by the variable name. The cells of the MB_data array can now be accessed as follows: MB_data[0] = cell 0 MB_data[1] = cell 1 MB_data[n] = cell etc. The example below shows a MOVE command being used to move an integer (7) into the function blocks data array. Any data place in variable REG_0004 will be moved to cell [4] of the MB_data array. This data will now be available for a Modbus read. This data will be located at Modbus holding register 40004 because it has been placed in cell [4] of the array.
Variable Properties MOVE Name: D_type: Usage: Init Val: REG_0004_ WORD_ VAR_GLOBAL 7 Name: MB_data[4] Variable Properties

In the example below, the data located in cell address of MB_data[6] is being moved to the variable Written_data. If a Modbus write command is used to write data to holding register 40006, that data can then be accessed via local variable Written_data.
Variable Properties MOVE Name: MB_data[6] Name: D_type: Usage: Written_data WORD_ VAR Variable Properties

PHOENIX CONTACT P.O. Box 4100 Harrisburg, PA 17111-0001 717-944-1300 Fax: 717-944-1625 e-mail: info@phoenixcon.com www.phoenixcon.com 2008 PHOENIX CONTACT Paper Title: Using the Modbus/TCP PCWorx Server function block Rev 1.00

How the Data Array is Mapped In the preceding example an array variable called MB_data was created. This variable is of the type COM_ARR_W_0_2004. Therefore, MB_data is an array with 2005 elements. The elements are addressed MB_data[0] to MB_data[2004]. Modbus holding register reads and writes as well as Coil status sets and clears all communicate to this same area of memory. The diagram below depicts 3 elements of the MB_data array. If Modbus command 03, read multiple registers of address 0 were performed for these three elements, the three values read would be: 7 (HEX) 15 (HEX) 10 (HEX) cell MB_data[0] cell MB_data[1] cell MB_data[2]

If a Modbus command 01, read single coil of address 0 were performed, it would return the LSB of cell MB_data[0]. If a Modbus command 01, read single coil of address 16 were performed, it would return the LSB of cell MB_data[1]. If a Modbus command 01, read single coil of address 36 were performed, it would return the fifth bit of cell MB_data[2]. Therefore it is up to the user to organize or manage this memory array as to their own needs.
Coil read of address 16 = 1 Coil read of address 0 = 1

0 0 0

0 0 0

0 0 0

0 0 0

0 0 0

0 0 0

0 0 0

0 0 0

0 0 0

0 0 0

0 0 0

0 1 1

0 0 0

0 1 0

1 0 0

1 1 0

MB_data[0] MB_data[1] MB_data[2]

Coil read of address 36 =

PHOENIX CONTACT P.O. Box 4100 Harrisburg, PA 17111-0001 717-944-1300 Fax: 717-944-1625 e-mail: info@phoenixcon.com www.phoenixcon.com 2008 PHOENIX CONTACT Paper Title: Using the Modbus/TCP PCWorx Server function block Rev 1.00

Anda mungkin juga menyukai