j.yang29
j.yang29

Reputation: 65

How exactly does Serial port write actually write the data from the buffer?

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 port.Write() to send the buffer.

port.Write Information from the Microsoft page

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.

  1. How exactly is the data being transferred? From the MS page since it says that it "writes a specified number of bytes to the serial port using data from a buffer", I'm assuming it sends all the bytes at the same time and not individually.

    i.e If I send the data as port.Write(new byte[] { b0, b1, b2, b3, b4, b5, b6, b7}, 0, 8); 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.

  2. 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?

    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

  3. 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?

  4. If I try to use a for loop to print an individual array, it says cannot convert from byte to char[]. Why is it saying it's a char array when I defined it as a byte array? I was previously using the //port.Write(sendbyte, 0, 8); to send the whole array and was getting some of the data.

Code to send from Windows Form GUI:

//bytes declared and data stored earlier
byte[] sendbyte = new byte[] { b1, b2, b3, b4, b5, b6, b7, b8};
            
port = new SerialPort("COM3", 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 <=7; x ++)
{
    port.Write( sendbyte[x], 0, 1);
} 
*/
port.Close();

Code to read the incoming bytes

void recvBytesWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    byte startMarker = 42;
    byte endMarker = 24;
    byte rb;
    byte comp;

    while (Serial.available() > 0 && newData == false) 
    {
        rb = Serial.read();
        if (recvInProgress == true) 
        {
            if (rb != endMarker)
            {
                receivedBytes[ndx] = rb;
                ndx++;
                if (ndx >= 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;
        }
    }
}

Upvotes: 0

Views: 2068

Answers (1)

Kaelan Mikowicz
Kaelan Mikowicz

Reputation: 355

  1. Serial ports are usually buffered, but the Microsoft documentation leads me to believe it writes the entire buffer all at once to the wire, as it can throw a timeout exception. However, if you write 1000 bytes from the windows at a time, you are not guaranteed to read the same number of bytes in each loop on the arduino.

  2. No delimiter between individual bytes. The comma that separates array bytes is really only the syntax of your programming language, ie. it only exists in your code file. Bytes are always laid out sequentially in memory.

  3. Not unless you cast a byte array to a string value. How convert byte array to string

  4. try port.Write( &sendByte[x], 0, 1 );

Upvotes: 2

Related Questions