简体   繁体   中英

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. 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. In a while loop I've got commands for reading the signal and display it to a user. And sorry for my English, i'm not very good at it.. code:

namespace arduinoRead { public partial class 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

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

Then you use COM.Write to send bytes to the port, and COM.Read to read. On the Arduino side, you use Serial.begin(115200) (make sure the speed matches), and then Serial.read and Serial.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. You need to put Firmata sketch on your Arduino, and then Windows Remote Arduino library handles all serial communication. See the sample project here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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