0

I would like to establish a communication between my C# program and my Arduino Duemilanove. I tried lot of different tutorials, but none of them were the same, and I'm a little bit confused.

Actually my code is:

C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InterfaceArduinoWindowsFOrm {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      serialPort1.PortName = "COM3";
      serialPort1.BaudRate = 9600;
      serialPort1.Open();
    }

    private void pictureBox1_Click(object sender, EventArgs e) {
    }

    private void tabPage2_Click(object sender, EventArgs e) {
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
    }

    private void toolTip1_Popup(object sender, PopupEventArgs e) {
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
      Console.WriteLine("test");
    }

    private void button1_Click_1(object sender, EventArgs e) {
      serialPort1.Write("5");
      Console.WriteLine(serialPort1.IsOpen);
    }
  }
}

Arduino:

/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.
  modified 8 Nov 2013
  by Scott Fitzgerald
  http://arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // Create servo object to control a servo.
// Twelve servo objects can be created on most boards.
int pos = 0;
int message = 0; // This will hold one byte of the serial message
                 // variable to store the servo position.

void setup() {
  Serial.begin(9600);
  myservo.attach(9);  // Attaches the servo on pin 9 to the servo object
}

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

    // Check to see if there is a new message
    message = Serial.read();

    // Put the serial input into the message
    Serial.print(message);

    if (message == '5') {
      Serial.print("test");
      for(pos = 0; pos <= 180; pos += 1) {
        // Goes from 0 degrees to 180 degrees
        // in steps of 1 degree

        myservo.write(pos);
        // Tell servo to go to position in variable 'pos'

        delay(15);
        // Waits 15 ms for the servo to reach the position
      }

      for(pos = 180; pos>=0; pos-=1) {
        // Goes from 180 degrees to 0 degrees

        myservo.write(pos);
        // Tell servo to go to position in variable 'pos'

        delay(15);
        // Waits 15 ms for the servo to reach the position
      }
    }
  }
}

The problem is, when I click my button, nothing happens, but obviously if I remove the if statement, the servo motor code works.

10
  • Did you make sure that the C# button event is fired, and that the Serial connection is established properly? Also, what happens if you open the Arduino COM terminal and press 5?
    – Omer
    Commented Dec 2, 2014 at 6:22
  • Hum yea , i tested with some console writte and Button work , what do you mean by : press 5?
    – War-sloop
    Commented Dec 2, 2014 at 6:24
  • I mean that, in your C# code, you just send "5" to the serial port. As part of the debugging - pin pointing where the problem lays - did you use the Arduino Serial console to send "5" the same way you send it from C#, and made sure it works there? Because if sending "5" from the Serial console in the Arduino IDE doesn't work, the C# way won't work either.
    – Omer
    Commented Dec 2, 2014 at 7:36
  • I follow lot of tutoriel covering this , the code is really similar as mine , but , when I try to use the serial monitor , and I try to fire button event , it say that Com3 is already in use and unauthorized ... 5 is only a message id to call the arduino IF statement ....
    – War-sloop
    Commented Dec 2, 2014 at 8:01
  • Yes, I get that 5 is the trigger. What I'm trying to understand is whether that trigger works or not. You cannot use Arduino IDE Serial Monitor while running your C# code since the Arduino COM port can interface only one serial port at a time. Try using the Arduino IDE Serial Monitor after closing the C# serial port and not in parallel to it.
    – Omer
    Commented Dec 2, 2014 at 8:50

1 Answer 1

1

Since you are sending from the Arduino the received serial data back to the C# application (Serial.print(message);), I would advise to check what you receive on the C# side. Register to the DataReceived event of the SerialPort as so:

serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

And add an event handler:

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort port = (SerialPort)sender;
    string data= port.ReadExisting();
    // check here what data you received, if any
}

This way you could confirm that the Arduino gets the data properly from the C# side to begin with. Next step would be to understand if this if statement treats the data properly:

if (message == '5')

But starting from examination of what you get from the C# side will help you decide where exactly is the problem.

8
  • EDIT : NeverMind I missundarstood your post i'm sorry, But i acctualy don't understand your answer, do you mean that I try to send from Arduino to C# because, Me I want to do the opposite...
    – War-sloop
    Commented Dec 8, 2014 at 9:54
  • Yes, I mean that you try send from Arduino to C# (you are already doing that) and see what you get. This will help you understand where is the problem.
    – Omer
    Commented Dec 9, 2014 at 19:33
  • Ok so i Will try if i understand to directly send from arduino to c#
    – War-sloop
    Commented Dec 9, 2014 at 21:04
  • You are already doing it... the line "Serial.print(message);" sending the message back to the C# code. Just see what you get from the C# side as I explained.
    – Omer
    Commented Dec 9, 2014 at 21:12
  • Hooo yea it was a test code , but I forget to read from C# I'm sorry if I didn't understand but it's because I forget this line ...
    – War-sloop
    Commented Dec 10, 2014 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.