gpt4 book ai didi

与 Arduino 的 C# 串行通信

转载 作者:行者123 更新时间:2023-11-30 18:34:47 26 4
gpt4 key购买 nike

我正在从事一个项目,该项目涉及我的客户端软件通过串行通信将数据发送到 Arduino 微 Controller AtMega32U4。到目前为止,我已经浏览了许多已回答的问题,但没有一个是针对我的问题的。但是,我相信我的问题可能仅限于线程问题或 Arduino 自动重置问题。

代码 1:

public MainForm()
{
InitializeComponent();
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
serialPort1.DtrEnable = true;
//serialPort1.RtsEnable = true;
}
private void button3_Click(object sender, EventArgs e)
{
// Disables button while processing
button3.Enabled = false;

GetDir dir = new App.GetDir();
dir.getCoords(Origin.Text, Destination.Text, Application.StartupPath + @"\temp2.html", "temp2.xml");
dataBrowser.Navigate(Application.StartupPath + @"\temp2.html");
dataBrowser.Update();

waypoints = dir.coordsLat.Length;
counter = dir.coordsLat.Length;
coords = new double[dir.coordsLat.Length, 2];

for (int i = 0; i < counter; i++)
{
coords[i, 0] = (Convert.ToDouble(dir.coordsLat[i]));
coords[i, 1] = (Convert.ToDouble(dir.coordsLon[i]));
}

//serialPort1.Close();
//System.Threading.Thread.Sleep(1000);


if (serialPort1.IsOpen && !doubleClick)
{
serialPort1.Close();
System.Threading.Thread.Sleep(2000);
try
{
serialPort1.Open();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Cannot open serial port");
}
System.Threading.Thread.Sleep(2000);
}
else
{
if (!serialPort1.IsOpen)
{
try
{
serialPort1.Open();
doubleClick = true;
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Cannot open serial port");
}
System.Threading.Thread.Sleep(2000);
serialPort1.Write("^");
System.Threading.Thread.Sleep(1000);
Console.WriteLine('^');
//button3.Enabled = true;
}
}
}

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//System.Threading.Thread.Sleep(1000);
readData = serialPort1.ReadLine();
Console.WriteLine(readData);
// If microcontroller sends "&", it is ready to receive next piece of data
if (readData == "&")
{
sendRequest = true;
}
else
{
sendRequest = false;
}

// Write next piece of data to microcontroller if it is ready
if (sendRequest)
{
this.BeginInvoke( new EventHandler (write_serialPort1));
}
}

在代码 1 的调试过程中,事件处理程序 (serialPort1_DataReceived) 永远不会被调用。在此过程中,由于控制台输出“^”两次,button3_click 以某种方式被调用了两次。之后,客户端因为没有收到任何东西而停止。请记住,Arduino 将在收到抑扬符 ('^') 后以与号 ('&') 响应。 Arduino 代码已经在 Arduino IDE 上进行了测试,看起来运行良好。我相信 button3_click 被调用两次的问题来自 button3_down 和 button3_up。

但是,我能够通过代码 2 绕过这个问题。但也遇到了另一堵墙。

代码 2:

 private void button3_Click(object sender, EventArgs e)
{
// Disables button while processing
button3.Enabled = false;

GetDir dir = new App.GetDir();
dir.getCoords(Origin.Text, Destination.Text, Application.StartupPath + @"\temp2.html", "temp2.xml");
dataBrowser.Navigate(Application.StartupPath + @"\temp2.html");
dataBrowser.Update();

waypoints = dir.coordsLat.Length;
counter = dir.coordsLat.Length;
coords = new double[dir.coordsLat.Length, 2];

for (int i = 0; i < counter; i++)
{
coords[i, 0] = (Convert.ToDouble(dir.coordsLat[i]));
coords[i, 1] = (Convert.ToDouble(dir.coordsLon[i]));
}

serialPort1.Close();

try
{
serialPort1.Open();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Cannot open serial port");
}

if (serialPort1.IsOpen)
{
System.Threading.Thread.Sleep(2000);
using (serialPort1)
{
serialPort1.Write("^");
System.Threading.Thread.Sleep(1000);
Console.WriteLine("^");
serialPort1.Close();
System.Threading.Thread.Sleep(5000);
}
}
else
{
button3.Enabled = true;
}

}

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//SerialPort sp = (SerialPort)sender;

