基于c#de串口通讯问题 2C

我想实时获取从串口里面传输出来的字符串数据,可是为什么老是显示不出来呢,求高手解答,本人渣渣,谢谢了

 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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string data;
        Boolean status = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM3";
            serialPort1.BaudRate = 57600;
            serialPort1.Encoding = Encoding.BigEndianUnicode;
            serialPort1.Open();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            Close();
        }
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            data = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(DisplayText));
        }

        private void DisplayText(object sender, EventArgs e)
        {
            char[] data1 = data.ToCharArray();
            textBox1.Text = data1.ToString();

        }


    }
}

1个回答

觉得这个是异步的问题,DataReceived的时候给data赋值,DisplayText里取的时候DataReceived会不会触发了,这个时候serialPort1.ReadExisting()为空,所以DisplayText显示不了,纯粹猜测,实际调一下看看吧, DataReceived加个断点看看有数据不

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问