问题描述
我正在尝试制作一个应用程序来读取来自 Arduino 的传出信号,但我无法让它在 C# 中工作 Windows 窗体,仅在控制台中.我的 C# Windows 窗体代码有错吗?我调试时没有出现任何错误,但这并不意味着我没有忘记某些东西.
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.
这是我的代码:
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)
{
}
}
}
}
推荐答案
默认情况下,ReadLine 方法将阻塞,直到收到一行.你的 Arduino 程序正在发送一行吗?您在运行程序时是否关闭了 Arduino 串行监视器程序?
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?
在您确认收到字符之前,我会更改为 port.ReadChar.
I would change to port.ReadChar until you verify that you are receiving characters.
这篇关于C# 读取 Arduino的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!