Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I receive data from a PC to an Arduino?

I developed an application that sends data for an Arduino by the serial port, but I can't understand how I can receive it on the Arduino. I send a string by the serial port for the Arduino and the Arduino receives it, but it does not work in my code (on the Arduino, I receive a byte at a time).

Update: it's working ;)

The code in C# that sends data:

using System;
using System.Windows.Forms;

using System.Threading;
using System.IO;
using System.IO.Ports;

pulic class senddata() {

    private void Form1_Load(object sender, System.EventArgs e)
    {
        //Define a serial port.
        serialPort1.PortName = textBox2.Text;
        serialPort1.BaudRate = 9600;
        serialPort1.Open();
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
        serialPort1.Write("10");  //This is a string. The 1 is a command. 0 is interpeter.
    }
}

The Arduino code:

I have Update the Code

#include <Servo.h>

Servo servo;
String incomingString;
int pos;

void setup()
{
    servo.attach(9);
    Serial.begin(9600);
    incomingString = "";
}

void loop()
{
    if(Serial.available())
    {
        // Read a byte from the serial buffer.
        char incomingByte = (char)Serial.read();
        incomingString += incomingByte;

        // Checks for null termination of the string.
        if (incomingByte == '0') { //When 0 execute the code, the last byte is 0.
            if (incomingString == "10") { //The string is 1 and the last byte 0... because incomingString += incomingByte.
                servo.write(90);
            }
            incomingString = "";
        }
    }
}
like image 817
FredVaz Avatar asked Nov 27 '11 01:11

FredVaz


2 Answers

Some things which make my eyebrow raise:

serialPort1.Write("1");

This will write exactly one byte, the 1, but no newline and no trailing NUL-Byte. But here you are waiting for an additional NUL byte:

if (incomingByte == '\0') {

You should use WriteLine instead of Write, and wait for \n instead of \0.

This has two side effects:

First: If there is some buffering configured, then there is a certain chance, than a new line will push the buffered data to the Arduino. To be certain you have to dig through the docs at MSDN.

Second: This makes your protocol ASCII-only. This is important for easier debugging. You can then use a plain terminal program like Hyperterm or HTerm (edit) or even the Serial Monitor in the Arduino IDE itself (edit) to debug your Arduino-Code without worrying about bugs in your C# code. And when the Arduino code works you can concentrate on the C# part. Divide et impera.

Edit: Another thing I noticed after digging out my own Arduino:

incomingString += incomingByte;
....
if (incomingByte == '\n') { // modified this
  if(incomingString == "1"){

This will of course not work as expected, because the string will contain "1\n" at this point. Either you compare to "1\n" or move the += line after the if.

like image 171
A.H. Avatar answered Oct 14 '22 13:10

A.H.


You could alternatively try using the Firmata library - it's a much better way of having standard firmware on the Arduino and managing it from .net

I believe, Firmata 2.0+ has support for I2C and servo control.

http://firmata.org/

like image 44
Asti Avatar answered Oct 14 '22 13:10

Asti