Sei sulla pagina 1di 46

SoMachine

Conversion Functions
Conversion_Functions.project
Example Guide
04/2012

Basic
Intermediate

EIO0000000900.01

Expert

www.schneider-electric.com

The information provided in this documentation contains general descriptions and/or


technical characteristics of the performance of the products contained herein. This
documentation is not intended as a substitute for and is not to be used for
determining suitability or reliability of these products for specific user applications. It
is the duty of any such user or integrator to perform the appropriate and complete
risk analysis, evaluation and testing of the products with respect to the relevant
specific application or use thereof. Neither Schneider Electric nor any of its affiliates
or subsidiaries shall be responsible or liable for misuse of the information contained
herein. If you have any suggestions for improvements or amendments or have
found errors in this publication, please notify us.
No part of this document may be reproduced in any form or by any means,
electronic or mechanical, including photocopying, without express written
permission of Schneider Electric.
All pertinent state, regional, and local safety regulations must be observed when
installing and using this product. For reasons of safety and to help ensure
compliance with documented system data, only the manufacturer should perform
repairs to components.
When devices are used for applications with technical safety requirements, the
relevant instructions must be followed.
Failure to use Schneider Electric software or approved software with our hardware
products may result in injury, harm, or improper operating results.
Failure to observe this information can result in injury or equipment damage.
2012 Schneider Electric. All rights reserved.

EIO0000000900.01 04/2012

Table of Contents

SAFETY INFORMATION 5
ABOUT THE BOOK 7
1. DESCRIPTION13
1.1.

Presentation 13

1.2.

Functions Used in this Example 14

1.3.

Hardware Installation 16

2. DESCRIPTION OF THE EXAMPLE CONTENT 17


3. CREATION OF THE PROJECT18
4. LIBRARY MANAGER 20
5. CFC, LD, OR ST PROGRAM22
5.1.

CFC Program 23

5.2.

LD Program 26

5.3.

ST Program 35

6. RUNNING THE EXAMPLE 37


6.1.

MAST Task Configuration 37

6.2.

Downloading the Example to the Controller 39

6.3.

Running the Example on the Controller 42

EIO0000000900.01 04/2012

EIO0000000900.01 04/2012

Safety Information

Safety Information

Important Information
NOTICE
Read these instructions carefully, and look at the equipment to become familiar with the
device before trying to install, operate, or maintain it. The following special messages
may appear throughout this documentation or on the equipment to warn of potential
hazards or to call attention to information that clarifies or simplifies a procedure.
The addition of this symbol to a Danger or Warning safety label
indicates that an electrical hazard exists, which will result in personal
injury if the instructions are not followed.
This is the safety alert symbol. It is used to alert you to potential
personal injury hazards. Obey all safety messages that follow this
symbol to avoid possible injury or death.

DANGER
DANGER indicates an imminently hazardous situation which, if not avoided,
will result in death or serious injury.

WARNING
WARNING indicates a potentially hazardous situation which, if not avoided, can
result in death or serious injury.

CAUTION
CAUTION indicates a potentially hazardous situation which, if not avoided, can
result in minor or moderate injury.

NOTICE
NOTICE is used to address practices not related to physical injury.
PLEASE NOTE
Electrical equipment should be installed, operated, serviced, and maintained only by
qualified personnel. No responsibility is assumed by Schneider Electric for any
consequences arising out of the use of this material.
A qualified person is one who has skills and knowledge related to the construction
and operation of electrical equipment and the installation, and has received safety
training to recognize and avoid the hazards involved.

EIO0000000900.01 04/2012

EIO0000000900.01 04/2012

About the Book

About the Book

At a Glance
Document Scope
This document describes one of the SoMachine examples.
Since the example described in this document is intended for learning purposes
only, it must not be run, nor tested, on products that are part of a machine or
process.

Validity Note
This document has been updated with the release of SoMachine V3.1.
The technical characteristics of the device(s) described in this manual also appear
online. To access this information online:
Step

Action

Go to www.schneider-electric.com

In the Search box on the home page, type a model number. Do not type any blank
spaces in the model number. To get information on a grouping of similar modules, you
can use the characters **; do not use dots or xx's.

Under All, click Products Product Datasheets and select the model number that
interests you.

To save or print a data sheet as a .pdf file, click Export to PDF.


The characteristics presented in this manual should be the same as those that
appear online. In line with our policy of constant improvement we may revise
content over time to improve clarity and accuracy. In the event that you see a
difference between the manual and online information, use the online information as
your reference.

Related Documents
Title of Documentation

Reference Number

Modicon M238 Logic Controller Hardware Guide

EIO0000000016 (ENG);
EIO0000000017 (FRE);
EIO0000000018 (GER);
EIO0000000019 (SPA);
EIO0000000020 (ITA);
EIO0000000021 (CHS)

SoMachine Toolbox Function Library

EIO0000000096 (ENG)

EIO0000000900.01 04/2012

About the Book


Title of Documentation

Reference Number

M238 ExecLoader User Guide

EIO0000000374 (ENG);
EIO0000000737 (FRE);
EIO0000000738 (GER);
EIO0000000739 (SPA);
EIO0000000740 (ITA);
EIO0000000741 (CHS)

Modicon M238 Logic Controller Programming


Guide

EIO0000000384 (ENG);
EIO0000000385 (FRE);
EIO0000000386 (GER);
EIO0000000387 (ITA);
EIO0000000388 (SPA);
EIO0000000389 (CHS)

Modicon M238 Logic Controller System Functions


and Variables M238 PLCSystem Library Guide

EIO0000000364 (ENG);
EIO0000000757 (FRE);
EIO0000000758 (GER);
EIO0000000759 (SPA);
EIO0000000760 (ITA);
EIO0000000761 (CHS)

