【问题标题】:C#, need to receive data from microcontrollerC#,需要从微控制器接收数据
【发布时间】:2017-06-15 05:06:56
【问题描述】:

尝试使用 DataReceived 和处理程序事件从 mk 接收数据,我所做的是 - 按下程序上的按钮(代码如下)然后 mk 上的 LED 将打开,然后数据应发送回程序(期望 1,字节值,但也尝试字符串值,不起作用)。发送方工作,但接收....不是 好像我错过了一些东西。任何帮助欣赏它。感谢进一步

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 WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


        }
        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) // As i understood, here we configure where i data will be shown,
                                                                                       // trying to get it on TextBox1
        {

            SerialPort sp = (SerialPort)sender;
            richTextBox1.Text += sp.ReadExisting() + "\n";
        }

        private void button1_Click(object sender, EventArgs e)                                      // There are a main actions, first i receive data then send data by a click.    
        {
            serialPort1.Write("\u0001");
            serialPort1.Close();

            System.ComponentModel.IContainer components = new System.ComponentModel.Container();  //  
            serialPort1 = new System.IO.Ports.SerialPort(components);
            serialPort1.PortName = "COM4";
            serialPort1.BaudRate = 9600;
            serialPort1.DtrEnable = true;
            serialPort1.Open();
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);


        }
    }
}

【问题讨论】:

  • 你需要在你的写作之前连接你的听众。看起来该引脚已打开,但没有任何内容在收听信息。当你开始监听端口时,什么都没有发生。
  • 我建议您使用串口模拟器来调试问题,这样您就可以准确了解发生了什么并确保有传入通信。
  • 感谢您的 cmets,正如我所说,我的设备正在工作,我可以发送数据,并且我还使用控制台检查了接收数据(也使用 c#),并且我收到了从 mk 发送的数据。因此,我上面的代码中存在问题。任何有助于纠正我的代码的帮助,请欣赏它,
  • 为什么要先写关闭再创建端口?你不应该先创建端口吗?然后读写?
  • 是的,我改变了它,分配给按钮点击。但结果是一样的

标签: c# serial-port microcontroller uart


【解决方案1】:

串行端口与 UI 位于不同的线程上。所以当你收到一个角色时,因为你没有调用 UI,你会得到一个异常并且 UI 没有更新。 首先在 DataReceivedHandler 中调用 UI。你可以这样做:

public static class ControlExt
{
    public static void InvokeChecked(this Control control, Action method)
    {
        try
        {
            if (control.InvokeRequired)
            {
                control.Invoke(method);
            }
            else
            {
                method();
            }
        }
        catch { }
    }
}


public partial class Form1 : Form
{
    private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        this.InvokeChecked(delegate
        {
            richTextBox1.Text += serialPort1.ReadExisting() + "\n";

            richTextBox1.SelectionStart = Text.Length;
            richTextBox1.ScrollToCaret();
        });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多