0

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?

2
  • this example from arduino playground should be able to help you to do a char handshake between arduino and C#. They are sending a request to the arduino and the arduino is replying back. Commented Nov 9, 2016 at 17:38
  • It was an nice help I will work on it @Josh Commented Nov 9, 2016 at 17:54

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.