SoMachine Boolean Functions Example Guide

EIO0000000898 (ENG)

SoMachine Analog Signal Functions Example


Guide

EIO0000000903 (ENG)

SoMachine ASCII Communications Send/Receive Text Example Guide

EIO0000000911 (ENG)

Product Related Information


This document and its related SoMachine project file focus on specific Functions
and Function Blocks of the Schneider Electric libraries provided with SoMachine,
and on specific features available in SoMachine if these features are related to
these libraries. They are intended to help you with developing, testing,
commissioning, and integrating applicative software of your own design on control
systems.
It is intended for new SoMachine users who already have some degree of expertise
in the design and programming of control systems.

WARNING
UNINTENDED EQUIPMENT OPERATION

Only use software approved by Schneider Electric for use with this equipment.
Update your application program every time you change the physical hardware
configuration.

Failure to follow these instructions can result in death, serious injury, or


equipment damage.

EIO0000000900.01 04/2012

About the Book

WARNING
LOSS OF CONTROL

The designer of any control scheme must consider the potential failure modes
of control paths and, for certain critical control functions, provide a means to
achieve a safe state during and after a path failure. Examples of critical control
functions are emergency stop and overtravel stop, power outage and restart.
Separate or redundant control paths must be provided for critical control
functions.
System control paths may include communication links. Consideration must be
given to the implications of unanticipated transmission delays or failures of the
link.
Observe all accident prevention regulations and local safety guidelines.1
Each implementation of this equipment must be individually and thoroughly
tested for proper operation before being placed into service.

Failure to follow these instructions can result in death, serious injury, or


equipment damage.
1

For additional information, refer to NEMA ICS 1.1 (latest edition), "Safety
Guidelines for the Application, Installation, and Maintenance of Solid State Control"
and to NEMA ICS 7.1 (latest edition), "Safety Standards for Construction and Guide
for Selection, Installation and Operation of Adjustable-Speed Drive Systems" or
their equivalent governing your particular location.

Before You Begin


The products specified in this document have been tested under actual service
conditions. Of course, your specific application requirements may be different from
those assumed for this and any related examples described herein. In that case,
you will have to adapt the information provided in this and other related documents
to your particular needs. To do so, you will need to consult the specific product
documentation of the hardware and/or software components that you may add or
substitute for any examples specified in this documentation. Pay particular attention
and conform to any safety information, different electrical requirements and
normative standards that would apply to your adaptation.

WARNING
REGULATORY INCOMPATIBILITY
Be sure that all equipment applied and systems designed comply with all
applicable local, regional and national regulations and standards.
Failure to follow these instructions can result in death, serious injury, or
equipment damage.

The use and application of the information contained herein require expertise in the
design and programming of automated control systems. Only the user or integrator
can be aware of all the conditions and factors present during installation and setup,
operation, and maintenance of the machine or process, and can therefore
determine the automation and associated equipment and the related safeties and
EIO0000000900.01 04/2012

About the Book


interlocks which can be effectively and properly used. When selecting automation
and control equipment, and any other related equipment or software, for a particular
application, the user or integrator must also consider any applicable local, regional
or national standards and/or regulations.
Some of the major software functions and/or hardware components used in the
proposed architectures and examples described in this document cannot be
substituted without significantly compromising the performance of your application.
Further, any such substitutions or alterations may completely invalidate any
proposed architectures, descriptions, examples, instructions, wiring diagrams and/or
compatibilities between the various hardware components and software functions
specified herein and in related documentation. You must be aware of the
consequences of any modifications, additions or substitutions. A residual risk, as
defined by EN/ISO 12100-1, Article 5, will remain if:

it is necessary to modify the recommended logic and if the added or modified


components are not properly integrated in the control circuit.

you do not follow the required standards applicable to the operation of the
machine, or if the adjustments to and the maintenance of the machine are
not properly made (it is essential to strictly follow the prescribed machine
maintenance schedule).

the devices connected to any safety outputs do not have mechanically-linked


contacts.

CAUTION
EQUIPMENT INCOMPATIBILITY
Read and thoroughly understand all device and software documentation before
attempting any component substitutions or other changes related to the
application examples provided in this document.
Failure to follow these instructions can result in injury or equipment
damage.

Start-up and Test


Before using electrical control and automation equipment after design and
installation, the application and associated functional safety system must be
subjected to a start-up test by qualified personnel to verify correct operation of the
equipment. It is important that arrangements for such testing be made and that
enough time is allowed to perform complete and satisfactory testing.

10

EIO0000000900.01 04/2012

About the Book

CAUTION
EQUIPMENT OPERATION HAZARD

Verify that all installation and set up procedures have been completed.

Before operational tests are performed, remove all blocks or other temporary
holding means used for shipment from all component devices.

Remove tools, meters and debris from equipment.

Failure to follow these instructions can result in injury or equipment


damage.

Verify that the completed system, including the functional safety system, is free from
all short-circuits and grounds, except those grounds installed according to local
regulations. If high-potential voltage testing is necessary, follow the
recommendations in equipment documentation to help prevent injury or equipment
damage.

Operation and Adjustments


Regardless of the care exercised in the design and manufacture of equipment or in
the selection and ratings of components, there are hazards that can be encountered
if such equipment is improperly installed and operated.
In some applications, such as packaging machinery, additional operator protection
such as point-of-operation guarding must be provided. This is necessary if the
hands and other parts of the body are free to enter the pinch points or other
hazardous areas where serious injury can occur. Software products alone cannot
protect an operator from injury. For this reason, the software cannot be substituted
for or take the place of point-of-operation protection.

WARNING
UNGUARDED MACHINERY CAN CAUSE SERIOUS INJURY

Do not use this software and related automation equipment on equipment


