对于我的小项目,我试图向 Arduino 发送小消息,并根据消息关闭和打开它的输出端口。 我已经在 C# 中尝试过它工作正常,但我想在 C++ 控制台应用程序中进行。 我的 C++ 知识不好,我搜索了它,但没有找到可以理解的解决方案。
我只需要在 C++ 控制台应用程序中通过串行端口从我的 Arduino 发送和检索消息。
这是我的工作 C# 代码。
using System.IO.Ports;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SerialPort port = new SerialPort("COM8", 9600);
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
port.Open();
port.Write("1");
port.Close();
}
private void button1_Click(object sender, EventArgs e)
{
port.Open();
String s = port.ReadLine();
port.Close();
}
}
}
最佳答案
Arduino 官方网站有一个很棒的example如何在 Windows 中连接 Arduino 和 C++。 我认为,如果您在项目中包含所描述的 SerialClass.h( header )、Serial.cpp(源代码文件)并改编示例 main.cpp 文件,这可能是适合您的解决方案。
关于c++ - 通过 USB 串行端口(C++ 控制台应用程序)在 Arduino 和 PC 之间进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25736659/