Losing data while communicating with C#

Asked

Viewed 126 times

3

Man Arduous sends data continuously to my C# via serial port. However, I understand that the first figures sent by Arduous are simply disappearing, sometimes disappear more numbers, sometimes less, and always only at the beginning of the application. Example:

Arduino sends -> "0 1 100 1023 45 67 ..."

C# gets -> " 1023 45 67 ..." or "1 100 1023 45 67..."

Currently I am printing in Serial as follows:

//ARDUINO:

int CANAL1, CANAL2, CANAL3;

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

void loop()
{
    analogReference(DEFAULT);
    CANAL1 = analogRead(A0);
    Serial.print(CANAL1,DEC);
    Serial.print(" ");
    CANAL2 = analogRead(A1);  
    Serial.print(CANAL2,DEC);
    Serial.print(" ");
    CANAL3 = analogRead(A2);
    Serial.print(CANAL3,DEC);
    Serial.print(" ");
}

And in my application in C#, to receiving of the data is made as follows:

//C#

string RxString = "";

private void SerialCOM_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    if (SerialCOM.IsOpen)
    {
        SerialPort sData = (SerialPort)sender;
        {
            RxString += sData.ReadExisting();
        }
    }
}

This kind of problem could not happen in my application because it would trigger problems with other functions.

Is there something wrong with receiving or sending the information? I’ve done it and I’ve repeated it many times and all this same problem happens.

  • As you can attest programmatically that the messages sent and received are exactly these?

  • 1

    I landed the Arduino input A0 in the hardware and left a known signal in A1 and A2, made inspection by the Serial Monitor proving that the first number corresponded to the input A0, every time.

  • Losing information in serial communication is normal. Your application should be robust enough to try to signal communication errors in the communication protocol.

  • Anyway, you can try handshaking hardware https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.handshake?redirectedfrom=MSDN&view=netframework-4.7.2

  • If it is normal to lose information in communication, there is some way to know what information and which Arduino input it belongs to by C#?

  • I’ll take a look at this Handshake, thanks

Show 1 more comment

1 answer

0

See if the code below works. In the method you get the data. Usually this is how I get data from Arduino.

private List<byte> byte_buffer = new List<byte>();

private void SerialCOM_DataReceived(object sender, SerialDataReceivedEventArgs e) {

byte_buffer.Clear();
while (SerialCOM.BytesToRead > 0) {
  byte_buffer.Add((byte)SerialCOM.ReadByte());
}
}

Received data is added to a List. If you want, you can use byte_buffer.ToString() to print the received string.

I ask forgiveness in case of any error. I’m on the cell phone, I have no way to test until I get home.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.