简体   繁体   English

C#与arduino的串行通讯

[英]C# Serial communcation with arduino

I'm trying to send a string from my arduino(leonardo) to a C# program. 我试图将字符串从arduino(leonardo)发送到C#程序。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;

namespace ConsoleApplication2
{
class Program
{

    static void Main(string[] args)
    {
        SerialPort mySerialPort = new SerialPort("COM7");

        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;


        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);

        mySerialPort.Open();

        Console.WriteLine("Press any key to continue...");
        Console.WriteLine();
        Console.ReadKey();
        mySerialPort.Close();
    }

    static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        Console.WriteLine("Data Received:");
        Console.Write(indata);
    }
}
}

This is my code wich I copied from the msdn example to try and understand what it does. 这是我从msdn示例中复制的代码,以尝试了解它的作用。 My arduino code below just sends hello world over te com port with a delay of 1000. 我下面的arduino代码只是通过com端口发送hello世界,延迟为1000。

void setup ()
{

 Serial.begin(9600);

 }


void loop(){

 Serial.println("Hello World");
 delay(1000);
}

My arduino is using the COM7 like I defined in the C# program. 我的arduino正在使用C7程序中定义的COM7。 When I run bot programs, The C# program never comes within the datareceived event handler. 当我运行机器人程序时,C#程序永远不会进入数据接收事件处理程序。 So no data is received. 因此没有收到任何数据。 I really want tis to work :) 我真的想让tis工作:)

Kind regards 亲切的问候

I Switched the code to a windows form application, it still was not working. 我将代码切换到Windows窗体应用程序,但仍无法正常工作。 Then i found a topic about serial communication with C# about arduino leonardo here 然后,我发现了一个关于串行通信话题C#关于Arduino的莱昂纳多这里

I had to do this: 我必须这样做:

        serial.DtrEnable = true;
        serial.RtsEnable = true;

I consider my problem as solved. 我认为我的问题已经解决。

Console applications do not have a message loop so naturally they don't respond to events. 控制台应用程序没有消息循环,因此自然不会响应事件。 You have one thread and it will be stuck blocking at Console.ReadKey() . 您只有一个线程,它将被阻塞在Console.ReadKey() Either use synchronous reads from the serial port or, if you wish to stick to an event-based model, move this code to a windows-based applications. 可以使用从串行端口进行的同步读取,或者,如果您希望使用基于事件的模型,请将此代码移至基于Windows的应用程序。

For a synchronous example, see this MSDN example : 有关同步示例, 请参见以下MSDN示例

while (_continue)
{
    try
    {
        string message = _serialPort.ReadLine();
        Console.WriteLine(message);
    }
    catch (TimeoutException) { }
}

The above is only an excerpt - the full example demonstrates setting up the timeout values, etc. 以上仅是摘录-完整示例演示了设置超时值等。

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

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