2

I am trying to create a communication between an Arduino Leonardo and C#.

Just now, the Arduino's software sends a simple message (in loop) on the serial port:

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL);
}
void loop() {
  Serial.println("test");
  delay(500);
}

C# try only to read these messages and print them on the shell:

public class Program
{
    private SerialPort mySerialPort;

    static void Main(string[] args)
    {
        Program p = new Program();
        Console.WriteLine("PORTS: " + String.Join(" ", p.getSerialPortsList())+ ", enter to start.");
        Console.Read();
        p.SerialRead("COM6");
    }

    public String[] getSerialPortsList()
    {
        string[] ports = SerialPort.GetPortNames();
        return ports;
    }

    public void SerialRead(String com)
    {
        mySerialPort = new SerialPort(com, 9600, Parity.None, 8, StopBits.One);
        Console.Read();
        Console.WriteLine("Incoming Data:");
        SerialRead sr = new SerialRead();
        Thread rs = new Thread(sr.StartRead);
        sr.SetMySerialPort(mySerialPort);
        rs.Start();
        while (!rs.IsAlive);
        Console.Read();
        sr.SetSuspendThread(true);
        rs.Join();
    }

}


public class SerialRead
{

    private Boolean suspendThread = false;
    SerialPort mySerialPort;

    public void StartRead()
    {
        mySerialPort.Open();
        Thread.Sleep(500);
        int i = 0;
        while (!suspendThread)
        { 
            i++;
            Console.WriteLine(i + ": " + mySerialPort.ReadLine());
            Thread.Sleep(500);
        }
    }

    public void SetMySerialPort(SerialPort mysp){ mySerialPort = mysp; }
    public void SetSuspendThread(Boolean a){ suspendThread = a; }

}

The output of this C# software depends. If I use the serial monitor on the Arduino IDE, then I receive the string's stream correctly (one each 500ms). Otherwise, the C# software freezes. Sometimes, I receive a couple of strings as we can see this figure; but almost all time, the software does not give any string, as we can see here. After that the software freezes (thus, if I press enter the shell does not response).

Can you suggest a solution in order to get a fluent flow of string, and -as a consequence- read each message sent by Arduino on the serial port?

I am using Window 10 x64 as OS and the COM6 (it is an USB 2.0).

2 Answers 2

3

I found the solution and I share it in order to help people with the same problem. C# does not activate as default the RTS and the DTR serial port. Thus, adding

mySerialPort.DtrEnable = true;
mySerialPort.RtsEnable = true;

after the serial port declaration, everything works fine.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I've been pulling my hair out trying to get powershell to read an Arduino and this was why! I just needed to set these two lines in Powerhsell and it all worked!
0

This is a really good example: Serial Port Polling and Data handling

The Serial Port got an event called DataRecived, so you dont have to sleep your thread.
Something like this:

serialPort.DataReceived +=SerialPortDataReceived;


private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
    Console.WriteLine(serialPort.ReadLine());
}

Comments

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.