PLC access

After the first step the address of the PLC memory byte 10 is assigned to the control. That can be done using the attribute refvar and the address token.

refvar="plc/MB10"

The control shows the content of the memory byte 10, if the dialog is started now.

Change this value it is nothing happen in the PLC. The cause lies in the memory behavior of the controls. The default behavior does not write the changes directly. To allow the direct access the tag data_access has to be added after the form tag.

 

Change this value using the programming tool, the control still shows the old value.  Shall the control displays all PLC value modifications, the attributes hotlink and time have to be added.

Special value conversion

The documentation describes the addressing of PLC bytes, words and double words. If a floating point value from the PLC is required, this can be read only as a double word. By this format a control shows this value as an integer and not as a floating point value. To change this behavior, set the control attribute display_format = "real". After this, the control shows this value as a floating point value.


<form name="my_form">
    <init>
    <data_access type="true" />
    <control name="e_float" xpos="100" ypos="24" hotlink="true" display_format="real" format="%9.3f" refvar="plc/md10"/>
    </init>

    <paint>
    </paint>

</form>

Displaying PLC bits

The examples above show the solution to assign a PLC byte to a control. But the PLC interface contains a lot of bit – variables. For that case we have to modify our form as follow:

 

 Each control needs a Boolean value to indicate set or reset status. This can be done when a control is created without a variable assignment. In that case the timer tag is used to read the PLC–variable to calculate the bit value and to copy the value into the control variable.

If the operator changes the control value, the opposite direction of data flow has to be calculated.

During the execution of timer tag the control variable is read and compared with the old value. If the values are unequal the PLC-byte value has to be calculated. After that the value is written into the PLC.

 

 

