2

I am already send data from C# to Arduino, and it's work perfect; but when I try to receive data on C# from sensors by event handler, it must keep port open to read any received data; so I can't send data via serial port any more.

How can I send and receive data at the same time between C# and Arduino?

This is my code to receive data:

public static SerialPort port=
    new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);

protected void DataReceived(object sender, SerialDataReceivedEventArgs e) {
    SerialPort sp=(SerialPort)sender;
    indata=sp.ReadExisting();
    Functions f=new Functions();

    if(indata!="") {
        if(indata.Equals("bed_light_on\r\n")) {
            f.update_flag("bed_light", 1);
        }
    }
}

protected void Page_Load(object sender, EventArgs e) {
    indata="";
    port.Close();
    port.DataReceived+=new SerialDataReceivedEventHandler(DataReceived);

    if(!port.IsOpen)
        port.Open();
}
4
  • 1
    Are you trying to do this from an ASP.NET application? Commented Apr 23, 2013 at 0:36
  • check that asking code stackoverflow.com/questions/12663116/c-sharp-read-arduino Commented Apr 23, 2013 at 0:46
  • You either need to use separate threads for reading and writing or use asynchronous operations. If this really is ASP then you have the added issue that the application may start and stop unpredictably. Commented Apr 23, 2013 at 0:47
  • A sanity check is indicated here. ASP code runs on a webserver. Typically in data center or server room. Behind a locked door, no Arduinos allowed inside. Commented Apr 23, 2013 at 1:02

1 Answer 1

0

Here's a helper class I wrote 6 years that may help. BTW - you can't read COM ports from ASP :)

using System;
using System.Text;
using System.IO.Ports;

namespace MyNamespace
{
    public class COMSerialPort : IDisposable
    {
        private SerialPort FSerialPort;
        public Boolean Disposed { get; private set; }

        //---------------------------------------------------------------------
        public COMSerialPort(String portName, Int32 baudRate, Encoding encode)
        {
            FSerialPort = new SerialPort(portName, baudRate, Parity.None, 8, StopBits.One);

            FSerialPort.NewLine = "\r";
            FSerialPort.Encoding = encode;
            Disposed = false;
        }
        //---------------------------------------------------------------------
        public COMSerialPort(String portName, Int32 baudRate) : this( portName, baudRate, Encoding.ASCII )
        {
        }
        //---------------------------------------------------------------------
        ~COMSerialPort()
        {
            Dispose(false);
        }
        //---------------------------------------------------------------------
        protected void Dispose(Boolean bDisposing)
        {
            lock (this)
            {
                if (!Disposed)
                {
                    Disposed = true;
                    GC.SuppressFinalize(this);

                    if (bDisposing)
                    {
                        if (FSerialPort != null)
                        {
                            Close();

                            FSerialPort.Dispose();
                            FSerialPort = null;
                        }
                    }
                }
            }
        }
        //---------------------------------------------------------------------
        public void Dispose()
        {
            Dispose(true);
        }
        //---------------------------------------------------------------------
        public void Open()
        {
            if (!FSerialPort.IsOpen) FSerialPort.Open();
        }
        //---------------------------------------------------------------------
        public void Close()
        {
            if (FSerialPort.IsOpen) FSerialPort.Close();
        }
        //---------------------------------------------------------------------
        public void WriteLine(String data)
        {
            if (FSerialPort.IsOpen) FSerialPort.WriteLine(data);
        }
        //---------------------------------------------------------------------
        public void Write(String data)
        {
            if (FSerialPort.IsOpen) FSerialPort.Write(data);
        }
        //---------------------------------------------------------------------
        public void Write(Byte[] data)
        {
            if (FSerialPort.IsOpen) FSerialPort.Write(data, 0, data.Length);
        }
        //---------------------------------------------------------------------
        public String ReadLine()
        {
            String rValue;
            if (FSerialPort.IsOpen)
                rValue = FSerialPort.ReadLine();
            else
                rValue = null;

            return rValue;
        }
        //---------------------------------------------------------------------
        public String Read(Int32 count)
        {
            String rValue = null;
            if (FSerialPort.IsOpen)
            {
                Char[] buffer = new Char[count];
                FSerialPort.Read(buffer, 0, count);

                StringBuilder sb = new StringBuilder();
                sb.Append(buffer);

                rValue = sb.ToString();
            }

            return rValue;
        }
        //---------------------------------------------------------------------
        public Byte[] ReadBytes(Int32 count)
        {
            Byte[] rValue = null;
            if (FSerialPort.IsOpen)
            {
                rValue = new Byte[count];
                FSerialPort.Read(rValue, 0, count);
            }

            return rValue;
        }
        //---------------------------------------------------------------------
        public Char ReadChar()
        {
            return (Char)FSerialPort.ReadChar();
        }
        //---------------------------------------------------------------------
        public Int32 ReadByte()
        {
            return FSerialPort.ReadByte();
        }
        //---------------------------------------------------------------------
        public SerialPort NativeObject
        {
            get { return FSerialPort; }
        }
    }
}
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.