日期:2014-05-17  浏览次数:20897 次

c#串口通信,如何让接受到的数据显示在textbox中

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

namespace 串口收发
{
    public partial class Form1 : Form
    {
        public string receiveing = string.Empty;
        public string transmission = string.Empty;

        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            
            transmission = txtTransmission.Text;
            serialPort1.WriteLine(transmission);
            MessageBox.Show("数据发送成功!", "系统提示");
        }

        private void serialPort2_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            receiveing = serialPort2.ReadLine();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            txtReceive.Text = receiveing;
            MessageBox.Show("数据接收成功!", "系统提示");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort2.Open();
        }
    }
}

------解决方案--------------------
用代码设置一下波特率试试,我用物理设备接到电脑上,发现得用代码来设置波特率才行,如果直接从属性选项那里去设置,会出现莫名其妙的问题,导致接收不到数据
------解决方案--------------------

using Sy