从arduino接收和发送数据到c#,反之亦然 [英] Receive and send data from arduino to c# and vice versa

查看:37
本文介绍了从arduino接收和发送数据到c#,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我昨天早上开始使用 c#,所以对于潜在的麻木问题,我深表歉意.

first of all I started using c# yesterday morning so I apologize for potential numb questions.

到目前为止,我编写了以下代码.我能够将数据从 c# 发送到 arduino,这似乎工作正常.现在我想同时通过serial.print从arduino接收数据并将其显示在一个框中(ListBox?).如何将其添加到代码中?

So far I wrote the following code. I'm able to send data from c# to arduino which seems to be working fine. Now i would like to receive data from arduino, through serial.print, at the same time and display it in a box (ListBox?). How could add this to the code?

如果您提供任何意见、提示和想法,我将不胜感激.

I would be thankful for any input, tips and ideas.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            stop.Enabled = false;
            left.Enabled = false;
            right.Enabled = false;
            up.Enabled = false;
            down.Enabled = false;
            string[] ports = SerialPort.GetPortNames();
            comboBox1.Items.AddRange(ports);

        }
        private void Form1_Load(object sender, EventArgs e)
        {


        }
        private void start_Click(object sender, EventArgs e)
        {


            if (comboBox1.SelectedIndex > -1)
            {
                string port = (string)comboBox1.SelectedItem;
                serialPort2.PortName = port; // "port";
                serialPort2.BaudRate = 9600;
                serialPort2.Open();
                if (serialPort2.IsOpen)
                {
                    start.Enabled = false;
                    stop.Enabled = true;
                    left.Enabled = true;
                    right.Enabled = true;
                    up.Enabled = true;
                    down.Enabled = true;
                }
            }
                else
                {
                    MessageBox.Show("Please connect the Arduino and select a Port");
                }

        }
        private void stop_Click(object sender, EventArgs e)
        {
            if (serialPort2.IsOpen)
            {
                serialPort2.Close();
                start.Enabled = true;
                stop.Enabled = false;
                left.Enabled = false;
                right.Enabled = false;
                up.Enabled = false;
                down.Enabled = false;
            }
        }
        private void up_Click(object sender, EventArgs e)
        {

            if (serialPort2.IsOpen)
            {
                serialPort2.WriteLine("1");
            }

        }

        private void left_Click(object sender, EventArgs e)
        {

            if (serialPort2.IsOpen)
            {
                serialPort2.WriteLine("4");
            }

        }

        private void right_Click(object sender, EventArgs e)
        {
            if (serialPort2.IsOpen)
            {
                serialPort2.WriteLine("2");
            }
        }

        private void down_Click(object sender, EventArgs e)
        {
            if (serialPort2.IsOpen)
            {
                serialPort2.WriteLine("3");
            }
        }

推荐答案

使用 SolidSoils4Arduino 您可以同时阅读并编写串行消息.Solid.Arduino.IStringProtocol 支持异步读取字符串消息,Solid.Arduino.ArduinoSession 有一个 StringReceived 事件,当消息有已收到.

With SolidSoils4Arduino you can simultaneously read and write serial messages. The Solid.Arduino.IStringProtocol supports asynchronous reading of string messages and the Solid.Arduino.ArduinoSession has a StringReceived event that fires when a message has been received.

这篇关于从arduino接收和发送数据到c#,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