C# Communication Problem

Dear All,

I have a project to send sensor data values from Arduino to C#.

We have written the code below.

int i=0;
char a[25];
String str;
int ap1=3;
int ap2=4;
int val1;
int val2;


void setup()
{
   Serial.begin(9600);
   Serial1.begin(9600);
}

void loop()
{
    
    val1= analog.Read(ap1);
    val2= analog.Read(ap2);
    str = "$";
    str = String(val1);
    str +=",";
    str += String(val2);

    str.toCharArray(a,22);
    Serial.print(a);
    Serial1.println(a);
    delay(20);


}

When I opened the serial monitor of Arduino I saw all the data as true.

but on c# formapplication, It does not show all of them. For example on arduino serial monitor I saw

"$898,317"

on c# application I saw

"$898,31" or "98,317$8"

What am I doing wrong.

I am reading Serial port with Serial Port DataRecieved event on C#

I have connected arduino mega to one pc and connected serial port 1 to another pc with FTDI converter with FT232RL.

Thanks for your helps Best Regards.

I don't know Windows stuff, but I wonder if you are opening and closing the serial port. If so, don't. Keep the serial port open until you are completely finished with the Arduino.

The ideas in this Python - Arduino demo should apply in any PC language.

...R

I am reading Serial port with Serial Port DataRecieved event on C#

So, as soon as the first character arrives, the event is triggered. You need to read, and store, the data until the end of the packet arrives.

The DataReceived event can be triggered only when some character arrives, which simplifies handling serial data.

Hi,

Thanks for your replies.

I do not close serialport on C# application. I always listen comport on C# and waiting data from answer. But as I said to you, data comes wrong sometimes. but when I changed the delay on function from 20ms to 50ms it send true. Also send the data as char array, a character eachtime with a loop but it made the same mistake. when I changed the delay again. It did not make the mistake but sending a chactar with 50ms delay it takes 1 second and it is to high for me. I need to send data with 5hz. so I need to send all data between 200ms.

The C# application is listening to Serial, right? The Serial1 data goes somewhere else, so you can see what is sent, right? So why are you NOT sending the SAME data to Serial and Serial1?

Making (foolish) assumptions about collecting and parsing the data sent to Serial, based on what you see sent to Serial1 is not reasonable when you do not send the same data to each.

You COULD send the same data to each AND then collect the data in the C# app AS FAST AS POSSIBLE, until the carriage return/line feed arrives, and THEN show the collected data.