如何在asp C#中的同一程序中通过串口读取和发送数据

时间:2013-04-23 00:34:02

标签: c# serial-port arduino

我已经将数据从C#发送到Arduino,并且它的工作非常完美;但是当我尝试通过event handler从传感器接收C#的数据时,它必须保持端口打开以读取任何接收的数据;所以我不能再通过串口发送数据了。

如何在C#和Arduino之间同时发送和接收数据

这是我接收数据的代码:

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();
}

1 个答案:

答案 0 :(得分:0)

这是我写了6年的助手课,可能有所帮助。 BTW - 您无法从ASP读取COM端口:)

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; }
        }
    }
}