title: How exactly does Serial port write actually write the data from the buffer?
body: <p>So I'm using a Visual Studios Windows form to create a GUI in order to send data to an Arduino. I've been testing how to receive data on the Arduino side, and I can't seem to find an explanation on how the data is being sent. I've been using <code>port.Write()</code> to send the buffer.</p>
  <p><code>port.Write</code> <a href="https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.write?view=netframework-4.7.2#System_IO_Ports_SerialPort_Write_System_Byte___System_Int32_System_Int32_" rel="nofollow noreferrer">Information from the Microsoft page</a></p>
  <p>The Arduino Serial Monitor reads ASCII Characters but since I'm using the GUI to communicate to the Arduino I'm not exactly sure how the data is being transmitted. In the Arduino Serial Monitor, I enter the data and press enter. This means if I input all my data and press enter, it's reading all the data as 1 byte. By entering the the data individually followed by enter it's a byte being read and the terminated by a newline.</p>
  <ol>
  <li><p>How exactly is the data being transferred? From the MS page since it says that it &quot;writes a specified number of bytes to the serial port using data from a buffer&quot;, I'm assuming it sends all the bytes at the same time and not individually.</p>
  <p>i.e If I send the data as <code>port.Write(new byte[] { b0, b1, b2, b3, b4, b5, b6, b7}, 0, 8);</code> is it basically sending all 8 bytes at once, or is it sending each element individually,if so what is the termination between bytes? I would assume it's the first since it asks for the number of bytes to write(count) so its sending 8 bytes from the array.</p>
  </li>
  <li><p>What terminates between bytes during transmission? The comma separates the array data but in transmission, does it even send the commas to terminate or is it just a stream of data?</p>
  <p>ie. In my Serial Monitor with the code runs, I enter each byte individually, followed by the newline character. The Arduino then reads the byte until it encounters the newline character</p>
  </li>
  <li><p>On the Arduino in C++ you can use Serial.print or Serial.println to have it automatically apply a new line. On the MS page, WriteLine method only writes a string and doesn't have any overloads, so I assume that means there's no way to use byte with this command?</p>
  </li>
  <li><p>If I try to use a for loop to print an individual array, it says cannot convert from <code>byte</code> to <code>char[]</code>. Why is it saying it's a char array when I defined it as a byte array?
  I was previously using the //<code>port.Write(sendbyte, 0, 8);</code> to send the whole array and was getting some of the data.</p>
  </li>
  </ol>
  <p>Code to send from Windows Form GUI:</p>
  <pre><code>//bytes declared and data stored earlier
  byte[] sendbyte = new byte[] { b1, b2, b3, b4, b5, b6, b7, b8};
              
  port = new SerialPort(&quot;COM3&quot;, 9600, Parity.None, 8, StopBits.One);
  port.Open();
  //port.Write(sendbyte, 0, 8);
  
  port.Write(new byte[] { b1, b2, b3, b4, b5, b6, b7, b8}, 0, 8);
  /*for( int x = 0; x &lt;=7; x ++)
  {
      port.Write( sendbyte[x], 0, 1);
  } 
  */
  port.Close();
  </code></pre>
  <p>Code to read the incoming bytes</p>
  <pre><code>void recvBytesWithStartEndMarkers() {
      static boolean recvInProgress = false;
      static byte ndx = 0;
      byte startMarker = 42;
      byte endMarker = 24;
      byte rb;
      byte comp;
  
      while (Serial.available() &gt; 0 &amp;&amp; newData == false) 
      {
          rb = Serial.read();
          if (recvInProgress == true) 
          {
              if (rb != endMarker)
              {
                  receivedBytes[ndx] = rb;
                  ndx++;
                  if (ndx &gt;= numBytes) 
                  {
                      ndx = numBytes - 1;
                  }
              }
              else 
              {
                  receivedBytes[ndx] = ','; // terminate the string
                  recvInProgress = false;
                  numReceived = ndx;  // save the number for use when printing
                  ndx = 0;
                  newData = true;
              }
          }
          else if (rb == startMarker) 
          {
              recvInProgress = true;
          }
      }
  }
  </code></pre>