收到从Arduino的数据发送到C#,反之亦然 [英] Recieve and send data from arduino to c# and vice versa

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

问题描述

首先,我开始使用C#昨天上午,所以我appologize潜在麻木的问题。

到目前为止,我写了下面的code。我能够从C#的数据发送到Arduino的,这似乎是工作的罚款。现在我想从Arduino的收到数据,通过serial.print,在同一时间,并在框中显示它(列表框?)。怎样才能将它添加到了code?

我想对于任何输入,技巧和想法应该心存感激。

 命名空间WindowsFormsApplication1
{
    公共部分Form1类:表格
    {        公共Form1中()
        {
            的InitializeComponent();
            stop.Enabled = FALSE;
            left.Enabled = FALSE;
            right.Enabled = FALSE;
            up.Enabled = FALSE;
            down.Enabled = FALSE;
            字符串[] =端口SerialPort.GetPortNames();
            comboBox1.Items.AddRange(端口);        }
        私人无效Form1_Load的(对象发件人,EventArgs的发送)
        {
        }
        私人无效start_Click(对象发件人,EventArgs的发送)
        {
            如果(comboBox1.SelectedIndex -1个)
            {
                串口=(字符串)comboBox1.SelectedItem;
                serialPort2.PortName =口; // 港口;
                serialPort2.BaudRate = 9600;
                serialPort2.Open();
                如果(serialPort2.IsOpen)
                {
                    start.Enabled = FALSE;
                    stop.Enabled = TRUE;
                    left.Enabled = TRUE;
                    right.Enabled = TRUE;
                    up.Enabled = TRUE;
                    down.Enabled = TRUE;
                }
            }
                其他
                {
                    的MessageBox.show(请连接的Arduino并选择一个端口);
                }        }
        私人无效stop_Click(对象发件人,EventArgs的发送)
        {
            如果(serialPort2.IsOpen)
            {
                serialPort2.Close();
                start.Enabled = TRUE;
                stop.Enabled = FALSE;
                left.Enabled = FALSE;
                right.Enabled = FALSE;
                up.Enabled = FALSE;
                down.Enabled = FALSE;
            }
        }
        私人无效up_Click(对象发件人,EventArgs的发送)
        {            如果(serialPort2.IsOpen)
            {
                serialPort2.WriteLine(1);
            }        }        私人无效left_Click(对象发件人,EventArgs的发送)
        {            如果(serialPort2.IsOpen)
            {
                serialPort2.WriteLine(4);
            }        }        私人无效right_Click(对象发件人,EventArgs的发送)
        {
            如果(serialPort2.IsOpen)
            {
                serialPort2.WriteLine(2);
            }
        }        私人无效down_Click(对象发件人,EventArgs的发送)
        {
            如果(serialPort2.IsOpen)
            {
                serialPort2.WriteLine(3);
            }
        }


解决方案

通过 SolidSoils4A​​rduino 可以同时读取写串行消息。在 Solid.Arduino.IStringProtocol 支持串消息和 Solid.Arduino.ArduinoSession 的异步读取有一个 StringReceived 事件时,将触发邮件已收到。

first of all i started using c# yesterday morning so i appologize for potential numb questions.

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 recieve 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 thankfull 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");
            }
        }

解决方案

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天全站免登陆