简体   繁体   English

Arduino和C#串口“崩溃”

[英]Arduino and C# Serial Port “Crash”

I am trying to create a communication between an Arduino Leonardo and C#. 我正在尝试在Arduino Leonardo和C#之间建立沟通。

Just now, the Arduino's software sends a simple message (in loop) on the serial port: 刚才,Arduino的软件在串口发送一条简单的消息(循环):

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: C#尝试只读取这些消息并在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. 这个C#软件的输出取决于。 If I use the serial monitor on the Arduino IDE, then I receive the string's stream correctly (one each 500ms). 如果我在Arduino IDE上使用串行监视器,那么我会正确接收字符串的流(每500ms一个)。 Otherwise, the C# software freezes. 否则,C#软件冻结。 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). 之后,软件冻结(因此,如果我按下输入,shell不响应)。

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? 你能建议一个解决方案,以获得流畅的字符串流,并且 - 结果 - 读取Arduino在串口上发送的每条消息?

I am using Window 10 x64 as OS and the COM6 (it is an USB 2.0). 我使用Window 10 x64作为操作系统和COM6(它是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 . C#不会默认激活RTS和DTR串行端口 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. 串口有一个名为DataRecived的事件,所以你不必睡觉你的线程。
Something like this: 像这样的东西:

serialPort.DataReceived +=SerialPortDataReceived;


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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