简体   繁体   English

Arduino与c#的通信

[英]Arduino communication with c#

i'm new to this community, and i have a big interest in programming as well as electronics.我是这个社区的新手,我对编程和电子学很感兴趣。 so I've got a problem with communication between arduino and c# windows application.所以我遇到了 arduino 和 c# windows 应用程序之间的通信问题。 I know how to automatically update data from serial port in console application, but in the form application for me it's pretty tricky.我知道如何在控制台应用程序中从串行端口自动更新数据,但在我的表单应用程序中,这非常棘手。 So.. in arduino i have a basic sketch for reading an analog signal from a potentiometer, and in ac# application i made a ComboBox for selecting a port, and a CheckBox which makes a while loop.所以..在arduino中,我有一个从电位计读取模拟信号的基本草图,在ac#应用程序中,我制作了一个用于选择端口的ComboBox和一个制作while循环的CheckBox。 In a while loop I've got commands for reading the signal and display it to a user.在 while 循环中,我获得了读取信号并将其显示给用户的命令。 And sorry for my English, i'm not very good at it.. code:对不起我的英语,我不太擅长......代码:

namespace arduinoRead { public partial class Form1 : Form { public Form1() { InitializeComponent();命名空间 arduinoRead { 公共部分类 Form1 : Form { public Form1() { InitializeComponent(); } }

    private void Form1_Load(object sender, EventArgs e)
    {

        string[] ports = SerialPort.GetPortNames();
        serialPort1.PortName = ports.ToString();
        comboBox1.Items.AddRange(ports);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string Selected1 = comboBox1.SelectedItem.ToString();
        MessageBox.Show(String.Format("Jūs esat izvēlējies: '{0}' Portu", Selected1));
        serialPort1.PortName = Selected1;
        serialPort1.Open();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2(textBox1.Text);
        f2.Show();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        while (checkBox1.Checked)
        {
            label1.Text = serialPort1.ReadLine();
        }
    }
}

} }

I think it would be better if you post your code.我认为如果您发布代码会更好。 In general, in C# form application you need to create an instance of serial port一般来说,在C#表单应用程序中需要创建一个串口实例

        SerialPort COM = new SerialPort("COM3", 115200);
        COM.Open();

Then you use COM.Write to send bytes to the port, and COM.Read to read.然后使用COM.Write将字节发送到端口,并使用COM.Read进行读取。 On the Arduino side, you use Serial.begin(115200) (make sure the speed matches), and then Serial.read and Serial.print .在Arduino的一面,您使用Serial.begin(115200)确保速度匹配),然后Serial.readSerial.print

You may also want to have a look at Windows Remote Arduino project, which allows you to control inputs/outputs of Arduino from C# program in a natural way.您可能还想查看Windows Remote Arduino项目,它允许您以自然的方式从 C# 程序控制 Arduino 的输入/输出。 You need to put Firmata sketch on your Arduino, and then Windows Remote Arduino library handles all serial communication.您需要将 Firmata 草图放在您的 Arduino 上,然后 Windows 远程 Arduino 库处理所有串行通信。 See the sample project here .请参阅此处的示例项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM