Serial communication

Hello, I need some help with serial communication. I have a small C# app from which I send data to arduino and it gives me back something to the app. It forks fine with Uno. The problem is that when I have tried the same code with Yun, it receives data from the app but the Yun doesn't gives me back anything to the app. I also tried to send something to the Yun from the arduino terminal and it works fine, while I have used the same code which I useing when sending from data from my app. Once again, everythig works perfect with Uno, but I have to use Yun.

Without seeing your code, how can we comment .....

Strange as it may seem, the Yun does not have a spare serial connection.

...R

I am sorry :slight_smile: here is my code for Arduino:

String primljeno = “”;
int x=0;
int y=0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
;
}
// Serial.println(“Program za izdvajanje brojeva iz stringa”);
// Serial.println(“Uneti string u obliku: x=12.y=8. obavezno uneti tacku na kraju kako bi se program izvrsio uspesno”);
Serial.println(“Program za upravljanje cilindrima pomocu serijske komunikacije”);
pinMode(13, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
while (Serial.available()>0){
int ulaz = Serial.read();

if (ulaz == ‘\n’)
{
}
else{
primljeno += (char)ulaz;
}

if (ulaz == ‘.’)
{
// Serial.println(“Poslao si:”);

if(primljeno.startsWith(“x”))
{
// Serial.print(“X vrednost koja iznosi: “);
primljeno.replace(“x=”, “”);
primljeno.replace(”.”, “”);
// Serial.print(primljeno);
x = primljeno.toInt();
x = x + 27;
Serial.write(“Postignuta vrednost za X je:”);
Serial.println(x);
// if(x>10)
// {
// Serial.println(", sto je vece od 10.");
// }
// if(x<10)
// {
// Serial.println(", sto je manje od 10.");
// }
primljeno = “”;
}

if(primljeno.startsWith(“y=”))
{
// Serial.print(“Y vrednost koja iznosi: “);
primljeno.replace(“y=”, “”);
primljeno.replace(”.”, “”);
// Serial.print(primljeno);
y = primljeno.toInt();
y = y - 64;
Serial.println(“Postignuta vrednost za Y je:”);
Serial.println(y);
// if(y>10)
// {
// Serial.println(", sto je vece od 10.");
//}
// if(y<10)
// {
// Serial.println(", sto je manje od 10.");
// }
primljeno = “”;
}
}
}
if (x>100 && y>100)
{
digitalWrite(13,HIGH);
delay(300);
digitalWrite(13,LOW);
delay(300);
}
}

I am sending from C# app something like x=123.y=456. and it works on Uno, the Yun receives these data but doesn’t gives back anything

Your code

should look like this

Use the code button </>

I have tried it on my Uno and my Yun and it does not work on my Yun either - apart from printing the opening line.

(In my earlier comments I did not realize you were just using the USB connection with your Yun)

I can’t understand your code because of the language. What is it expecting the user to do ? And what is the code doing with the data it receives?

…R

In arduino serial monitor you should enter x=300. with a dot at the end. Then the code deletes x= and the dot from the received string, and the value 300 converts to integer. After that there is a small operation x = x + 27, and the board shoul give you back some string and 327.

That is all. So, it works on Uno and Yun when I send from the arduino serial monitor, and it works just for Uno when I send from my C# app.

Yes, I have connected my Yun with the USB cable to my laptop. Should I maybe use the RS232 shield?

Do you want to convert my code to english and post it that way? I don't think it would help because there is just some strings like: 'you send me that...' and 'i sending you back that...' and also I don't think that there is something wrong with my C# app because it works fine with my Uno.

Yes, following your instructions the code works on my Yun and on my Uno.

There is no reason to use an RS232 shield.

Does your Uno restart when your C# program opens the COM port ? Does your C# program allow for that ?

The "Leonardo" attached to a Yun behaves differently. With a Leonardo to cause a restart, your C# program would need to open the serial port at 1200 baud and then close it again. Then open it at your chosen baud rate. I'm not certain if that works for a Yun.

Can you write a simple Arduino program that just prints "Hello World" every second and receive that from your Yun. That would help to avoid the risk that the Yun is not receiving the data correctly.

It seems a strange way to be using a Yun. What is your ultimate objective ?

...R

Ok, I will try with restarting the board from the app, I hope it will help.

Well, my Yun receives the data correctly, I have find it out at different way.

I need to use a Yun because I have to do the same thing from a web page, which I have tried and works fine. So, this is just a test code for my C# app. After receiving data, I need to handle with some pneumatic valves which will bring the cylinders to a position received from the user. Everything works, I just need to send back a message to the app: the cylinder reached the position, what my Yun doesn't want to do, which is very strange.

thedoctor46:
I need to use a Yun because I have to do the same thing from a web page, which I have tried and works fine.

