New to arduino, C# SerialPort problem

Greetings from germany. :slight_smile:

Im new to the arduino community and recently tried my first few sketches on the arduino nano.
Seems like i got hooked while having some freetime besides my highschool degree.
Im currently trying to send the value of 4 (maybe up to 6) pots to a C# program used to control volumes. Im using the Library ResponsiveAnalogRead wich helped greatly with noise from analogRead without implementing my own moving average algorithm. Im then sending the value of the 4 pots as a string (delimited by kommas) with the following code being called in the loop with a delay of 10ms:

void send_data() {
  for(int i = 0; i < count; i++) {
    analog[i].update();
    //1023 only gives 99 with ResponsiveAnalogRead!
    outputData[i] = map(analog[i].getValue(), 0, 1022, 0, 100);
  }
  
  if(new_data()) {    
    for(int i = 0; i < count - 1; i++)
    {
      Serial.print(outputData[i]);
      Serial.print(",");
    }
    
    Serial.println(outputData[count - 1]); 
  }
}

Where “newData()” simply checks if the data changed (comparing to an array wich holds the last values). So that im only sending Data once when a value changes. If i check with the build in serial monitor, i get exactly what i expected. When im now reading this data via a SerialInput in C# and logging it to a listBox like this:

private void Form1_Load(object sender, EventArgs e)
{
    arduino = new SerialPort();
    arduino.BaudRate = 9600;
    arduino.PortName = "COM3";
    arduino.DtrEnable = true;
    arduino.DataReceived += new SerialDataReceivedEventHandler(arduino_DataReceived);
    arduino.Open();
}

private void arduino_DataReceived(object sender, EventArgs e)
{
    string data = arduino.ReadLine();

    listBox1.Invoke((MethodInvoker)(() => {
        //Add read data and scroll to bottom
        listBox1.Items.Add(data);
        listBox1.TopIndex = listBox1.Items.Count - 1;
    }));
}

Im sometimes missing data sent from the arduino. This is often the case when turning a pot very fast to ground or 5v. Instead of getting values of 0 or 100 respectively ill get something like 2 or 98.
I think my “protocol” for exchanging data - if im even allowed to call it that - is probably poorly designed using linebreaks and ReadLine() instead of ReadExisting() and finding the start and end of the received data myself? Already tried doing that but still no luck. In the beginning i tried to always send the data wether the pots had been turned or not. This way i was getting the right data after some time but with some terrible delay. (Buffer filling up, reading isn’t keeping up?)

Im still new to using the arduino and haven’t had any experience with the SerialPort Class (or Serial Ports in general for that matter). Im not asking for a copy&paste solution here but probably for a hint in the right way as im not really succeeding with my “brute force experimental” approach. :stuck_out_tongue:

With kind regards, Collin

what baud ratae are you using? initally use a terminal emulator such as teraterm pro to check that the arduino is sent data correctly this example uses ReadExisting() to read serial data send it to a delegate to add to aa TextBox

// data received from serial port - display on textbox
     private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            //textBox1.AppendText(serialPort1.ReadExisting());  // not thread safe
            string s = serialPort1.ReadExisting();
            textBox1.Invoke(this.myDelegate, new Object[] { s });
        }

     // display seral data on textbox
     public void AddDataMethod(String myString)
     {
         textBox1.AppendText(myString);
     }

in your application in the serialport event handler use readline() to read a complete line of data then parse it

As horace says, if in doubt, use a proven terminal program to debug the Arduino code. If you're 100% confident that the Arduino code does what it should do, you can start developing the C# side of things.