C#读的Arduino [英] C# read Arduino

查看:170
本文介绍了C#读的Arduino的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个应用程序,从Arduino的读取传出的信号,但我不能让它在C#中的 Windows窗体,仅在控制台中。是我的C#Windows窗体code错了吗?当我调试我没有得到任何错误,但它并不意味着我没有忘记什么。

下面是我的code:

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;
使用System.IO.Ports;
使用的System.Threading;命名空间CommunicateWithArduino
{
    公共部分Form1类:表格
    {
        公共静态System.IO.Ports.SerialPort端口;        委托无效SetTextCallback(字符串文本);        私人BackgroundWorker的刻苦的;        私人主题readThread = NULL;
        公共Form1中()
        {
            的InitializeComponent();            刻苦的=新的BackgroundWorker();
            sendBtn.Enabled = FALSE;
        }        私人无效btnConnect_Click(对象发件人,EventArgs的发送)
        {
            System.ComponentModel.IContainer成分=
                新System.ComponentModel.Container();
            端口=新System.IO.Ports.SerialPort(组件);
            port.PortName = comPort.SelectedItem.ToString();
            port.BaudRate = Int32.Parse(baudRate.SelectedItem.ToString());
            port.DtrEnable =真;
            port.ReadTimeout = 5000;
            port.WriteTimeout = 500;
            port.Open();            readThread =新主题(新的ThreadStart(this.Read));
            readThread.Start();
            this.hardWorker.RunWorkerAsync();            btnConnect.Text =下;连接>中;            btnConnect.Enabled = FALSE;
            comPort.Enabled = FALSE;
            sendBtn.Enabled = TRUE;
        }        私人无效Form1_Load的(对象发件人,EventArgs的发送)
        {
            的foreach(在SerialPort.GetPortNames字符串s())
            {
                comPort.Items.Add(多个);
            }
            如果(comPort.Items.Count大于0)
                comPort.SelectedIndex = comPort.Items.Count-1;
            其他
                comPort.SelectedIndex = 0;            baudRate.Items.Add(2400);
            baudRate.Items.Add(4800);
            baudRate.Items.Add(9600);
            baudRate.Items.Add(14400);
            baudRate.Items.Add(19200);
            baudRate.Items.Add(28800);
            baudRate.Items.Add(38400);
            baudRate.Items.Add(57600);
            baudRate.Items.Add(115200);            baudRate.SelectedIndex = 2;
        }        私人无效sendBtn_Click(对象发件人,EventArgs的发送)
        {
            如果(port.IsOpen)
            {
                port.Write(sendText.Text);
            }
        }        私人无效的setText(字符串文本)
        {            如果(this.receiveText.InvokeRequired)
            {
                SetTextCallback D =新SetTextCallback(的setText);
                this.Invoke(四,新的对象[] {文本});
            }
            其他
            {
                this.receiveText.Text + =文本;
                this.receiveText.Text + =文本;
                this.receiveText.Text + = Environment.NewLine;
            }
        }        公共无效阅读()
        {
            而(port.IsOpen)
            {
                尝试
                {
                    如果(port.BytesToRead大于0)
                    {
                        字符串消息= port.ReadLine();
                        this.SetText(消息);
                    }
                }
                赶上(TimeoutException异常){}
            }
        }        Form1_FormClosed私人无效(对象发件人,FormClosedEventArgs E)
        {
            尝试
            {
                如果(!(readThread == NULL))
                    readThread.Abort();
            }
            赶上(的NullReferenceException)
            {
            }            尝试
            {
                port.Close();
            }
            赶上(的NullReferenceException)
            {
            }
        }
    }
}


解决方案

默认情况下,ReadLine方法将阻塞,直到接收到一条线。是您的Arduino的程序发送一条线?你有没有关闭,同时运行程序Arduino的串行监控程序?

我想改变port.ReadChar直到您验证您收到的字符。

I'm trying to make a app that read the outgoing signals from Arduino, but I can't make it work in C# Windows Forms, only in the console. Is my C# Windows Forms code wrong? I don't get any errors when I debug, but it doesn't mean that I haven't forgot something.

Here is my code:

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;
using System.IO.Ports;
using System.Threading;

namespace CommunicateWithArduino
{
    public partial class Form1 : Form
    {
        public static System.IO.Ports.SerialPort port;

        delegate void SetTextCallback(string text);

        private BackgroundWorker hardWorker;

        private Thread readThread = null;


        public Form1()
        {
            InitializeComponent();

            hardWorker = new BackgroundWorker();
            sendBtn.Enabled = false;
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            System.ComponentModel.IContainer components =
                new System.ComponentModel.Container();
            port = new System.IO.Ports.SerialPort(components);
            port.PortName = comPort.SelectedItem.ToString();
            port.BaudRate = Int32.Parse(baudRate.SelectedItem.ToString());
            port.DtrEnable = true;
            port.ReadTimeout = 5000;
            port.WriteTimeout = 500;
            port.Open();

            readThread = new Thread(new ThreadStart(this.Read));
            readThread.Start();
            this.hardWorker.RunWorkerAsync();

            btnConnect.Text = "<Connected>";

            btnConnect.Enabled = false;
            comPort.Enabled = false;
            sendBtn.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (string s in SerialPort.GetPortNames())
            {
                comPort.Items.Add(s);
            }
            if (comPort.Items.Count > 0)
                comPort.SelectedIndex = comPort.Items.Count-1;
            else
                comPort.SelectedIndex = 0;

            baudRate.Items.Add("2400");
            baudRate.Items.Add("4800");
            baudRate.Items.Add("9600");
            baudRate.Items.Add("14400");
            baudRate.Items.Add("19200");
            baudRate.Items.Add("28800");
            baudRate.Items.Add("38400");
            baudRate.Items.Add("57600");
            baudRate.Items.Add("115200");

            baudRate.SelectedIndex = 2;
        }

        private void sendBtn_Click(object sender, EventArgs e)
        {
            if (port.IsOpen)
            {
                port.Write(sendText.Text);
            }
        }

        private void SetText(string text)
        {

            if (this.receiveText.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.receiveText.Text += "Text: ";
                this.receiveText.Text += text;
                this.receiveText.Text += Environment.NewLine;
            }
        }

        public void Read()
        {
            while (port.IsOpen)
            {
                try
                {
                    if (port.BytesToRead > 0)
                    {
                        string message = port.ReadLine();
                        this.SetText(message);
                    }
                }
                catch (TimeoutException) { }
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if(!(readThread == null))
                    readThread.Abort();
            }
            catch (NullReferenceException)
            {
            }

            try
            {
                port.Close();
            }
            catch (NullReferenceException)
            {
            }
        }
    }
}

解决方案

By default, the ReadLine method will block until a line is received. Is your Arduino program sending a line? Did you close the Arduino serial monitor program while running your program?

I would change to port.ReadChar until you verify that you are receiving characters.

这篇关于C#读的Arduino的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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