which does not have point-of-operation protection.
Do not reach into machinery during operation.
Failure to follow these instructions can result in death, serious injury, or
equipment damage.

Ensure that appropriate safeties and mechanical/electrical interlocks related to


point-of-operation protection have been installed and are operational before placing
the equipment into service. All interlocks and safeties related to point-of-operation
protection must be coordinated with the related automation equipment and software
programming.
NOTE: Coordination of safeties and mechanical/electrical interlocks for point-ofoperation protection is outside the scope of the examples and implementations
suggested herein.

EIO0000000900.01 04/2012

11

About the Book


It is sometimes possible to adjust the equipment incorrectly and this may produce
unsatisfactory or unsafe operation. Always use the manufacturer instructions as a
guide to functional adjustments. Personnel who have access to these adjustments
must be familiar with the equipment manufacturer instructions and the machinery
used with the electrical equipment.
Only those operational adjustments actually required by the machine operator
should be accessible to the operator. Access to other controls should be restricted
to help prevent unauthorized changes in operating characteristics.

User Comments
We welcome your comments about this document. You can reach us by e-mail at
techcomm@schneider-electric.com.

12

EIO0000000900.01 04/2012

1. Description

1.

Description

1.1. Presentation
This example presents the Conversion Functions of the Toolbox library. In this
example, these functions are used on a M238 logic controller.
The controllers program is created using SoMachine software.
Related SoMachine project: Conversion_Functions.project
CFC
ST
LD

Supported SoMachine Languages:

IL
FBD
SFC

Key features: Conversion Functions of the Toolbox library


Requirements: To use this example, the user must have:
installed SoMachine V3.1 on a PC.

Download
& Monitoring

PC with SoMachine

EIO0000000900.00 04/2012

M238 logic controller

13

1. Description

1.2. Functions Used in this Example


The Functions (and Function Blocks) used in this example are listed below, grouped
by library:

Toolbox library (Schneider Electric)

Function

Description

Location in the Input Assistant

DWORD_AS_WORD

Splitting the double word into two words

WORD_AS_DWORD

Shifting higher word and adding lower word

Function Blocks { } SE_TBX


Numerical Conversions

Celsius_TO_Kelvin

Converting Celsius to Kelvin

Kelvin_TO_Celsius

Converting Kelvin To Celsius

ArrayOfByte_TO_String

Converting array in byte format to string format

String_TO_ArrayOfByte

Output array and ASCII value of the input string

Celsius_TO_Fahrenheit

Converting Celsius to Fahrenheit

Fahrenheit_TO_Celsius

Converting Fahrenheit to Celsius

Frequency_TO_Period

Calculating period of time of given frequency

Period_TO_Frequency

Calculating frequency of given time

Function Blocks { } SE_TBX


Physical Conversions
Module Calls { } SE_TBX
Numerical Conversions

Module Calls { } SE_TBX


Physical Conversions

NOTE: The other functions of the Toolbox library are used in the examples
described in the SoMachine Boolean Functions Example Guide and in the
SoMachine Analog Signal Functions Example Guide.

14

EIO0000000900.01 04/2012

1. Description
Please refer to the SoMachine online help.
NOTE: In the remaining sections of this document, the former sentence instructs
you to refer to the online help of SoMachine which is accessible through the upperhelp button.
right
Please refer to the SoMachine online help for detailed information on these
Functions and Function Blocks: Function description, Graphical representation, I/O
Variables description, and more.

To install these libraries in your own project, please refer to Library Manager (see
page 20).

EIO0000000900.00 04/2012

15

1. Description

1.3. Hardware Installation


Required Devices

1
3

USB

Controller

2a
or
2b

Prg.
Port

Designation

Reference

Use or Description

SoMachine Software

MSD CHNLMUA

SoMachine Software, 1-station license, installed on a PC

2a

Terminal port/USB
port cordset

TCS XCN AM UM3P

From the mini B USB port on the Modicon M238 base to the
type A USB port on the PC terminal for programming and
updating firmware; length: 3 m (10 ft)

2b

Programming cable

BMX XCA USB H018

Same as TCS XCN AM UM3P, but with two ground


connections along the cable; length: 1.8 m (6 ft)

M238 controller

TM238

Compact base logic controller with 24 I/O (removable battery to


be ordered separately: TSX PLP 01)

Please refer to the Modicon M238 Logic Controller Hardware Guide for the
hardware setup of this device.

16

EIO0000000900.01 04/2012

2. Description of the Example Content

2.

Description of the Example Content


In SoMachine, the configuration of the example is made with the following device:
1 Logic Controller: TM238LFDC24DT
The program of the M238 controller is made of the following specific items:
Library Manager: List of the libraries linked to the programs of this example.
CFC program: Contains the source code that uses the Conversion
Functions of the Toolbox library. This is the default program since it is called
by the MAST task of the controller.
LD program: Translation of the CFC program into LD language. To run this
program on the controller, change the POU called by the MAST task of the
controller from Conversion_Functions_CFC to Conversion_Functions_LD.
ST program: Translation of the CFC program into ST language. To run this
program on the controller, change the POU called by the MAST task of the
controller from Conversion_Functions_CFC to Conversion_Functions_ST.
Task Configuration: The standard MAST task, cyclically called every 20ms.
Example of the content visible in the Devices panel of the Program tab:

EIO0000000900.00 04/2012

17

3. Creation of the Project

3.

Creation of the Project


The steps listed in the following table describe how to create the SoMachine project,
and how to set up the device(s) used in this example. No details are given here
since it is assumed that you already know the Basic commands of SoMachine.

Step
1

Action
In the Create new machine part of the Home tab, select Start with empty project to
create a new SoMachine project.
Give this new project the following name: Conversion_Functions.

In the Configuration tab, add a TM238LFDC24DT Logic Controller.

