简体   繁体   中英

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

I wanna create an if condition which tells something like it:

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

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.

Instead if this int variables I want string variables

Could you guys help me?

maybe you can use solidsoils4Arduino to send and receive ASCII commands. Do you have any working code already? how is the arduino detecting if the door is closed?

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