0

I am currently trying to build a windows forms app that gets sensor data from an arduino via the serial com.

when checking in the arduino IDE the data gets writen into the serial port correctly. But i can't figure out how to read the data via c#.

class Program
{
    static SerialPort SP;
    static void Main(string[] args)
    {
        SP = new SerialPort();
        SP.PortName = "COM7";
        SP.BaudRate = 9600;
        SP.Handshake = System.IO.Ports.Handshake.RequestToSend;
        SP.Open();

        while (true)
        {
            Console.WriteLine(DateTime.Now.ToString() + " : " + SP.ReadLine());
        }

    }
}

My guess is that the Port is not properly set up, but i have no idea what i am missing.

The Goal is just to receive strings from the arduino, i do not necessarily need to send any data to the arduino.

edit: i am working with an arduino micro

2
  • Add some more details of the currenct behaviour, what exactly is not in not working in which way? Do you receive anything? Do receive skirmish, ..... Commented Aug 25, 2021 at 5:17
  • I receive nothing at all, for testing the arduino prints a new number on the serial port every 0.5s None of these are read on the port, but all show up as intended when opening the serial port in the arduino IDE. Commented Aug 26, 2021 at 5:10

1 Answer 1

1
  • Did you close Arduino IDE?
  • You need to add a wait code before reading from the port

Below is a working example:

private SerialPort _currentPort = new SerialPort("COM7", 9600);

private readonly  object _sync = new object();

public bool Open()
{ 
   _currentPort.Encoding = Encoding.UTF8;
   _currentPort.DtrEnable = true;
   _currentPort.ReadTimeout = 2000;
    try
    {
        if (!_currentPort.IsOpen)
            lock (_sync)
            {
                if (_currentPort.IsOpen)
                    return true;
                _currentPort.Open();
                System.Threading.Thread.Sleep(1500);
            }
    }
    catch (Exception e)
    {
        //_localLogger?.Error($"{_currentPort.PortName}, {e.Message}", e);
        return false;
    }

    return _currentPort.IsOpen;
}

public bool Subscribe()
{
    try
    { 
    
        if (Open())
        {
            _currentPort.DataReceived += CurrentPortOnDataReceived;
            return true;
        } 
        return false; 
    }
    catch (Exception e)
    {
        //_localLogger?.Error($"{_currentPort.PortName}, {e.Message}", e);
        return false;
    } 
}
private void CurrentPortOnDataReceived(object sender, SerialDataReceivedEventArgs e)
{
    if (!_currentPort.IsOpen)
    {
        //_localLogger.Info($"{_currentPort} is closed");
        Open();
    }

    Console.WriteLine(_currentPort.ReadExisting());
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks for the answer first of all. Since i am fairly new to C# i am having a bit of trouble actually getting your code running. Do i paste your solution into a new class in the windows forms App and create a new instance of that class on start ? Also is there anything (that you know of) that i have to do in the arduino code ? Currently i am sending the Serial data with Serial.println();
this archive contains a working example of reading data from a Com port bsasearch.org/WinForms.ComPort.Reader.rar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.