NOTE: Details on the selected controller are displayed in the Information section of
SoMachine.
SoMachine controller version: Defines the version of the selected controller; it is
displayed in the Information section of SoMachine.
Target controller firmware version: Defines the firmware version of your controller. This
version is shown when you select your controllers node, as shown in Downloading the
Example to the Controller (see page 40).
For compatibility purposes between a SoMachine controller version and a target
controller firmware version, only the first three numbers of a version must be identical. In
the preceding dialog display, the 2.0.30.13 SoMachine controller version is compatible
with any 2.0.30. target controller firmware version.
For each controller model, SoMachine only presents the latest available version. If you
check the Display all versions (for expert only) option, SoMachine will list the
supported controller firmware versions. However, a good practice consists in using the
latest available version and updating the firmware of your controller, if required. Please
refer to the M238 ExecLoader User Guide.
18

EIO0000000900.01 04/2012

3. Creation of the Project


Step

Action

Rename this controller to M238_Controller.

Save your new project.

EIO0000000900.00 04/2012

19

4. Library Manager

4.

Library Manager
The steps listed in the following table describe how to add and/or check the list of
the libraries linked to this example.

Step
1

Action
Select the Program tab.
In the Devices tree view, double-click on the Library Manager to open the list of the
libraries linked to the Application software of this example.

Click on the Add library command of the Library Manager.

In the Add Library window:


Select the Toolbox library, located in the Util libraries and provided by Schneider
Electric.
Click OK.

20

EIO0000000900.01 04/2012

4. Library Manager
Step

Action
This adds the Toolbox library to the list of libraries linked to the project.

NOTE: In the Library Manager, this library is represented in black to inform that it has
been manually linked to the program and that it can be removed.

EIO0000000900.00 04/2012

21

5. CFC, LD, or ST Program

5.

CFC, LD, or ST Program


Each of the following three chapters describes how to create the program used in
the example. Choose the language of your program (CFC, LD, or ST) and refer to
the corresponding chapter:
CFC Program ........................................................................................ 23
LD Program ........................................................................................... 26
ST Program ........................................................................................... 35
You only need to write your SoMachine program in one of these three languages.
In addition, each of these three chapters begins with detailed explanations of any
optional steps.

22

EIO0000000900.01 04/2012

5. CFC, LD, or ST Program

5.1. CFC Program


Step
1

Action
Creation of the POU: Create a new POU in CFC language, called
Conversion_Functions_CFC.

Upon creation of this POU, it is automatically opened by SoMachine.


2

CFC variables: In the upper part of the CFC editor, declare the following variables:
PROGRAM Conversion_Functions_CFC
VAR
(* Temperature Conversions *)
(* Celsius to Fahrenheit Conversion *)
i_rCelsiusValue
: REAL
q_rFahrenheitValue
: REAL;
(* Celsius to Kelvin Conversion
q_rKelvinValue
q_xErrorConvCelsiusToKelvin
CelsiusToKelvinConversion

*)
: REAL;
: BOOL;
: Celsius_TO_Kelvin;

(* Kelvin to Celsius Conversion


i_rKelvinValue
q_rKelvinToCelsiusValue
q_xErrorConvKelvinToCelsius
KelvinToCelsiusConversion

*)
: REAL
: REAL;
: BOOL;
: Kelvin_TO_Celsius;

(* Fahrenheit to Celsius Conversion *)


i_rFahrenheitValue
: REAL
q_rFahrenheitToCelsiusValue
: REAL;
(* Period & Frequency Conversions *)
(* Period to Frequency Conversion *)
i_tPeriodMs
: TIME
q_rFrequencyHz
: REAL;
(* Frequency to Period Conversion *)
i_rFrequencyHz
: REAL
q_tPeriodMs
: TIME;

:= 25.0;

:= 298.15;

:= 77.0;

:= T#20MS;

:= 50.0;

(* String & Array of Byte Conversions *)


(* String to Array of Byte *)

EIO0000000900.00 04/2012

23

5. CFC, LD, or ST Program


Step

Action
i_sString
i_xInStringOrder
q_ArrayOfByte
(* Array of Byte to String *)
i_ArrayOfByte
[72,101,108,108,111];
i_xInArrayOfByteOrder
i_xInsertSpace
q_sString

: STRING (255)
:= 'Hello';
: BOOL
:= TRUE;
: ARRAY [1..255] OF BYTE;

: ARRAY [1..255] OF BYTE :=


: BOOL
: BOOL
: STRING (255);

:= TRUE;
:= TRUE;

(* Dword & Word *)


(* Dword as Word *)
i_dwDword
q_wLowWord
q_wHighWord
Splitting_Dword

:
:
:
:

DWORD
WORD;
WORD;
DWORD_AS_WORD;

:= 1310865;

(* Word as Dword *)
i_wLowWord
i_wHighWord
q_dwDword
Shifting_Words

:
:
:
:

WORD
WORD
DWORD;
WORD_AS_DWORD;

:= 145;
:= 20;

END_VAR

24

CFC program: In the lower part of the CFC editor, implement the following program:

EIO0000000900.01 04/2012

5. CFC, LD, or ST Program


Step

Action

Order By Data Flow


When you have finished implementing this CFC program, perform the following steps to
correctly set the execution order of its blocks:
Right-click on an empty area of the central worksheet.
In the Execution Order part of the contextual menu, execute the Order By Data
Flow command.

EIO0000000900.00 04/2012

25

5. CFC, LD, or ST Program

5.2. LD Program
The following table presents optional steps that give you information on how to
program in LD language.
Step
1

Action
Optional step
How to display titles and comments in LD language
Select the Options command of the Tools menu.
Select, in the Options window, the FBD, LD and IL editor section.
If you wish to add a title and/or a comment for each LD network, check the
Show network title and/or the Show network comment options, as shown
below:

