首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >SerialDataReceivedEventHandler不工作c#

SerialDataReceivedEventHandler不工作c#
EN

Stack Overflow用户
提问于 2018-06-10 07:36:11
回答 1查看 1.4K关注 0票数 0

我正在尝试用windows窗体中的C#程序连接到arduino。我可以将数据从c#发送到arduino,但我希望将数据从arduino发送到C#程序。

我已经尝试过SerialDataReceivedEventHandler,但在构建表单后无法获得数据...

我能做什么?

代码语言:javascript
复制
       public Form1()
    {
        InitializeComponent();
        Init();

    }//end form 1

    private void Init()
    {
        try
        {
            arduinoPort = new SerialPort();
            arduinoPort.BaudRate = 9600;
            arduinoPort.PortName = "COM4";
            arduinoPort.Handshake = Handshake.None;
            arduinoPort.RtsEnable = true;//request to send true
            arduinoPort.DtrEnable = true;//arduino can send messages to the c# program
            arduinoPort.DataReceived += new SerialDataReceivedEventHandler(GetFromArduino);               
            arduinoPort.Open();
        }//end try

        catch (Exception ex) { MessageBox.Show(ex.Message); }

    }//end init


    private void GetFromArduino(object sender, SerialDataReceivedEventArgs e)
    {
        //string arduinoInputString = arduinoPort.ReadLine();
        //Invoke(new Action(() => label1.Text = arduinoInputString));
        MessageBox.Show("does it work?");
    }//end get from arduino
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-10 18:00:28

代码语言:javascript
复制
I don't know if it matters, but this what i did and it solved:
1. in the setup function of the arduino program i had a loop that ran on all the pins, now i specified it to the pins i really use.
2. i don't open the port in the form.cs, but in the program.cs and run a while loop that just check if the port is open. then, it gets the data without disturbing the form to run... 

**in the Program.cs:**


  public static SerialPort arduinoPort { get; set; }
    public static string arduinoInputString { get; set; }
    [STAThread]

    static void Main()
    {
        arduinoPort = new SerialPort();
        try
        {
            arduinoPort = new SerialPort();
            arduinoPort.BaudRate = 250000;
            arduinoPort.PortName = "COM4";
            //arduinoPort.Handshake = Handshake.None;
            //arduinoPort.RtsEnable = true;//request to send true
            //arduinoPort.DtrEnable = true;//arduino can send messages to the c# program
            ////arduinoPort.DataReceived += new SerialDataReceivedEventHandler(GetFromArduino);
            arduinoPort.DataReceived += DataReceivedHandler;
            arduinoPort.Open();
        }//end try
        catch (Exception ex) { Console.WriteLine((ex.Message)); }

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());

        while (arduinoPort.IsOpen)//read data if the port is open
        {

        }



    }//end main

    static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort comm = (SerialPort)sender;
        arduinoInputString = comm.ReadExisting();
        Form1.label1.Text = arduinoInputString;//label to show the input string from arduino
    }//end get data from arduino
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50779414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档