Click here to Skip to main content
15,878,543 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I have this tested code that works.
I am reading the serial port 'COM5' at 9600 Baud Rate, with successful results. The data on this port comes from an arduino board that is reading some other stuff on its I/O pins.
But some results are erroneous. So this is my question. How to eliminate the errors in this serial reading?
View : https://europe1.discourse-cdn.com/arduino/original/4X/e/9/4/e94046a13b95d61da40e4953fedfac9df8a22cb8.png[^]

I have to do either a string trick or to find a specific function inside SerialPort method. Your advise is invaluable.
Thank you.

What I have tried:

C#
using System;
using System.IO.Ports;

namespace ConsoleApplication1
{
    class ArduinoSerialReader
    {
        static void Main(string[] args)
        {
            SerialPort arduinoPort = new SerialPort("COM5", 9600); // Replace "COM3" with the appropriate port name

            try
            {
                arduinoPort.Open(); // Open the serial port
                arduinoPort.DataReceived += new SerialDataReceivedEventHandler(ArduinoPort_DataReceived); // Subscribe to the DataReceived event
                Console.WriteLine("Listening for Arduino data...");

                Console.ReadLine(); // Wait for user input to exit
         
                arduinoPort.Close(); // Close the serial port
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message );
            }
        }


        static void ArduinoPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort port = (SerialPort)sender;
            string receivedData = port.ReadExisting(); // Read the available data from the serial port

            Console.WriteLine(receivedData);
        }
    }
}
Posted
Updated 12-Jun-23 22:54pm
v2
Comments
Graeme_Grant 11-Jun-23 9:03am    
In the old days we used a header + data + checksum with ACK (ok) & NAK (not ok) response handshaking.
0x01AA 11-Jun-23 10:23am    
Looks not like a mistake. Looks more, your PC reads then and when faster than arduino can send.

If you do a quick google search you can find samples to get you started: c# serial communication with arduino - Google Search[^]

Here are the first 2 results returned:
* C# Serial Port Communication Arduino : 4 Steps - Instructables[^]
* Control Arduino from C# Serial Port | C# Tutorials Blog[^]
 
Share this answer
 
Comments
_Q12_ 11-Jun-23 9:18am    
In my OP, I put an image with the actual error in it.
Please look on that image to get the feel of the error I get.
https://imgur.com/56wjWVA
Graeme_Grant 11-Jun-23 9:31am    
As mentioned above, if you want error handling, you need to add more to the data, like a checksum and handshaking. TCP/IP does this internally.
Graeme_Grant 11-Jun-23 9:40am    
This is a bit of a rs-232 comms primer for you: 3 Easy Steps to Understand and Control Your RS232 Devices[^]
I found the solution myself.
That particular error (from the image I posted) was because of this original method used:
string receivedData = port.ReadExisting();

By changing it into
string receivedData = port.ReadLine();

solved that particular 'error'/functionality.
Thats it, Thank you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900