26

EIO0000000900.01 04/2012

5. CFC, LD, or ST Program


Step
2

Action
Optional step
How to add the EN/ENO pins on a Function Block
You must perform this step if you wish to append the additional Boolean enable input EN
and Boolean output ENO (Enable Out) to the function blocks of your own programs.
Please refer to the SoMachine online help: search for additional Boolean ENO.
NOTE: The EN/ENO pins are not used in this example.
In the View part of the FBD/LD/IL menu, select the View as function block
diagram mode.

EIO0000000900.00 04/2012

27

5. CFC, LD, or ST Program


Step

Action
Select, in your LD program, the LD network where you want to add a Function
Block (or a Function).
In the FBD/LD/IL menu, execute the Insert Box with EN/ENO command.

28

EIO0000000900.01 04/2012

5. CFC, LD, or ST Program


Step

Action
In the Input Assistant window that is displayed, select the Function blocks
category, if you wish to add a Function Block, or the Module Calls category, if
you wish to add a Function.
Select the Function Block (or the Function) you wish to add in your program.
In the following dialog, the Celsius_TO_Kelvin Function Block of the Toolbox
library is selected. The location of this Function Block is given in Functions Used
in this Example (see page 14):

Click OK.
This adds the selected Function Block (or Function) to your program, with the two
EN and ENO pins.
In the View part of the FBD/LD/IL menu, you can now select the View as ladder
logic mode to return to the default view mode.

EIO0000000900.00 04/2012

29

5. CFC, LD, or ST Program


Step
3

Action
Optional step
How to use the Insert Empty Box Command
Sometimes, the Empty Box created by the Insert Empty Box command does not work
with a Function or Function Block.
This is the case with this example: The Empty Box is not compatible with the Toolbox
librarys Conversion Functions.
Therefore, you must use the Insert Box command instead of the Insert Empty Box
command for any of the Conversion Functions of the Toolbox library.
If, in your own programs, the Empty Box is incompatible with a Function or Function
Block instance, then you must also use the Insert Box command instead of the Insert
Empty Box command.
Example: In the following image, the Celsius_TO_Kelvin Function Block is rejected by
an Empty Box; in this case, the Insert Box command must be used:

Select in your LD program the LD network where you want to add a Function
Block (or a Function).
In the FBD/LD/IL menu, execute the Insert Box command.

30

EIO0000000900.01 04/2012

5. CFC, LD, or ST Program


Step

Action
In the Input Assistant window that is displayed, select the Function blocks
category if you wish to add a Function Block or the Module Calls category if you
wish to add a Function.
Select the Function Block (or the Function) you wish to add in your program.
In the following dialog the Celsius_TO_Kelvin Function Block of the Toolbox
library is selected:

NOTE: The location of the Conversion Functions of the Toolbox library are given
in Functions Used in this Example (see page 14).
Click OK.
This adds the selected Function Block (or Function) to your program.

EIO0000000900.00 04/2012

31

5. CFC, LD, or ST Program


Step
4

Action
Creation of the POU: Create a new POU in LD language, called
Conversion_Functions_LD.

Upon creation of this POU, it is automatically opened by SoMachine.


5

LD variables: In the upper part of the LD editor, declare the following variables:
PROGRAM Conversion_Functions_LD
VAR
(* Temperature Conversions *)
(* Celsius to Fahrenheit Conversion *)
i_rCelsiusValue
: REAL
q_rFahrenheitValue
: REAL;
(* Celsius to Kelvin Conversion
q_rKelvinValue
q_xErrorConvCelsiusToKelvin
CelsiusToKelvinConversion

*)
: REAL;
: BOOL;
: Celsius_TO_Kelvin;

(* Kelvin to Celsius Conversion


i_rKelvinValue
q_rKelvinToCelsiusValue
q_xErrorConvKelvinToCelsius
KelvinToCelsiusConversion

*)
: REAL
: REAL;
: BOOL;
: Kelvin_TO_Celsius;

(* Fahrenheit to Celsius Conversion *)


i_rFahrenheitValue
: REAL
q_rFahrenheitToCelsiusValue
: REAL;
(* Period & Frequency Conversions *)
(* Period to Frequency Conversion *)
i_tPeriodMs
: TIME
q_rFrequencyHz
: REAL;
(* Frequency to Period Conversion *)
i_rFrequencyHz
: REAL
q_tPeriodMs
: TIME;

:= 25.0;

:= 298.15;

:= 77.0;

:= T#20MS;

:= 50.0;

(* String & Array of Byte Conversions *)


(* String to Array of Byte *)
i_sString
: STRING (255)
:= 'Hello';
i_xInStringOrder
: BOOL
:= TRUE;
q_ArrayOfByte
: ARRAY [1..255] OF BYTE;
(* Array of Byte to String *)

32

EIO0000000900.01 04/2012

5. CFC, LD, or ST Program


Step

Action
i_ArrayOfByte
[72,101,108,108,111];
i_xInArrayOfByteOrder
i_xInsertSpace
q_sString

: ARRAY [1..255] OF BYTE :=


: BOOL
: BOOL
: STRING (255);

:= TRUE;
:= TRUE;

(* Dword & Word *)


(* Dword as Word *)
i_dwDword
q_wLowWord
q_wHighWord
Splitting_Dword

:
:
:
:

DWORD
WORD;
WORD;
DWORD_AS_WORD;

:= 1310865;

(* Word as Dword *)
i_wLowWord
i_wHighWord
q_dwDword
Shifting_Words

:
:
:
:

WORD
WORD
DWORD;
WORD_AS_DWORD;

:= 145;
:= 20;

END_VAR

EIO0000000900.00 04/2012

33

