Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read byte array from serial port in C# [duplicate]

I have a problem with C# serial port programming
my aim send ByteArray to serial port and than get as ByteArray. but I can not get from serial port. I tried:

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

Could any one tell me what’s wrong? Thanks

my code:

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

EDIT for correct answer:

delete:

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

and add:

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

 int bytes = port.BytesToRead;
 byte[] buffer = new byte[bytes];
 port.Read(buffer, 0, bytes);
like image 277
Hamdi Bayhan Avatar asked Oct 21 '22 03:10

Hamdi Bayhan


1 Answers

To get a Byte array rather than a String use BytesToRead and Read(Byte[], Int32, Int32)

like image 191
weston Avatar answered Oct 22 '22 16:10

weston