Pages: [1]   Go Down
Author Topic: .Net Form to interface with Arduino  (Read 3154 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey Guys,
I have an arduino decilima connected to a motor through an h-bridge.

I want to use .net (preferably c#) to tell it when to start/stop.  I have the motor and h-bridge stuff all figured out but I was wondering if theres a way to somehow interface them so I can have a .net form button tell the motor when to start stop?

Basically I need to send messages from .net to usb and then picking them up on the board.

Thanks guys,
Poohshoes
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There sure is!  I'm not sure how familiar you are with all the plumbing, but what you'll want to do is connect the Arduino to your computer using the serial-over-usb cable, then send commands to the Arduino using the C# SerialPort interface.  Here's the basic outline:

1. In your C# form object, add a member of type System.IO.Ports.SerialPort:

using System.IO.Ports;
...
SerialPort port;


2. In your form's constructor, create and open the serial port.  (Make sure you use the correct port name.)
port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
port.Open();


3. In your button handlers, transmit a code, preferably a single character, to Arduino
//On button "go"
port.Write("G");
//On button "stop"
port.Write("S");


4. In your Arduino setup() function, set up for Serial communications
void setup()
{
...
  Serial.begin(9600);
...
}


5. In your Arduino loop(), handle incoming characters
void loop()
{
...
  if (Serial.available())
  {
    int c = Serial.read();
    if (c == 'G') { // turn on motor }
    else if (c == 'S') { // turn off motor }
  }
...
}


Seem reasonable?

Mikal
« Last Edit: September 24, 2008, 10:44:00 am by mikalhart » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you so much mikalhart.  Thats exactly what I am looking for, mmm spoon feeding.  The only thing I am a little confused about is the Port.  Are the ports different for each physical USB connection?  Is there a way to cycle through all the ports to discover which one the arduino is connected to?  Or at least ask your computer (via C#) what the available ports are?

Thanks
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Pooh, I found this in the docs:

string[] ports = SerialPort.GetPortNames();

This returns an array of strings.  On my computer it returns {"COM1", "COM2", "COM3"}

I don't know how to automatically determine which one is the USB serial port, but you can manually check Device Manager.  In my experience, it's usually the last one, in this case COM3.

Mikal
« Last Edit: September 25, 2008, 02:09:59 pm by mikalhart » Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You know, if you wanted, you could get a list of all the available serial ports, open them one at a time, and then send a quick "Arduino, are you there?" command like:

Computer: "?"
Arduino: "!"

If you read back a !, you'd be pretty sure you had the Arduino at the other end of the line.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thats a good idea, I will do that or maybe have a drop down menu of all the available ports and let the user choose.  Thanks for all your help,  If I get it working I'll post it for others to use.
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have one of those Microsoft USB GPS devices, and Microsoft's own Streets and Trips seems to follow a hybrid strategy when offering to let the user select the COM port.  They provide a menu of all the available ports for you to choose, but also offer to probe for which one it thinks is the "real" GPS device.  Just FYI...

Mikal
« Last Edit: September 25, 2008, 11:15:26 pm by mikalhart » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So this is what I ended up with if anybody needs this in the future.  I have a windows form, this is the code in the constructor to fill a combo box with a list of ports.
Code:
string[] portlist = SerialPort.GetPortNames();

foreach (String s in portlist)
{
     portComboBox.Items.Add(s);
}

and then I have the below function that gets called when you press the connect button:
Code:
private void connectButton_Click(object sender, EventArgs e)
{
    if (port != null)
    {
        port.Close();
        port.Dispose();
    }
    port = new SerialPort(portComboBox.SelectedItem.ToString(), 9600, Parity.None, 8, StopBits.One);
    port.Open();
}

and finally when you press a key the following happens:
Code:
port.Write(e.KeyCode.ToString());

and the arduno code to controll two motors using an h-bridge is as follows:
Code:
int motor1Pin = 3;    // H-bridge leg 1
int motor2Pin = 2;    // H-bridge leg 2

int motor3Pin = 4;    // H-bridge leg 3
int motor4Pin = 5;    // H-bridge leg 4


void setup() {

  // set all the other pins you're using as outputs:
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  
  pinMode(motor3Pin, OUTPUT);
  pinMode(motor4Pin, OUTPUT);
  
  //set the inital motor states to stopped
  digitalWrite(motor1Pin, HIGH);  
  digitalWrite(motor2Pin, HIGH);
  
  digitalWrite(motor3Pin, HIGH);
  digitalWrite(motor4Pin, HIGH);

  //the serial connection allows you to read commands sent through usb (from my windows form in my case)
  Serial.begin(9600);
}

void loop() {

 if (Serial.available())
 {
   int c = Serial.read();
   switch (c)
   {
     case 'Q': //for some reason lower case characters get converted to uppdercase characters at some point in time
       digitalWrite(motor1Pin, HIGH);  //rotate forward
       digitalWrite(motor2Pin, LOW);
       break;
     case 'A':
       digitalWrite(motor1Pin, HIGH);   //stop
       digitalWrite(motor2Pin, HIGH);
       break;
     case 'Z':
       digitalWrite(motor1Pin, LOW);   //rotate backwards
       digitalWrite(motor2Pin, HIGH);
       break;
     case 'W':
       digitalWrite(motor3Pin, HIGH);   //rotate forwards
       digitalWrite(motor4Pin, LOW);
       break;
     case 'S':
       digitalWrite(motor3Pin, HIGH);   //stop
       digitalWrite(motor4Pin, HIGH);
       break;
     case 'X':
       digitalWrite(motor3Pin, LOW);   //rotate backwards
       digitalWrite(motor4Pin, HIGH);
       break;
   }
 }

}

Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice, Pooh!  Congratulations on getting it all to work.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

port = new SerialPort(portComboBox.SelectedItem.ToString(), 9600, Parity.None, 8, StopBits.One);

Is the first argument where we specify the port number Arduino is using?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49302
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is the first argument where we specify the port number Arduino is using?
It is where you specify the port name that the Arduino is connected to. Different operating systems have different formats for the names. On Windows, the Arduino might be connected to COM4 (commonly referred to as port 4)., so, the 1st argument would be "COM4". Names are different on Macs and Linux boxes.
Logged

Pages: [1]   Go Up
Jump to: