简体   繁体   中英

Arduino and C# Serial Port “Crash”

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).

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.

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM