c#的串口控件操作

来源:互联网 发布:大学生笔记本必备软件 编辑:程序博客网 时间:2024/06/05 09:01

定时发送和接收串口的数据:

        private void timer1_Tick(object sender, EventArgs e)

        {

            byte[] buf = new byte[100];

            byte[] Recbuf = new byte[100];

            int b, i;

            ASCIIEncoding encoding = new ASCIIEncoding();

 

            serialPort1.Open();

            serialPort1.Write(textBox2.Text);

            Thread.Sleep(500);  //同步慢速从设备

            b = serialPort1.Read(buf, 0, 90);

 

            for (i = 1; i < 100; i++)

            {

                Recbuf[i - 1] = buf[i];

            }

            textBox3.Text += (encoding.GetString(Recbuf) );

            textBox3.Text += "/r/n";

            serialPort1.Close();

        }

原创粉丝点击