C#代码无法从Arduino读取串行输入

时间:2013-03-03 20:40:40

标签: c#-4.0 serial-port arduino

您好我正在尝试从我的Arduino读取串行输入,但我没有运气。我相信我正确地打开和关闭连接,但似乎没有取得多大成功!

我确信Arduino正在输出数据,因为我可以在串口终端中看到它。

我的C#程序的代码如下,我想知道是否有人可以发现我可能错过的任何错误。

这也是我应该收到的串行数据的一个例子“12.2,1111,332,233”

namespace FridgeProtectionDeviceMonitor
{
    public partial class Online_mode : Form
    {

        public Online_mode()
        {
            InitializeComponent();
        }

        private void Online_mode_Load(object sender, EventArgs e)
        {
            cmbPortSelection.DataSource = SerialPort.GetPortNames();
            cmbChartSelection.SelectedIndex = 0;
        }

        string x = "";
        SerialPort port;

        private void btnFindPorts_Click(object sender, EventArgs e)
        {
            var ports = SerialPort.GetPortNames();
            cmbPortSelection.DataSource = ports;
        }

        private void btnOpenPort_Click(object sender, EventArgs e)
        {
            if (cmbPortSelection.SelectedIndex > -1)
            {
                port = new SerialPort(cmbPortSelection.Text);
                try
                {
                    if (!port.IsOpen)
                    {
                        port.BaudRate = 9600;
                        port.Open();
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Serial connection request denied: Port is in use!");
                }
            }
            else
            {
                MessageBox.Show("Serial connection request denied: No port selected!");
            }
        }

        private void btnClosePort_Click(object sender, EventArgs ex)
        {

            try
            {
                port.Close();

            }
            catch (Exception)
            {
                MessageBox.Show("Serial close connection request denied: ", ex.ToString());
            }
        }

        private void update(object sender, EventArgs e)
        {
            txtSaveLocation.Text = x;
        }

        private void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            x = port.ReadLine().ToString();
            MessageBox.Show(x);
            this.Invoke(new EventHandler(update));

        }
    }
}

3 个答案:

答案 0 :(得分:0)

我正在和你做同样的事情,如果你想将它用于你的目的,你将不得不修改它,但这里是我使用的代码。唯一的问题是,对我来说,我需要它非常快速地阅读,因为它适用于速度计而且有点滞后(任何人都知道为什么?)但无论如何这里是我的代码对我有用。

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                if (arduinoCom.IsOpen == false)
                {
                    arduinoCom.Open();
                }
            }

            catch
            {
                MessageBox.Show("Serial Error: Is your serial plugged in?");
            }

        }

        private void refresh_Tick(object sender, EventArgs e)
        {            
            string speedReading = arduinoCom.ReadLine();
            speed.Text = speedReading;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            arduinoCom.Close();
        }

    }
}

答案 1 :(得分:0)

我有一个USB转串口转换器连接到一个链接到PIC微控制器的XBee无线网桥,我发现在一个单独的线程中运行串口读取功能会增加慢速通信速度!

readline阻塞代码,直到收到换行符,因此阻塞了主线程。在我将它移动到另一个线程之前,我的GUI变得无法响应等。 顺便说一句,我正在使用ReadByte()。

我不断地从后台工作程序中的串行缓冲区读取字节并将它们存储在线程安全队列中。然后检查队列中的通信开始位并从那里分析数据包。使用手动重置事件我正在同步所有内容。

根据我的经验,port_datareceived事件是对serialport类的一个很好的补充,但不幸的是它对于快速通信来说相对无用。我的波特率是12900。

如果你想要代码片段,我可以提供它们。

此致 皮特

P.S。我正在使用ATS5004D 4通道差分示波器和多通道软件。多通道软件可以通过内置的串行分析器模块将电压信号分析成串行数据。通过这种方式,您可以看到串行通信线路上实际讨论的内容!切断所有BS其他串行终端添加......

答案 2 :(得分:0)

@michael b我首先看看实际通信正在做什么,即确定TX RX线的范围,看看arduino板正在推出什么。我对arduino只有很少的经验,但是它的前身PICAXE系统有一个调试功能,可以减慢所有的速度,这是怎么回事?

您想要更新speedo每秒多少次? SAE J1939汽车串行通信标准规定RPM只需每100ms更新一次。人眼通常看不到/反应的速度超过300毫秒。 9600波特每毫秒为您提供一个8位字节

就个人而言,我不会使用计时器来获取更新,而是在与main的单独线程上执行所有操作,并让它以所需的速度/速度运行。

此主题大致向您展示了我如何设置通讯System.IO.IOException: A device attached to the system is not functioning C# .NET 4.0

希望它有所帮助, 皮特

相关问题