0
votes

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;
        }
    }
}
1
A serial port writes one byte at a time.Johnny Mopp
I'm assuming it sends all the bytes at the same time and not individually The write goes into a buffer somewhere and from that buffer into the hardware maybe in a small block or maybe byte by byte depending on the hardware. The hardware then writes the data to the wire one byte at a time along with some extra control bits.user4581301
At the bottom of the MS page it says "By default, SerialPort uses ASCIIEncoding to encode the characters. ASCIIEncoding encodes all characters greater than 127 as (char)63 or '?'. To support additional characters in that range, set Encoding to UTF8Encoding, UTF32Encoding, or UnicodeEncoding." So I'm assuming that's why it's char. going to look at the other support informationj.yang94
@user4581301 when you say "the write goes into a buffer somewhere" I'm assuming you do mean that the write function doesn't necessarily always start at the beginning of the buffer? Do you happen to know where I could read more about the control bits? I'm assuming that's what I'm after. What do you mean by hardware? Isn't the write technically in the buffer in the software so it could be written to the hardware?j.yang94
When I use the default Serial Monitor, I enter the data followed by new line, so I'm assuming the new line is the control bit? How exactly are each bytes separated when being written?j.yang94

1 Answers

2
votes
  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 );