简体   繁体   English

从Arduino发送和接收数据

[英]Send and receive data from Arduino

So I am trying to develop a console application which is able to send and receive automatically data from an Arduino through serial port. 因此,我正在尝试开发一个控制台应用程序,该应用程序能够通过串行端口自动从Arduino发送和接收数据。 Basically this application will be executed as a task scheduler. 基本上,此应用程序将作为任务计划程序执行。

Daily, at a certain time the application execute a command to close a door. 每天,应用程序都会在特定时间执行命令以关闭门。 To do that I will send something like this comport.Write("y") and the Arduino will understand that this command will close the door but it will just be closed if the Arduino send to me an ascii command. 为此,我将发送类似comport.Write("y") ,并且Arduino将了解该命令将关闭门,但是如果Arduino向我发送ascii命令,它将被关闭。

I wanna create an if condition which tells something like it: 我想创建一个if条件,该条件会告诉它类似的内容:

"If the command sent to the arduino were read then it will response something and I will open the door. If not it will do nothing because the door is already closed." “如果读取了发送到arduino的命令,它将响应某些内容,然后我将打开门。否则,由于门已经关闭,它将无济于事。”

This is the code I already have: 这是我已经拥有的代码:

static void Main(string[] args)
    {
        BackgroundWorker work = new BackgroundWorker();
        work.DoWork += new DoWorkEventHandler(work_DoWork);
        work.ProgressChanged += new ProgressChangedEventHandler(work_ProgressChanged);
        work.WorkerReportsProgress = true;
        work.RunWorkerAsync();
        Console.ReadLine();
    }

    static void work_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        Console.WriteLine(e.ProgressPercentage);
    }

    static void work_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker work = sender as BackgroundWorker;
        int grams = -1;
        SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
        port.Open();
        while (true)
        {
            string buff = port.ReadLine();
            int value;
            if (int.TryParse(buff, out value) && value != grams)
            {
                grams = value;
                work.ReportProgress(grams);
            }
        }
    }

I wanna use something like this to read what the arduino sent to me and it will continue if the command is correct. 我想使用类似的方法来读取arduino发送给我的内容,如果命令正确,它将继续。

Instead if this int variables I want string variables 相反,如果此int变量我想要字符串变量

Could you guys help me? 你们能帮我吗?

maybe you can use solidsoils4Arduino to send and receive ASCII commands. 也许您可以使用solidsoils4Arduino发送和接收ASCII命令。 Do you have any working code already? 您已经有任何有效的代码了吗? how is the arduino detecting if the door is closed? arduino如何检测门是否关闭?

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

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