5. CFC, LD, or ST Program


Step
6

34

Action
LD program: In the lower part of the LD editor, implement the following program:

EIO0000000900.01 04/2012

5. CFC, LD, or ST Program

5.3. ST Program
Step
1

Action
Creation of the POU: Create a new POU in ST language, called
Conversion_Functions_ST.

Upon creation of this POU, it is automatically opened by SoMachine.


2

ST variables: In the upper part of the ST editor, declare the following variables:
PROGRAM Conversion_Functions_ST
VAR
(* Temperature Conversions *)
(* Celsius to Fahrenheit Conversion *)
i_rCelsiusValue
: REAL
q_rFahrenheitValue
: REAL;
(* Celsius to Kelvin Conversion
q_rKelvinValue
q_xErrorConvCelsiusToKelvin
CelsiusToKelvinConversion

*)
: REAL;
: BOOL;
: Celsius_TO_Kelvin;

(* Kelvin to Celsius Conversion


i_rKelvinValue
q_rKelvinToCelsiusValue
q_xErrorConvKelvinToCelsius
KelvinToCelsiusConversion

*)
: REAL
: REAL;
: BOOL;
: Kelvin_TO_Celsius;

(* Fahrenheit to Celsius Conversion *)


i_rFahrenheitValue
: REAL
q_rFahrenheitToCelsiusValue
: REAL;
(* Period & Frequency Conversions *)
(* Period to Frequency Conversion *)
i_tPeriodMs
: TIME
q_rFrequencyHz
: REAL;
(* Frequency to Period Conversion *)
i_rFrequencyHz
: REAL
q_tPeriodMs
: TIME;

:= 25.0;

:= 298.15;

:= 77.0;

:= T#20MS;

:= 50.0;

(* String & Array of Byte Conversions *)


(* String to Array of Byte *)

EIO0000000900.00 04/2012

35

5. CFC, LD, or ST Program


Step

Action
i_sString
i_xInStringOrder
q_ArrayOfByte

: STRING (255)
:= 'Hello';
: BOOL
:= TRUE;
: ARRAY [1..255] OF BYTE;

(* Array of Byte to String *)


i_ArrayOfByte
[72,101,108,108,111];
i_xInArrayOfByteOrder
i_xInsertSpace
q_sString

: ARRAY [1..255] OF BYTE :=


: BOOL
: BOOL
: STRING (255);

:= TRUE;
:= TRUE;

(* Dword & Word *)


(* Dword as Word *)
i_dwDword
q_wLowWord
q_wHighWord
Splitting_Dword

:
:
:
:

DWORD
WORD;
WORD;
DWORD_AS_WORD;

:= 1310865;

(* Word as Dword *)
i_wLowWord
i_wHighWord
q_dwDword
Shifting_Words

:
:
:
:

WORD
WORD
DWORD;
WORD_AS_DWORD;

:= 145;
:= 20;

END_VAR

ST program: In the lower part of the ST editor, implement the following program:
(* Temperature Conversions *)
(* Celsius to Fahrenheit Conversion *)
q_rFahrenheitValue := Celsius_TO_Fahrenheit(i_rIput := i_rCelsiusValue);
(* Celsius to Kelvin Conversion *)
CelsiusToKelvinConversion.i_rIput
CelsiusToKelvinConversion();
q_rKelvinValue
q_xErrorConvCelsiusToKelvin
(* Kelvin to Celsius Conversion *)
KelvinToCelsiusConversion.i_rIput
KelvinToCelsiusConversion();
q_rKelvinToCelsiusValue
q_xErrorConvKelvinToCelsius

:= i_rCelsiusValue;
:= CelsiusToKelvinConversion.q_rOput;
:= CelsiusToKelvinConversion.q_xErr;

:= i_rKelvinValue;
:= KelvinToCelsiusConversion.q_rOput;
:= KelvinToCelsiusConversion.q_xErr;

(* Fahrenheit to Celsius *)
q_rFahrenheitToCelsiusValue := Fahrenheit_TO_Celsius(i_rIput :=
i_rFahrenheitValue);
(* Period & Frequency Conversions *)
(* Period to Frequency Conversion *)
q_rFrequencyHz := Period_TO_Frequency(i_tIput := i_tPeriodMs);
(* Frequency to Period Conversion *)
q_tPeriodMs := Frequency_TO_Period(i_rIput := i_rFrequencyHz);
(* String & Array of byte Conversions *)
(* String to Array of Byte Conversion *)
q_ArrayOfByte := String_TO_ArrayOfByte(i_sIput := i_sString,
i_xOrdr := i_xInStringOrder);
(* Array of Byte to String conversion *)
q_sString := ArrayOfByte_TO_String(i_abyIput := i_ArrayOfByte,
i_xOrdr
:= i_xInArrayOfByteOrder,
i_xSpce
:= i_xInsertSpace);
(* Dword & Word *)
(* Dword as Word *)
Splitting_Dword.i_dwIput := i_dwDword;
Splitting_Dword();
q_wLowWord
:= Splitting_Dword.q_wLow;
q_wHighWord
:= Splitting_Dword.q_wHigh;
(* Word as Dword *)
Shifting_Words.i_wLow
Shifting_Words.i_wHigh
Shifting_Words();
q_dwDword

36

:= i_wLowWord;
:= i_wHighWord;
:= Shifting_Words.q_dwOput;

EIO0000000900.01 04/2012

6. Running the Example

6.

Running the Example

6.1. MAST Task Configuration


Step
1

Action
In the Devices tree view:
Expand the contents of the M238_Controller (TM238LFDC24DT) item.
Double-click on the MAST task of the Task Configuration item:
Click on the Add POU command.

EIO0000000900.00 04/2012

37

6. Running the Example


Step
2

