title: Snap7 writing to a S7-1200 PLC
body: <p>I'm trying to write something to my Siemens <a href="https://nl.wikipedia.org/wiki/Programmable_logic_controller" rel="nofollow noreferrer">PLC</a> with a C++/CLI application.</p>
  <p>Reading is ok (except the first time it reads it gives odd values).</p>
  <p>But writing is doing something completely different then what I want.</p>
  <p>below you can find the code:</p>
  <pre><code>    private: void WriteSiemensDB()
      {
          byte* buffer;
  
          if (ConnectToSiemensPLC()) //Check if you are connected to PLC
          {
          String^ msg;
          int DBNumber = 2;
          bool NDR;
                                                         
          //Getting the values 1 time so buffer has a value
          buffer = sPLC-&gt;ReadDB(DBNumber);
          
          //give variables a value to write it to the PLC
          NDR = true;
          
          sPLC-&gt;SetBitAt(buffer, 0, 0, NDR); //Convert a bool to a bit
      
          msg = sPLC-&gt;WriteDB(DBNumber, buffer); //write to the Datablock in Siemens
  
          MessageBox::Show(msg); //Show if it worked or not
      }
  }
  </code></pre>
  <p>The sPLC-&gt;SetBitAt method:</p>
  <pre><code>void SiemensPLC::SetBitAt(byte buffer[], int Pos, int Bit, bool Value)
  {
      byte Mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
      if (Bit &lt; 0) Bit = 0;
      if (Bit &gt; 7) Bit = 7;
  
      if (Value)
      {
          buffer[Pos] = (byte)(buffer[Pos] | Mask[Bit]);
      }
      else
      {
          buffer[Pos] = (byte)(buffer[Pos] &amp; ~Mask[Bit]);
      }
  }
  </code></pre>
  <p>WriteDB method:</p>
  <pre><code>System::String^ SiemensPLC::WriteDB(int DBnumber, byte buffer[])
  {
      int Result;
      String^ msg;
      Result = MyClient-&gt;DBWrite(DBnumber, 0, 80, buffer);
  
      if (Result == 0)
      {
          msg = &quot;Gelukt!&quot;; //success
      }
      else
      {
          msg = &quot;Mislukt, error:&quot; + Result; //failed
      }
      return msg;
  }
  </code></pre>
  <p>I actually get the message &quot;Gelukt&quot;, but it still writes the rwong values. So something goes wrong with filling my <code>buffer</code>. Am I doing something wrong in with the buffer?</p>
  <p>In C# I have the same kind of application except the buffer is a <code>byte buffer[];</code></p>
  <p>My questions are:</p>
  <ul>
  <li>Whats the difference between a <code>byte* buffer;</code>in C++ and a <code>byte buffer[];</code> in C#?</li>
  <li>When I mouse over on my buffer when I'm debugging, it says <code>buffer* = 0 ''</code>. Does that mean it's empty? if so, why does it still send random numbers to my PLC?</li>
  </ul>