7 Ekim 2012 Pazar

how to communicate with the Arduino via a computer and using the C # language


I'll use this post to give an idea of how to communicate with the Arduino via a computer and using the C # language. The aim is to do something with this:

It should say that I'm no expert in C #, on the contrary, so if you are a knowledgeable and find something that aches obscene, gives a discount! (And who communicates well all learned)

First install the free version of Microsoft Visual C # Express (Microsoft also has good things!)

The main idea of ​​the program is to send information via the serial port to which is connected the Arduino. Strictly speaking, the Arduino is connected to the computer via USB but as it has a USB-Serial converter, USB abstracted us and speak only Serial Link.
In order to simplify the "protocol" of communication information transmitted consists only of characters. A character corresponds to a particular action. We describe here the simplest example is that turn on and off an LED.

Let then the code!

Open Microsoft Visual C # Express and does File> New> Project tab and choose Visual C # "Windows Forms Application".
You'll need a TextBox, a Button, two Labels, two ComboBoxes and a SerialPort. Drag each compotentes for Form1 is created so that the offspring project. To add code to each component just click twice on top of each other.

I will list here only the two pieces of code that I consider most important. The code is available in its entirety at the end of this post.

So you can communicate via serial port you have to define at least two things: a data transfer rate (BAUD RATE) and port that the Arduino is connected (eg COM7). If you do not know which port is connected to the Arduino clicks with the right mouse button on "My Computer", "Properties" and "Device Manager." There, you will find this information.

To definires the baud rate using the following code: 
  private void baudrate_combobox_SelectedIndexChanged (object sender, EventArgs e) 
  { 
  serialPort.BaudRate = Convert.ToInt16 (baudrate_combobox.Text); 
  } 
The value chosen for the baud rate must match that used by the Arduino. 

To definires the serial port as well as efectuares its opening, use the following code: 
  private void port_combobox_SelectedIndexChanged (object sender, EventArgs e) 
  { 
  serialPort.PortName = port_combobox.Text; 

  try 
  { 
  SerialPort.Open (); 
  } 
  catch (Exception ex) 
  { 
  MessageBox.Show (ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); 
  } 
  } 

The functions try / catch prevent the program from any exception that might occur when opening the door Serial. 

Finally, to send a message to the Arduino uses the following code: 
  private void send_button_Click (object sender, EventArgs e) 
  { 
  try 
  { 
  SerialPort.Write (message_textbox.Text); 
  message_textbox.Text = ""; 
  } 
  catch (Exception ex) 
  { 
  MessageBox.Show (ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); 
  } 
  } 
In turn you will need to send code to the Arduino so that it sits waiting and acknowledge commands (characters in this case) that you send him. Just upload the following code: 
  # Define BAUD_RATE 9600 
  # Define ledPin 13 

  incomingChar char; 

  void setup () 
  { 
  pinMode (ledPin, OUTPUT); 

  / / Serial communication 
  Serial.begin (BAUD_RATE); 
  } 

  void loop () 
  { 
  if (Serial.available ()> 0) { 

  incomingChar Serial.read = (); 

  / / Your protocol goes here 
  switch (incomingChar) { 
  case '1 ': 
  / / LED is ON 
  digitalWrite (ledPin, HIGH); 
  break; 
  case '0 ': 
  / / LED is OFF 
  digitalWrite (ledPin, LOW); 
  break; 
  } 
  } 
What does this code mean? It means that if you send a character '1 'LED connected on pin 13 is lit and that if you send a character '0' LED is off. 

Hiç yorum yok: