【问题标题】:How to read byte array from serial port in C# [duplicate]如何在C#中从串口读取字节数组[重复]
【发布时间】:2014-08-20 08:11:55
【问题描述】:

C#串口编程有问题
我的目标是将 ByteArray 发送到串行端口,而不是作为 ByteArray。 但我无法从串口获取。我试过了:

string gelen = port.ReadExisting();
   int asd = port.ReadByte();
   string qwe = port.ReadLine();

谁能告诉我怎么了? 谢谢

我的代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO.Ports;

    namespace serial_port_app
    {

        public partial class Form1 : Form
        {

            public Form1()
            {       
                InitializeComponent();

            }

            private void button1_Click(object sender, EventArgs e)
            {

                int text1, text2,text3,text4,text5, text6;

                Int32.TryParse(textBox1.Text, out text1);
                byte byteValue1 = Convert.ToByte(text1);

                Int32.TryParse(textBox2.Text, out text2);
                byte byteValue2 = Convert.ToByte(text2);

                Int32.TryParse(textBox3.Text, out text3);
                byte byteValue3 = Convert.ToByte(text3);

                Int32.TryParse(textBox4.Text, out text4);
                byte byteValue4 = Convert.ToByte(text4);

                Int32.TryParse(textBox5.Text, out text5);
                byte byteValue5 = Convert.ToByte(text5);

                Int32.TryParse(textBox6.Text, out text6);
                byte byteValue6 = Convert.ToByte(text6);


                byte[] byteArray = new byte[6];

                byteArray[0]=byteValue1;
                byteArray[1]=byteValue2;
                byteArray[2]=byteValue3;
                byteArray[3]=byteValue4;
                byteArray[4]=byteValue5;
                byteArray[5]=byteValue6;


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

                port.Open();
                port.Write(byteArray,0,6);

>>>>>>>>>>>>>   string text = port.ReadExisting();
>>>>>>>>>>>>>   int asd = port.ReadByte();
>>>>>>>>>>>>>   string qwe = port.ReadLine();

                int n=dataGridView1.Rows.Add();

                    dataGridView1.Rows[n].Cells[1].Value = text;
                    dataGridView1.Rows[n].Cells[2].Value = asd;
                    dataGridView1.Rows[n].Cells[3].Value = qwe;
                    dataGridView1.Rows[n].Cells[4].Value = text4;
                    dataGridView1.Rows[n].Cells[5].Value = text5;
                    dataGridView1.Rows[n].Cells[6].Value = text6;



                    if ((n % 2) == 0)
                        dataGridView1.Rows[n].DefaultCellStyle.BackColor = Color.LightBlue;
                    else
                        dataGridView1.Rows[n].DefaultCellStyle.BackColor = Color.LightGreen;

                    port.Close();
            }

编辑正确答案:

删除:

string text = port.ReadExisting();
int asd = port.ReadByte();
string qwe = port.ReadLine();

并添加:

port.DtrEnable = true;
 port.RtsEnable = true;

 int bytes = port.BytesToRead;
 byte[] buffer = new byte[bytes];
 port.Read(buffer, 0, bytes);

【问题讨论】:

  • 当你尝试那些你尝试过的东西时发生了什么?
  • 您忽略了处理握手信号的需要。将 DtrEnabled 和 RtsEnabled 属性设置为 true。使用 Hyperterminal 或 Putty 等其他程序来获得对电气连接和驱动程序的信心。
  • @HansPassant 谢谢,程序运行正常,您的回答正确。

标签: c# serial-port bytearray


【解决方案1】:

要获取Byte 数组而不是String,请使用BytesToReadRead(Byte[], Int32, Int32)

【讨论】:

  • 很遗憾,没用。
  • 我添加的是 DtrEnabled 和 RtsEnabled 属性对我的程序为 true 并且有效。谢谢你的回答。
  • @hamdi 太好了。您无需将标题编辑为“已解决”。您需要单击灰色勾号以将答案标记为已接受。如果汉斯有答案,我会说接受他的,但如果他不在,我会接受!
  • @HamdiBayhan 看起来对你有用,你能接受吗?
猜你喜欢
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-23
  • 2010-09-22
  • 2011-08-05
  • 2015-09-18
  • 2013-10-08
相关资源
最近更新 更多