向串口发送字符

发布于 2024-09-06 23:52:30 字数 138 浏览 3 评论 0原文

我想将字符“a”发送到串行端口。

我尝试过:

serialPort1.WriteLine("a");

但它实际上并没有将字符“a”发送到我的主板。

有什么想法吗?

I would like to send the character 'a' to the serial port.

I tried:

serialPort1.WriteLine("a");

but it's not actually sending the character 'a' to my board.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆光飞翔i 2024-09-13 23:52:30

我与您分享我在一个富有成效的项目中创建的课程。处理串行连接和读写缓冲区:

   public class CommSERIAL : IComm
    {

        #region Events

        public event EventHandler EvtOnConnect;
        public event EventHandler EvtOnDisconnect;


        public event EventHandler<OnDataReceivedEventArgs> EvtOnDataReceived;

        #endregion

        System.IO.Ports.SerialPort Rs232 = new System.IO.Ports.SerialPort();


        private string m_Port;

        public string Port
        {
            get { return m_Port; }
            set { m_Port = value; }
        }
        private int m_Baud;

        public int Baud
        {
            get { return m_Baud; }
            set { m_Baud = value; }
        }

        private int m_DataBit;

        public int DataBit
        {
            get { return m_DataBit; }
            set { m_DataBit = value; }
        }

        private System.IO.Ports.StopBits m_stopBits;

        public System.IO.Ports.StopBits StopBits
        {
            get { return m_stopBits; }
            set { m_stopBits = value; }
        }

        private System.IO.Ports.Parity m_parity;

        public System.IO.Ports.Parity Parity
        {
            get { return m_parity; }
            set { m_parity = value; }
        }


        public void Connect()
        {
            if (!(Rs232.IsOpen))
            {
                Rs232.PortName = this.Port;
                Rs232.BaudRate = this.Baud; ;
                Rs232.DataBits = this.DataBit;
                Rs232.StopBits = this.StopBits;
                Rs232.Parity = this.Parity;
                Rs232.ReadTimeout = 5000;
                Rs232.Handshake = System.IO.Ports.Handshake.None;
                Rs232.ReadTimeout = 1000;
                Rs232.WriteTimeout = 500;               

                Rs232.Open();


            }
        }

        public void Disconnect()
        {
            Rs232.Close();
            if (EvtOnDisconnect != null)
                EvtOnDisconnect(new object(), new System.EventArgs());

            m_Connected = false;
        }


        public CommSERIAL()
        {
            this.ConnType = ConnType.Direct;

        }


        public bool Connected
        {
            get
            {
                return m_Connected;

            }
        }


        private ConnType m_ConnType;

        public ConnType ConnType
        {
            get { return m_ConnType; }
            set { m_ConnType = value; }
        }

        public string ReadOnByte(int Lenght,char EndChar)
        {
            char[] bytes = new char[Lenght];
            string ret = "";
            int numBytesRead = 0;
            while (bytes[numBytesRead] != EndChar && numBytesRead <= bytes.Length)
            {
                while (Rs232.Read(bytes, numBytesRead, 1) == 1 && bytes[numBytesRead] != EndChar && numBytesRead <= bytes.Length)
                {
                    numBytesRead++;
                }
            }

            foreach (char b in bytes)
                ret += b.ToString();
            return ret.Substring(0,numBytesRead);


        }

        public String ReadBuffer()
        {

            try
            {
                if (Rs232.IsOpen)
                {

                    Byte[] readBuffer = new Byte[Rs232.ReadBufferSize + 1];

                    try
                    {
                        // If there are bytes available on the serial port,
                        // Read returns up to "count" bytes, but will not block (wait)
                        // for the remaining bytes. If there are no bytes available
                        // on the serial port, Read will block until at least one byte
                        // is available on the port, up until the ReadTimeout milliseconds
                        // have elapsed, at which time a TimeoutException will be thrown.
                        Int32 count = Rs232.Read(readBuffer, 0, Rs232.ReadBufferSize);

                        String SerialIn = System.Text.Encoding.ASCII.GetString(readBuffer, 0, count);

                        return SerialIn;

                    }
                    catch (TimeoutException) { return ""; }

                }
                else
                {
                    Thread.Sleep(50);
                    return "";
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
        public string Read()
        {
            return Rs232.ReadExisting();
        }

        public string ReadAll()
        {

            //TO DO
            return "";
        }

        public void Write(string Data)
        {
            if (Rs232 == null)
                throw new Exception("Must be connected before Write");

            if (!Rs232.IsOpen)
                throw new Exception("Must be opened before Write");

            Rs232.Write(Data);
        }

        public void ClearBuffer()
        {
            Rs232.DiscardInBuffer();
        }


        private bool m_Connected;


        #region IComm Members


        private int m_Id;

        public int Id
        {
            get
            {
                return m_Id;
            }

            set
            {
                m_Id = value;
            }
        }

        #endregion

        public override string ToString()
        {
            return this.ConnType.ToString();
        }

        #region IComm Members



        #endregion
    }

公共接口 IComm
{
连接类型 连接类型
{
得到;
放;

    bool Connected
    {
        get;
    }

    int Id
    {
        get;
        set;
    }


    void Connect();
    void Disconnect();
    void Write(string Data);
    string Read();
    string ReadOnByte(int Lenght, char EndChar);
    String ReadBuffer();
    void ClearBuffer();
    string ReadAll();
    event EventHandler EvtOnConnect;
    event EventHandler EvtOnDisconnect;
    event EventHandler<EventArgs.OnDataReceivedEventArgs> EvtOnDataReceived;
}

希望这对你有用

I share to you a class created by me in a productive project. This Handles the serial connection and reading and writing buffer:

   public class CommSERIAL : IComm
    {

        #region Events

        public event EventHandler EvtOnConnect;
        public event EventHandler EvtOnDisconnect;


        public event EventHandler<OnDataReceivedEventArgs> EvtOnDataReceived;

        #endregion

        System.IO.Ports.SerialPort Rs232 = new System.IO.Ports.SerialPort();


        private string m_Port;

        public string Port
        {
            get { return m_Port; }
            set { m_Port = value; }
        }
        private int m_Baud;

        public int Baud
        {
            get { return m_Baud; }
            set { m_Baud = value; }
        }

        private int m_DataBit;

        public int DataBit
        {
            get { return m_DataBit; }
            set { m_DataBit = value; }
        }

        private System.IO.Ports.StopBits m_stopBits;

        public System.IO.Ports.StopBits StopBits
        {
            get { return m_stopBits; }
            set { m_stopBits = value; }
        }

        private System.IO.Ports.Parity m_parity;

        public System.IO.Ports.Parity Parity
        {
            get { return m_parity; }
            set { m_parity = value; }
        }


        public void Connect()
        {
            if (!(Rs232.IsOpen))
            {
                Rs232.PortName = this.Port;
                Rs232.BaudRate = this.Baud; ;
                Rs232.DataBits = this.DataBit;
                Rs232.StopBits = this.StopBits;
                Rs232.Parity = this.Parity;
                Rs232.ReadTimeout = 5000;
                Rs232.Handshake = System.IO.Ports.Handshake.None;
                Rs232.ReadTimeout = 1000;
                Rs232.WriteTimeout = 500;               

                Rs232.Open();


            }
        }

        public void Disconnect()
        {
            Rs232.Close();
            if (EvtOnDisconnect != null)
                EvtOnDisconnect(new object(), new System.EventArgs());

            m_Connected = false;
        }


        public CommSERIAL()
        {
            this.ConnType = ConnType.Direct;

        }


        public bool Connected
        {
            get
            {
                return m_Connected;

            }
        }


        private ConnType m_ConnType;

        public ConnType ConnType
        {
            get { return m_ConnType; }
            set { m_ConnType = value; }
        }

        public string ReadOnByte(int Lenght,char EndChar)
        {
            char[] bytes = new char[Lenght];
            string ret = "";
            int numBytesRead = 0;
            while (bytes[numBytesRead] != EndChar && numBytesRead <= bytes.Length)
            {
                while (Rs232.Read(bytes, numBytesRead, 1) == 1 && bytes[numBytesRead] != EndChar && numBytesRead <= bytes.Length)
                {
                    numBytesRead++;
                }
            }

            foreach (char b in bytes)
                ret += b.ToString();
            return ret.Substring(0,numBytesRead);


        }

        public String ReadBuffer()
        {

            try
            {
                if (Rs232.IsOpen)
                {

                    Byte[] readBuffer = new Byte[Rs232.ReadBufferSize + 1];

                    try
                    {
                        // If there are bytes available on the serial port,
                        // Read returns up to "count" bytes, but will not block (wait)
                        // for the remaining bytes. If there are no bytes available
                        // on the serial port, Read will block until at least one byte
                        // is available on the port, up until the ReadTimeout milliseconds
                        // have elapsed, at which time a TimeoutException will be thrown.
                        Int32 count = Rs232.Read(readBuffer, 0, Rs232.ReadBufferSize);

                        String SerialIn = System.Text.Encoding.ASCII.GetString(readBuffer, 0, count);

                        return SerialIn;

                    }
                    catch (TimeoutException) { return ""; }

                }
                else
                {
                    Thread.Sleep(50);
                    return "";
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
        public string Read()
        {
            return Rs232.ReadExisting();
        }

        public string ReadAll()
        {

            //TO DO
            return "";
        }

        public void Write(string Data)
        {
            if (Rs232 == null)
                throw new Exception("Must be connected before Write");

            if (!Rs232.IsOpen)
                throw new Exception("Must be opened before Write");

            Rs232.Write(Data);
        }

        public void ClearBuffer()
        {
            Rs232.DiscardInBuffer();
        }


        private bool m_Connected;


        #region IComm Members


        private int m_Id;

        public int Id
        {
            get
            {
                return m_Id;
            }

            set
            {
                m_Id = value;
            }
        }

        #endregion

        public override string ToString()
        {
            return this.ConnType.ToString();
        }

        #region IComm Members



        #endregion
    }

public interface IComm
{
ConnType ConnType
{
get;
set;
}

    bool Connected
    {
        get;
    }

    int Id
    {
        get;
        set;
    }


    void Connect();
    void Disconnect();
    void Write(string Data);
    string Read();
    string ReadOnByte(int Lenght, char EndChar);
    String ReadBuffer();
    void ClearBuffer();
    string ReadAll();
    event EventHandler EvtOnConnect;
    event EventHandler EvtOnDisconnect;
    event EventHandler<EventArgs.OnDataReceivedEventArgs> EvtOnDataReceived;
}

I hope that works for you

筱果果 2024-09-13 23:52:30

你怎么知道它没有发送角色?波特率/字长/奇偶校验设置是否有可能关闭,并且您的主板无法识别该字符?

另外,为什么是 C#?对于硬件 IO 来说,这似乎是一个奇怪的选择。

How do you know that it isn't sending the character? Is it possible that the baud/word length/parity setting are off and your board is just not recognizing the character?

Also, why c#? Just seems an odd choice for hardware IO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文