2

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

如何在 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();
}
4

1 回答 1

0

这是我写了 6 年的帮助类,可能会有所帮助。顺便说一句-您无法从 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; }
        }
    }
}
于 2013-04-23T02:21:58.560 回答