Search code examples
c#c++serial-communication

Unable to transfer complete data stream from Arduino Uno at high baudrate


I am trying to transfer some data at 115200 Bd to a C# form RichTextBox. Below is my Arduino code:

void serialEvent() { 
  
  if (Serial.available()) {
    int command = Serial.parseInt();
    Serial.println(command);

    switch(command) {
    case 1:
      /*executes its job and writes data in the following format in each line - xxxxxx xxx xxx*/
      break;
    case 0:
      /*executes another unrelated job*/
      break;  
    }
  }
}

Now the total written lines stop printing to my C# form around the 6000/7000 line. Why is that, and how to rectify it? I can't reduce my baudrate; in fact, I would like to increase it. I would like to have the data accessible in a way that I can perform mathematical functions on it through the C# form and also copy it if I need to.

Below is my C# form application code part:

private void settext(string val)
    {
        richTextBox1.AppendText(val);
    }

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        string incomstring = serialPort1.ReadLine();
        settext(incomstring);
    }
private void button5_Click_1(object sender, EventArgs e)
    {
        try
        {
            Cursor.Current = Cursors.WaitCursor;
            if (serialPort1.IsOpen)
            {
                serialPort1.WriteLine("1");
            }
            else
            {
                MessageBox.Show("Open Port FIrst.", "Port not open.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            Cursor.Current = Cursors.Default;
        }
        
    }

Solution

  • You mentioned 'Arduino', then your 'serial line' is likely a virtual USB-serial port.

    I had similar problems about a month ago. But I had only control over the code at the receiving end and at a late point in time (for the project) some insight in the code at the sending end.

    Virtual USB-serial connections are a known source of problems. I did some comparison testing with a physical serial line and did not see the kind of problems I had (and still have) on the virtual USB-serial line. Giving it some tought then, I see two approaches.

    The first one is adding buffering and flow control. This is a general technique and you should be able to google up some examples of it.

    The second one is to use a dedicated serial line. Using an Ardino maybe you have some unused I/O spare that you can use for this. An extra requirement is that at your receiving end you need a real serial port. Using an RS232-to-USB converter is not an option for this approach. Then it is much easier to implement flow control by using hardware lines like DTR and RTS or software solutions with Xon/Xoff. You can look up some details at wikipedia

    edit 2022-oct-7 My original problem is still nagging me, got it working.

    Played with the C++ version to add priotiry and found this

    Added a line SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS) at the very start.

    Got stable communication, tested for three hours transferring about 250 MB every 10 minutes continuously.

    Process priority makes a big difference.