title: Reading TwinCAT variables are not correct (TwinCAT 3 - C# Framework 4.7.2)
body: <p>I use struct in BeckHoff to read variables. When struct has data variables, I can read vaiables in c#. But when Struct has an function block, I can't read variables correctly.
  But I can write in any case.</p>
  <p>1: I can read these variables on C# (I can write);</p>
  <pre><code>TYPE AxisStruct :
  STRUCT
      EnablePower         : BOOL;         // Enable
      ExecuteHome         : BOOL;         // Home
      HomeSwitch          : BOOL;         // Home Switch
      ExecuteReset        : BOOL;         // Reset
  END_STRUCT
  END_TYPE 
  </code></pre>
  <p>2: I can't read Axis variables correctly on C#; (I can write)</p>
  <pre><code>TYPE fbAxis :
  STRUCT
      Axis        : fbServo;
  END_STRUCT
  END_TYPE
  </code></pre>
  <p>// fbServo Function Block:</p>
  <pre><code>FUNCTION_BLOCK fbServo
  VAR_INPUT
          
      EnablePower         : BOOL;  
      ExecuteHome         : BOOL;  
      HomeSwitch          : BOOL;  
      ExecuteReset        : BOOL;
  END_VAR
  VAR_OUTPUT
      ActualPos           : REAL;
      ActualVel           : REAL;
      HasJob              : BOOL;
      HasError            : BOOL;
  END_VAR
  VAR
     AxisName             : Axis_Ref; 
  END_VAR
  </code></pre>
  <p>3: MAIN in BeckHoff;</p>
  <pre><code>PROGRAM MAIN
  VAR
      Rotary          : fbAxis;
  END_VAR
  
  Rotary.Axis();
  </code></pre>
  <p>4:I try to read on C# like this;</p>
  <pre><code>private void Window_Loaded(object sender, RoutedEventArgs e)
  {
      beckhoffClient.NotificationReceived += BeckhoffClient_NotificationReceived;
      RotaryAxis = Get_NotficationHandleForPages(&quot;MAIN.Rotary.Axis&quot;, 100, 200, typeof(fbServo));
  }
  
  public int RotaryAxis = 0;
      public struct fbServo 
      {
  
          [MarshalAs(UnmanagedType.I1)]
          public bool EnablePower;
          [MarshalAs(UnmanagedType.I1)]
          public bool ExecuteHome;
          [MarshalAs(UnmanagedType.I1)]
          public bool HomeSwitch;
          [MarshalAs(UnmanagedType.I1)]
          public bool ExecuteReset;
          
          [MarshalAs(UnmanagedType.R4)]
          public float ActualPos;
          [MarshalAs(UnmanagedType.R4)]
          public float ActualVel;
          [MarshalAs(UnmanagedType.I1)]
          public bool HasJob;
          [MarshalAs(UnmanagedType.I1)]
          public bool HasError;
      }
      fbServo Axiss = new fbServo();
  
      private void BeckhoffClient_NotificationReceived(object sender, AdsNotificationExEventArgs e)
      {
          if (e.NotificationHandle == RotaryAxis)
          {
              Axiss = (fbServo)e.Value;
              Application.Current.Dispatcher.Invoke((Action)(() =&gt;
              {
                  EnablePowerText.Text = Axiss.EnablePower.ToString();
                  ExecuteHometext.Text = Axiss.ExecuteHome.ToString();
                  HomePostext.Text = Axiss.HomePos.ToString();
                  HomeSwitchtext.Text = Axiss.HomeSwitch.ToString();
                  ExecuteResettext.Text = Axiss.ExecuteReset.ToString();
  
                  ActualPostext.Text = Axiss.ActualPos.ToString();
                  ActualVeltext.Text = Axiss.ActualVel.ToString();
                  HasJobtext.Text = Axiss.HasJob.ToString();
                  HasErrortext.Text = Axiss.HasError.ToString();
  
              }));
          }
      }
  </code></pre>
  <p>5: I can write like this;</p>
  <pre><code>    private void TestButton_Click(object sender, RoutedEventArgs e)
      {
          BeckHoffWriteVal(&quot;MAIN.Rotary.Axis.EnablePower&quot;, true);
      }
  </code></pre>
  <p>When I use function block in struct (number 2), my all boolean values set 'TRUE' and my all float values (real variable in beckhoff) set '0' (picture in below) when I change the order of MarshalAs and its variable, some of float values changes (corrupt data).</p>
  <p><a href="https://i.stack.imgur.com/93WjQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/93WjQ.png" alt="enter image description here" /></a></p>