gpt4 book ai didi

c# - 通过 Serial 从 Arduino 读取字符串

转载 作者:太空宇宙 更新时间:2023-11-03 22:56:08 27 4
gpt4 key购买 nike

我有一个 Arduino 连接到我制作的接口(interface)。一切正常,但 Arduino 正在向接口(interface)发送一个 string。该程序正在读取数据,并将其存储在一个变量中。

我遇到的问题是变量存储数据,但当新数据从 Arduino 传入时不会更新它。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
public SerialPort myport;

int irisvalue;
string myString;
string s = "";

//String s2;

public Form1()
{
InitializeComponent();
//Load += new EventHandler(Form1_Load);
connectbtn.Text = "Connect";
disconnect.Text = "Disconnect";
this.connectbtn.Click += new EventHandler(connectbtn_Click);
this.disconnect.Click += new EventHandler(disconnect_Click);
this.iris1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.iris1_MouseDown);
this.iris1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.iris1_MouseUp);
this.iris2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.iris2_MouseDown);
this.iris2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.iris2_MouseUp);
this.focus1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.focus1_MouseDown);
this.focus1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.focus1_MouseUp);
this.focus2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.focus2_MouseDown);
this.focus2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.focus2_MouseUp);

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


void connect()
{

myport = new SerialPort();
myport.BaudRate = 9600;
myport.PortName = "COM3";
myport.Open();

}

void read()
{
s = myport.ReadLine();
//Form1_Load();

}

void discon()
{


myport.Close();

}

private void disconnect_Click(object sender, System.EventArgs e)
{
discon();
if (myport.IsOpen)
{

}
else
{

connectbtn.Text = "Connect";
disconnect.BackColor = default(Color);
connectbtn.BackColor = default(Color);
}
}

private void connectbtn_Click(object sender, System.EventArgs e)
{
connect();

if (myport.IsOpen)
{
connectbtn.Text = "Connected";
connectbtn.BackColor = Color.Green;
//Load += new EventHandler(Form1_Load);
Form1_Load();
disconnect.BackColor = Color.Red;
disconnect.Text = "Disconnect";
read();
//s = myport.ReadLine();

}
else
{
connectbtn.Text = "Error";
connectbtn.BackColor = Color.Red;
}


}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void iris1_MouseDown(object sender, MouseEventArgs e)
{
//Console.WriteLine("Hello");

irisvalue = 1;
myString = irisvalue.ToString();
Form1_Load();

}

private void iris1_MouseUp(object sender, MouseEventArgs e)
{

irisvalue = 0;
myString = irisvalue.ToString();
Form1_Load();
}



private void iris2_MouseDown(object sender, MouseEventArgs e)
{
irisvalue = 2;
myString = irisvalue.ToString();
Form1_Load();

}


private void iris2_MouseUp(object sender, MouseEventArgs e)
{
irisvalue = 0;
myString = irisvalue.ToString();
Form1_Load();

}

private void focus1_MouseDown(object sender, MouseEventArgs e)
{
irisvalue = 3;
myString = irisvalue.ToString();
Form1_Load();

}


private void focus1_MouseUp(object sender, MouseEventArgs e)
{
irisvalue = 0;
myString = irisvalue.ToString();
Form1_Load();


}

private void focus2_MouseDown(object sender, MouseEventArgs e)
{
irisvalue = 4;
myString = irisvalue.ToString();
Form1_Load();

}

private void focus2_MouseUp(object sender, MouseEventArgs e)
{

irisvalue = 0;
myString = irisvalue.ToString();
Form1_Load();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void Form1_Load()
{




textBox1.Text = s;



Console.WriteLine(s);


myport.WriteLine(myString);




}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


}

}

最佳答案

要接收更新,您应该订阅串行端口事件。试试这个代码:

myport.DataReceived += (sender, e) =>
{
if (e.EventType == SerialData.Chars)
s = myport.ReadLine();
};

关于c# - 通过 Serial 从 Arduino 读取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45297962/

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