C# SerialPort - Receiving extra 255 bytes

Hi Pawel

Thanks for clarifying about the C# code. I just wanted to check there was nothing else going on.

My reading of the C# code is that is is doing the following ...

  1. Opening the serial port to the Arduino (over USB, I guess?)
  2. Writing a single character.
  3. Waiting for a key press on the PC.
  4. When key press received, end the program.

Something you could try is changing the code to be ...

static void Main(string[] args)
        {
            SerialPort serial = new SerialPort("COM4", 9600);
            serial.Open();
      
     // put some code here to write a prompt to the PC screen to press a key

    // add this ReadKey() to pause before writing the character to the Arduino
            Console.ReadKey();

            serial.Write("A");

            Console.ReadKey();
        }

When you run the code, wait a few seconds before hitting the first key on the PC, and see if this changes what is received on the Arduino.

Note: I'm not suggesting that a delay is the way to fix the problem long term, but maybe it will show up if there is a timing problem around opening the serial connection to the Arduino. I had a similar problem a while back with C# to an Arduino Nano over USB. I think (?) what I discovered is that opening the serial over USB in effect resets the Nano, so the C# program thought it was ready when it wasn't.

Cheers

Ray