Action
In the Input Assistant window that is displayed:
Select your program.
In the following dialog, the CFC program described in CFC Program (see page 23),
Conversion_Functions_CFC, is selected:

Click OK.
This adds the selected POU to the list of programs run by the MAST task of the
controller.

38

EIO0000000900.01 04/2012

6. Running the Example

6.2. Downloading the Example to the Controller


The steps listed in the following table describe how to download the example to the
Logic Controller. If needed, please refer to the SoMachine online help for further
information on these steps: search for Communication Settings.
Step
1

Action
Connect the USB programming cable between your PC and the M238 controller.
Please refer to Hardware Installation (see page 16), for the reference and usage of this
cable.

In the Devices tree view, double-click on the M238_Controller (TM238LFDC24DT) item


to open its configuration panel.

In the Communication Settings tab of this panel:


Click on the Gateway-1 node.
Click on the Scan network button.
If the controller is switched on and connected to your PC with the USB programming
cable, it will be detected by SoMachine as shown below:

In this example, the TM238LFDC24DT SN 015342 controller has been detected.

EIO0000000900.00 04/2012

39

6. Running the Example


Step
4

Action
Select this controllers node and click on the Set active path button.

This will show SoMachine the way to reach your controller.


Option: Uncheck the Secure online mode box to avoid validation messages during
future online modifications.
NOTE: The firmware version of your controller is displayed as the Target Version.
5

The following window will display and prompts you to confirm your choice:
Read the hazard message;
Simultaneously press the <Alt> and <F> keys to validate your choice and close
this window;
Or, click on the Cancel button if you can not comply with the statements in the
hazard message.

40

EIO0000000900.01 04/2012

6. Running the Example


Step

Action
Login button of the toolbar to establish a connection from SoMachine to

Click on the
the controller.

7a

If you download the current project to the controller for the first time and if there is no
project on the controller, the following window is displayed:

Click Yes to download the Application software to the controller.


7b

If you download the current project to the controller for the first time and if there is
another project on the controller, the following window is displayed:

Click Yes to download the Application software to the controller.


7c

If you already have downloaded the current project to the controller, the following window
displays if new modifications were made to this project:

Select the Login with download choice.


Click OK to download the Application software to the controller.
7d

If you already have downloaded the current project to the controller, the connection is
immediate if no modifications were made to this project.

Wait for the completion of the download operation.


Once it is finished, the status bar of SoMachine displays the state of the controller:
STOP

Click on the
controller.

Start button of the toolbar to run the Application software on the

The state of the controller switches from

EIO0000000900.00 04/2012

STOP

to

RUN

41

6. Running the Example

6.3. Running the Example on the Controller


The steps listed in the following table describe how to use the example, once it has
been downloaded to the controller.
Step
1

Action
In the Devices panel, double-click on your Conversion_Functions program to open it
(e.g. Conversion_Functions_CFC if you previously selected this program as the
program run by the MAST task of your controller).
This command opens the program in the central panel of SoMachine; the online values of
the variables used by this program are also displayed, as shown below:

Temperature conversions:
Celsius to Fahrenheit conversion using the Celsius_TO_Fahrenheit Function:
To test this conversion Function, proceed as follows:
Change the value of the i_rCelsiusValue variable (expressed in Celsius
degrees); the corresponding Fahrenheit temperature is stored in
q_rFahrenheitValue (expressed in Fahrenheit degrees).
Celsius to Kelvin conversion using the Celsius_TO_Kelvin Function Block:
Name of the Celsius_TO_Kelvin function block instance:
CelsiusToKelvinConversion
To test the CelsiusToKelvinConversion instance, proceed as follows:
Change the value of the i_rCelsiusValue variable (expressed in Celsius
degrees); the corresponding Kelvin temperature is stored in q_rKelvinValue
(expressed in Kelvin degrees).
Please note that an error is generated by this instance if the
i_rCelsiusValue variables value goes beyond the absolute zero
Temperature (273.15C). In this case, the q_xErrorConvCelsiusToKelvin
variable is set to TRUE and the q_rKelvinValue variable is reset to 0.

42

EIO0000000900.01 04/2012

6. Running the Example


Step

Action
Kelvin to Celsius conversion using the Kelvin_TO_Celsius Function Block:
Name of the Kelvin_TO_Celsius function block instance:
KelvinToCelsiusConversion
To test the KelvinToCelsiusConversion instance, proceed as follows:
Change the value of the i_rKelvinValue variable (expressed in Kelvin
degrees); the corresponding Celsius temperature is stored in
q_rKelvinToCelsiusValue (expressed in Celsius degrees).
Please note that an error is raised by this instance if the i_rKelvinValue
variables value goes beyond the absolute zero Temperature (0K). In this case,
the q_xErrorConvKelvinToCelsius variable is set to TRUE and the
q_rKelvinToCelsiusValue variable is set to the absolute zero Temperature
(0K).
Fahrenheit to Celsius conversion using the Fahrenheit_TO_Celsius Function:
To test this conversion Function, proceed as follows:
Change the value of the i_rFahrenheitValue variable (expressed in
Fahrenheit degrees); the corresponding Celsius temperature is stored in
q_rFahrenheitToCelsiusValue (expressed in Celsius degrees).