If you are using the Yun with a web page why are you trying to send data via the USB connection ?

So, this is just a test code for my C# app. After receiving data,

If the C# app is to receive the data from a web page why are you testing it with the USB connection ?

If you just want something to give your C# app stuff to chew on why not just use the Uno ?

…R

No,maybe I wasn't clear enough :). It's like two projects in one. I need to enable two different types of communications with arduino:

  1. User sends data from a C# app to arduino and it gives him back something, to the app, (serial communication)

  2. User sends data from a web page, which is in a MicroSD card in arduino, to my arduino and it gives him back something, on the page.

The second type works well, I have tried it and everything is fine. The problem is with the first type, the Yun receives the data but doesn't sends to the app or to any other serial monitor/terminal, it just works with the arduino serial monitor.

So, I dont want to change my Uno to Yun every day, because there are many outputs and inputs connected to it. I was like 'ok, it should work with yun, it is just a simple serial communication' but I haven't expected a problem like this.

I have no trouble talking back and forth over the USB serial connection between my Yun and C# programs. The Yun talks equally well to my program, the SerialMonitor, PuTTY, or HyperTerminal.

I can't think of any reason why the Yun would behave differently than the Uno when it comes to the C# program. It seems like you have something really weird going on. I was going to suggest a driver or hardware problem us preventing you from receiving the data, but it works with the SerialMonitor, so the ability to receive data is there. I'm at a complete loss why the C# code can't receive it.

You say the Yun is receiving the data, are you sure? It looks like you have code that flashes the LED if you enter sufficiently large X and Y values. This is happening, right? And no flash if you enter small values?

I fear this will be a difficult one to find...

Yes, everything is how you said. When I sending data from my C# app to arduino the Rx led indicates incoming data, but it doesn't send back anything (nothing happen on Tx led). So, I dont know...

Uno + serial monitor works everything

Uno + C# works everything

Yun + serial monitor works everything

Yun + C# - the Yun just receives

The same code for arduino

http://playground.arduino.cc/Csharp/SerialCommsCSharp

This code does not work on Leonardo's because, apparently, a Leonardo requires setting the DtrEnable property of the SerialPort object to true.

C# code for Leonardo/Yun:

mySerialPort.DtrEnable = true;
mySerialPort.RtsEnable = true;

Thanks, I'll try it on monday

Yes! Setting DTR sounds very familiar: now that you mention it, I'm sure I did that with my C# code. I'm sure that will be the solution.

Why do you need a yun if you already are using a pc with a c# app?

Ok, the DtrEnable solved my problem, thank you once again!

mart256: Why do you need a yun if you already are using a pc with a c# app?

Because there are some things a PC doesn't do well, like real-time operations, hardware I/O control, sensor data collection and logging when the PC isn't connected and running all the time, SPI and I2C communications, etc.

I have a similar setup where the Yun is a custom I/O processor, talking to a few USB devices using the Linux USB host interface, some discrete I/O, LED indicators, and a couple LCD character displays controlled by the sketch, and all talking to the PC using direct USB serial or a network TCP socket. The main brains of the application are in C#, providing the GUI and main user interface, while the sketch runs the I/O hardware, and the Linux side of the Yun ties it all together.

Multiple systems, each with their own strengths and weaknesses. Any one system would struggle to do the whole thing by themselves, but by working together the strengths of each system are amplified and the weaknesses become insignificant.

Sure, I could've done it with an array of off-the-shelf interfaces and devices, and controlled it all on the PC. But using the Yun allows a single board to do it all, allows network connectivity, and allows adding some intelligence to make the PC interface simpler and run at a higher level, making the PC code less dependent on the actual hardware implementation (future hardware upgrades won't affect the PC code as much.)

In that case Id rather use a cheaper board, like a Uno or mega. In my opinion the linux side being not necessary.

mart256: In that case Id rather use a cheaper board, like a Uno or mega. In my opinion the linux side being not necessary.

That's a matter of personal choice. If you need a WiFi or ethernet shield there is a much smaller price gap.

And there is a lot of stuff (especially web stuff) that is very much easier to do in Python rather than on a Mega or Uno.

...R

Robin2: And there is a lot of stuff (especially web stuff) that is very much easier to do in Python rather than on a Mega or Uno.

Precisely! I value my time, as I don't have a lot of it to spare, and would rather use it it make money than fiddle around with pet projects. If the Yun saves me an hour in implementation time, it's paid for the higher hardware cost.

40 years ago, I played around with hardware circuits just for the fun if it. 30 years ago, I played around with computers just for the fun of it. But now I've gotten that out of my system - these days I build circuits and write programs so that I can actually accomplish something and solve a problem. I still have fun doing it, and I look forward to these projects, but having fun isn't the primary goal anymore.