0

嗨,我正在尝试从我的 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));

        }
    }
}
4

3 回答 3

0

我有一个连接到连接到 PIC 微控制器的 XBee 无线网桥的 USB 到串行转换器,我发现在单独的线程中运行串行端口读取功能会提高通信速度,这是一种长期存在的问题!

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

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

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

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

问候,皮特

PS 我正在使用带有多通道软件的 ATS5004D 4 通道差分示波器。多通道软件可以通过其内置的串行分析模块将电压信号分析成串行数据。这样你就可以看到串行通信线路上实际讨论的内容!!删除所有 BS 其他串行终端添加...

于 2013-09-10T23:19:15.827 回答
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();
        }

    }
}
于 2013-03-06T03:40:36.507 回答
0

@michael b我首先查看实际通信正在做什么,即范围TX RX线以查看arduino板正在输出什么。我对 arduino 的经验很少,但它的前身 PICAXE 系统有一个调试功能,可以减慢一切堆的速度,这样的事情正在发生吗?

你想每秒更新几次speedo?SAE J1939 汽车串行通信标准规定,RPM 只需每 100ms 更新一次。人眼通常不能看到/反应快于 300 毫秒。9600 波特每毫秒大约为您提供一个 8 位字节。

就我个人而言,我不会使用计时器来获取更新,而是在与主线程不同的线程上做所有事情,并让它运行得尽可能快/慢。

该线程向您大致展示了我如何设置通信System.IO.IOException: A device attach to the system is not working C# .NET 4.0

希望它有帮助,皮特

于 2013-09-11T06:08:30.777 回答