Period/frequency conversions:
Period to frequency conversion using the Period_TO_Frequency Function:
To test this conversion Function, proceed as follows:
Change the value of the i_tPeriodMs variable (in TIME format); the
corresponding frequency is stored in q_rFrequencyHz (expressed in Hertz).
Please note that the minimum period is 1 ms (T#1ms in TIME format), for a
maximum frequency of 1,000 Hz.
Frequency to period conversion using the Frequency_TO_Period Function:
To test this conversion Function, proceed as follows:
Change the value of the i_rFrequencyHz variable (in Hertz); the
corresponding period is stored in q_tPeriodMs (in TIME format).
Please note that the maximum frequency is 1,000 Hz, for a minimum period of
1 ms (T#1ms in TIME format).

String/array of bytes conversions:


String to array of bytes conversion using the String_TO_ArrayOfByte
Function:
To test this conversion Function, proceed as follows:
Change the contents of the i_sString text variable (a 255-character STRING
in ASCII format).
Each character of this text is converted into its ASCII character code and stored
in one byte of the q_ArrayOfByte 255-byte array.
You can also change the value of i_xInStringOrder from TRUE to FALSE if
you wish to swap the resulting bytes on a two-by-two basis (i.e. the high byte
and the low byte of a word).

EIO0000000900.00 04/2012

43

6. Running the Example


Step

Action
1) The following example converts the 'ABCDEFG' string into a non-swapped
array of bytes (i_xInStringOrder = TRUE):
i_sString
ASCII codes
q_ArrayOfByte

' A B C D E F G'

65 66 67 68 69 70 71 0
1 2 3 4 5 6 7 8

0
9

2) The following example converts the 'ABCDEFG' string into a swapped array
of bytes (i_xInStringOrder = FALSE):
i_sString

'A

G'

ASCII codes
q_ArrayOfByte

66 65 68 67 70 69 00 71
1 2 3 4 5 6 7 8

0
9

Array of bytes to string conversion using the ArrayOfByte_TO_String


Function:
To test this conversion Function, proceed as follows:
Change the ASCII character codes in the i_ArrayOfByte 255-byte array:
Each byte of this array can vary from 32 to 127 (for a printable character), and
the first 0 set in this array marks its end.
The ASCII character codes of this array (up to the first 0) are converted into their
corresponding characters and set into the q_sString text variable (a 255character STRING in ASCII format).
You can also change the value of i_xInArrayOfByteOrder from TRUE to
FALSE if you wish to swap the characters in the resulting text on a two-by-two
basis (i.e. the high byte and the low byte of a word).
If i_xInArrayOfByteOrder is equal to FALSE and if the number of ASCII
character codes in the q_ArrayOfByte array is odd, you can also change the
value of i_xInsertSpace from FALSE to TRUE to add a space character
before the last character.
1) The following example converts a non-swapped array of bytes
(i_xInArrayOfByteOrder = TRUE) into the corresponding 'Hello'
string (odd or even number of bytes):
i_ArrayOfByte
ASCII codes
q_sString

1 2 3 4 5 6
72 101 108 108 111 0

'H e
l
l
o'

NOTE: In this case, the value of i_xInsertSpace has no effect.

44

EIO0000000900.01 04/2012

6. Running the Example


Step

Action
2) The following example converts a swapped array of bytes
(i_xInArrayOfByteOrder = FALSE) into the corresponding 'Hello'
string (even number of bytes):
i_ArrayOfByte
ASCII codes

1 2 3 4 5 6
101 72 108 108 0 111

q_sString

'H

o'

NOTE: Here, the 0 ASCII character code located at position 5 in


i_ArrayOfByte becomes the first 0 only after the conversion, not before.
Thus, the resulting q_sString is 'Hello', not 'Hell', because 6 bytes are
converted, not 4.
NOTE: In this case, the value of i_xInsertSpace has no effect.
3) The following example converts a swapped array of bytes
(i_xInArrayOfByteOrder = FALSE) into the corresponding 'Hello'
string (odd number of bytes). No space is inserted because
i_xInsertSpace is equal to FALSE:
i_ArrayOfByte
ASCII codes

1 2 3 4 5 6
101 72 108 108 111 0

q_sString

'H

o'

4) The following example converts a swapped array of bytes


(i_xInArrayOfByteOrder = FALSE) into the corresponding 'Hello'
string (odd number of bytes). A space is inserted before the last character
because i_xInsertSpace is equal to TRUE, thus resulting in 'Hell o':
i_ArrayOfByte
ASCII codes

1 2 3 4 5 6
101 72 108 108 111 0

q_sString

'H

o'

NOTE: These conversion functions can be useful for ASCII communications. An example
is given in the SoMachine ASCII Communications - Send/Receive Text Example Guide:
The example described in this guide uses the String_TO_ArrayOfByte function to
convert a text into an array of bytes; then, this array of bytes is sent on the controllers
serial line using the SEND_RECV_MSG Function Block of the PLCCommunication library.

EIO0000000900.00 04/2012

45

6. Running the Example


Step
5

Action
Dword/Word conversions:
NOTE: To correctly test these conversions, you must set the Display Mode of
SoMachine from Decimal to Hexadecimal, as shown below:
In the Display Mode part of the Debug/Watch menu, select the Hexadecimal
mode.

Dword to Word conversion using the DWORD_AS_WORD Function Block:


Name of the DWORD_AS_WORD function block instance: Splitting_Dword
To test the Splitting_Dword instance, proceed as follows:
Change the value of the i_dwDword variable; the high Word of this variable is
set into the q_wHighWord variable and its low Word is set into the
q_wLowWord variable.
Example: If i_dwDword = 16#12345678, then q_wHighWord is set to 16#1234
and q_wLowWord is set to 16#5678.
Word to Dword conversion using the WORD_AS_DWORD Function Block:
Name of the WORD_AS_DWORD function block instance: Shifting_Words
To test the Shifting_Words instance, proceed as follows:
Change the values of both i_wLowWord and i_wHighWord variables; the first
variable becomes the low Word of the q_dwDword variable and the second
variable becomes its high Word.
Example: If i_wLowWord = 16#ABCD and i_wHighWord = 16#2345, then
q_dwDword is set to 16#2345ABCD.

46

EIO0000000900.01 04/2012

Potrebbero piacerti anche