System.Threading.Thread.Sleep(10000);
/*if (!serialPort1.IsOpen)
{
serialPort1.Close();
System.Threading.Thread.Sleep(10000);
serialPort1.Open();
System.Threading.Thread.Sleep(10000);
}*/
//serialPort1.Open();
//using (sp)
using (serialPort1)
{
serialPort1.Open();
System.Threading.Thread.Sleep(5000);
readData = serialPort1.ReadExisting();
Console.WriteLine(readData);
// If microcontroller sends "&", it is ready to receive next piece of data
if (readData == "&")
{
sendRequest = true;
}
else
{
sendRequest = false;
}

// Write next piece of data to microcontroller if it is ready
if (sendRequest)
{
this.BeginInvoke(new EventHandler(write_serialPort1));
}
}
}

在代码 2 中,事件处理程序确实被调用并且 button3_click 仅运行一次。但是当它尝试打开端口时,它会返回错误“Access to Port X denied”。此外,我希望我不必像这样关闭和打开端口,但是当调用事件处理程序时(在较早的代码中)它返回了 COM 端口未打开的错误。为了解决这个错误,我不得不在 button3_click 和事件处理期间关闭它并重新打开它。

在阅读了很多处理串行通信线程问题的问题后,我在代码中添加了很多延迟。我什至尝试过延迟一分钟,希望线程结束以解决问题。但是,那里没有运气。

我还在 MainForm 设计器中指定了我的串行端口,而不是在代码中声明它(起初我同时做了这两个并意识到这是多余的)。我不确定这是否会导致问题,但我已经看到使用这两种方法的示例。

最后,它绝对可以在每次建立串行连接(例如打开和关闭端口)时处理 Arduino 自动重置。综上所述,似乎是通过串口发送数据,但无法读取串口传入的数据。

感谢您阅读本文,如果有人能为我指出正确的方向,我们将不胜感激。

编辑#1:即使在代码 1 中使用了 BeginInvoke 之后,它仍然会死锁,因为从未调用过事件处理程序。

编辑#2:根据新手的建议对代码 1 进行编辑。

编辑 #3: 添加了主窗体初始化并将代码 1 更新为当前状态。

编辑 #4: 删除(注释掉)事件处理程序中的 sleep 。我在事件处理程序期间正在 sleep ,因此我无法收到微 Controller 发送给我的任何信息。代码现在可以正常工作了。

最佳答案

Make sure you are using COM1, if you do not COM1 serial port, change through Computer -> Device Manager -> Ports (COM & LPT) -> Select the COM to be changed -> Port Settings -> Advanced -> ComPort Number -> select COM1.

Make sure that you have installed jumper / connect with a screwdriver between pin2 and pin3 of COM1.

Add button1 and textBox1 to Form and run this program

using System;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Text;

namespace WindowsFormsApplication1 {
public partial class Form1 : Form {

const int MAX_BUFFER = 100;

int i = 0;
byte[] DataReceived = new byte[MAX_BUFFER];
SerialPort serialPort = new SerialPort();

public Form1() {
InitializeComponent();
serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
}

void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) {
// wait data ready
Thread.Sleep(500);

// while data ready in buffer
while (serialPort.IsOpen && serialPort.BytesToRead > 0) {
// read data serial
DataReceived[i] = Convert.ToByte(serialPort.ReadByte());

// counter data
i++;

// reset conter if more then maxvalue
if (i >= MAX_BUFFER) {
i = 0;
}
}

if (i == 1 && DataReceived[0] == ASCIIEncoding.ASCII.GetBytes("^")[0]) {
this.textBox1.Invoke(new Action(() => {
this.textBox1.Text = ASCIIEncoding.ASCII.GetString(DataReceived, 0, 1);

}));
}

}

public void InitSerialPort() {
serialPort.PortName = "COM1";
serialPort.BaudRate = 9600;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.ReceivedBytesThreshold = 1;
}

private void Form1_Load(object sender, EventArgs e) {
// initialize serial port
InitSerialPort();

// assure port is closed before open it
if (serialPort != null && serialPort.IsOpen) {
serialPort.Close();
}
serialPort.Open();
}

private void button1_Click(object sender, EventArgs e) {
if (serialPort.IsOpen) {
serialPort.Write("^");
// wait data sent
Thread.Sleep(500);
}
}
}
}

关于与 Arduino 的 C# 串行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15781840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com