<DialogGui>
  <let name="plc_mb10">0</let>
  <let name="old_edit_bit1_value">0</let>
  <let name="old_edit_bit2_value">0</let>

  <menu name= "main">
    <open_form name = "PLC bits" />
    <softkey position= "1" >
      <caption>PLC bit%n2</caption>
      <navigation>menu1</navigation>
    </softkey>
  </menu>

  <form name = "PLC bits" >

    <init>
      <caption>PLC bits </caption>

      <control name= "edit_bit1" xpos = "200" ypos="60">
        <!-- set input range-->
        <property min="0" />
        <property max="1" />
      </control>

      <control name= "edit_bit2" xpos = "200" ypos="80">
        <!-- set input range-->
        <property min="0" />
        <property max="1" />
      </control>

      <!-- initialize the variables -->
      <op>old_edit_bit1_value = 0 </op>
      <op> edit_bit1 = 0 </op>
      <op>old_edit_bit2_value = 0 </op>
      <op> edit_bit2 = 0 </op>

    </init>
    <paint>
      <text xpos= "40" ypos="61" >mb10.1</text>
      <text xpos= "40" ypos="81" >mb10.2</text>
    </paint>

    <timer>
      <let name="temp"> </let>
      <let name="update">0</let>

      <!-- first check if the controls have been changed -->

      <op> plc_mb10 = "PLC/mb10" </op>          
      <if>
        <condition> old_edit_bit1_value !=  edit_bit1 </condition>
        <then>
          <if>
            <condition> edit_bit1 == 1 </condition>
            <then>
              <!-- set bit 1 -->
              <op> plc_mb10 = plc_mb10 | 1 </op>
            </then>
            <else>
              <!-- reset bit 1 -->
              <op> plc_mb10 = plc_mb10 & (255 - 1)</op>
            </else>
          </if>
          <!-- mark the modification -->
          <op> update = 1 </op>
        </then>
      </if>


      <!-- ---------------------------------------------------- -->

      <if>
        <condition>old_edit_bit2_value != edit_bit2 </condition>
        <then>
          <if>
            <condition> edit_bit2 == 1</condition>
            <then>
              <!-- set bit  2 -->
              <op> plc_mb10 = plc_mb10 | 2</op>
            </then>
            <else>
              <!-- reset bit 2 -->
              <op> plc_mb10 = plc_mb10 & (255 - 2)</op>
            </else>
          </if>
          <!-- mark the modification -->          
          <op> update = 1 </op>
        </then>
      </if>
      

      <!-- write the result into the PLC -->
      <if>
        <condition> update  == 1 </condition>
        <then>
          <op> "PLC/mb10" = plc_mb10 </op>
        </then>
      </if>

      <!-- ---------------------------------------------------- -->

      <!-- reading part -->


      <op> plc_mb10 = "PLC/mb10" </op>

      <!-- ---------------------------------------------------- -->
      <op> temp = plc_mb10 & 1 </op>
      <if>
        <condition>temp != 0 </condition>
        <then>
          <op> edit_bit1 = 1 </op>
        </then>
        <else>
          <op> edit_bit1 = 0 </op>
        </else>
      </if>
      <!-- ---------------------------------------------------- -->

      <!-- ---------------------------------------------------- -->
      <op> temp = plc_mb10 & 2 </op>
      <if>
        <condition>temp != 0 </condition>
        <then>
          <op> edit_bit2 = 1 </op>
        </then>
        <else>
          <op> edit_bit2 = 0 </op>
        </else>
      </if>
      <!-- ---------------------------------------------------- -->

      <op> old_edit_bit1_value = edit_bit1 </op>
      <op> old_edit_bit2_value = edit_bit2 </op>

      <update_controls type="true"/>

    </timer>
  </form>


  <menu name= "menu1">
    <open_form name = "PLC bits 2" />
    <softkey position= "16" >
      <caption>back</caption>
      <navigation>main</navigation>
    </softkey>
  </menu>

  <function_body name="manage_control" return="true" parameter="edit_field_value, old_value, plc_byte, bit_number">

    <if>
      <condition>old_value != edit_field_value </condition>
      <then>
        <if>
          <condition> edit_field_value == 1</condition>
          <then>
            <!-- set bit -->
            <op> plc_byte = plc_byte | bit_number</op>
          </then>
          <else>
            <!-- reset bit -->
            <op> plc_byte = plc_byte & (255 - bit_number)</op>
          </else>
        </if>
      </then>
    </if>

    <op> $return = plc_byte </op>
  </function_body>


  <form name = "PLC bits 2" >

    <init>
      <caption>PLC bits </caption>

      <control name= "edit_bit1" xpos = "200" ypos="60">
        <!-- set input range-->
        <property min="0" />
        <property max="1" />
      </control>

      <control name= "edit_bit2" xpos = "200" ypos="80">
        <!-- set input range-->
        <property min="0" />
        <property max="1" />
      </control>

      <!-- initialize the variables -->
      <op>old_edit_bit1_value = 0 </op>
      <op> edit_bit1 = 0 </op>
      <op>old_edit_bit2_value = 0 </op>
      <op> edit_bit2 = 0 </op>

    </init>
    <paint>
      <text xpos= "40" ypos="61" >mb10.1</text>
      <text xpos= "40" ypos="81" >mb10.2</text>
    </paint>

    <timer>
      <let name="temp"> </let>
      <let name="update">0</let>

      <!-- first check if the controls have been changed -->

      <op> plc_mb10 = "PLC/mb10" </op>
      <op> temp = "PLC/mb10" </op>


      <function name="manage_control" return="temp">edit_bit1, old_edit_bit1_value , plc_mb10, 1 </function>
      <if>
        <condition>temp != plc_mb10</condition>
        <then>
          <op> update = 1 </op>
        </then>
      </if>
      <op> plc_mb10 = temp </op>
      


      <!-- ---------------------------------------------------- -->

      <function name="manage_control" return="temp">edit_bit2, old_edit_bit2_value , plc_mb10, 2 </function>
      <if>
        <condition>temp != plc_mb10</condition>
        <then>
          <op> update = 1 </op>
        </then>
      </if>
      <op> plc_mb10 = temp </op>

      <!-- write the result into the PLC -->
      <if>
        <condition> update  == 1 </condition>
        <then>
          <op> "PLC/mb10" = plc_mb10 </op>
        </then>
      </if>

      <!-- ---------------------------------------------------- -->

      <!-- reading part -->


      <op> plc_mb10 = "PLC/mb10" </op>

      <!-- ---------------------------------------------------- -->
      <op> temp = plc_mb10 & 1 </op>
      <if>
        <condition>temp != 0 </condition>
        <then>
          <op> edit_bit1 = 1 </op>
        </then>
        <else>
          <op> edit_bit1 = 0 </op>
        </else>
      </if>
      <!-- ---------------------------------------------------- -->

      <!-- ---------------------------------------------------- -->
      <op> temp = plc_mb10 & 2 </op>
      <if>
        <condition>temp != 0 </condition>
        <then>
          <op> edit_bit2 = 1 </op>
        </then>
        <else>
          <op> edit_bit2 = 0 </op>
        </else>
      </if>
      <!-- ---------------------------------------------------- -->

      <op> old_edit_bit1_value =  edit_bit1 </op>
      <op> old_edit_bit2_value =  edit_bit2 </op>

      <update_controls type="true"/>

    </timer>
  </form>

</DialogGui>

bit access bit access