Pages: [1]   Go Down
Author Topic: Arduino - C#  (Read 936 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey all.

I spent the weekend trying to get a simple C# program to send data via serial to my Arduino Diecimila. I'm just trying to send a very simple stream of 12 bytes - however my timing seems to be off. Most of the time, it misses all the data, and sometimes it catches the last couple.

The code worked fine when that's all it did, but when I included the code into the rest of my work it suddenly stopped working.

I can't, for the life of me, understand why. The rest of the code in the loop() section only activates when ldrPin (2) changes state... So I don't see how I could possibly miss so much data. I even included 5 "dummy" bytes in hopes of giving the arduino a chance to catch up.

If anybody would mind taking a look at this code, I would greatly appreciate it.

http://pastebin.com/m405d7d44 - arduino code
http://pastebin.com/m13775e0a - C# code

Thanks in advance.
Tim
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 163
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you tripping the autoreset when you open the serial port by any chance?
Logged

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

I bet that fjornir's analysis is correct:

1. C# app opens the serial port.
2. This causes a reset on Arduino
3. C# app take a few ms to print the 12 bytes.
4. At the same time, Arduino is still in the 2-7 second restart phase in which it looking for a newly uploaded sketch.
5. By the time Serial.begin() is actually called on the Arduino, the serial data from C# is long gone.

I bet if you inserted a 10 second delay at the beginning of the C# app, you'd get the results you seek.

Mikal
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Basically, it looks like you guys were right on the money (Thank you ever so much) I added a delay in the C# code before sending the data and it worked. I just never had to do anything like that when using PySerial in the past... but it worked.

There is still one teensy tiny little problem I can't wrap my head around, however.

I changed the sleep mode to SLEEP_MODE_IDLE so that the arduino could be awakened by serial data... however, when the arduino is awakened by serial data it reads it perfectly,

However, if it is not awakened (already awake) then it misses the first byte from the C# program. This is consistent behavior. Can you see any reason for this in the code? I'm seeing cross eyed at this point...

Thanks again for all your help thus far.

[EDIT]
Please allow me to pose this question in addition to the weirdness above.

My project uses a light dependent resistor in a voltage divider to act sort of like a night light. When the ambient light in the room is too high, the arduino goes to sleep.

So it seems to me that to ensure the arduino is ready to receive updates via serial, I have one of two options: use SLEEP_MODE_IDLE so that serial data awakens the arduino, or attach a physical button that the user must depress before sending the serial data (to pull the ldrPin low and make sure the arduino is awake)

Now, the device would only need to receive updates every couple of months or so... and using SLEEP_MODE_IDLE yields some weird results... for example, when the arduino is supposed to be asleep it keeps repeating over serial "going to sleep" - even though if it were truly sleeping it wouldn't be looping anymore... also, using SLEEP_MODE_IDLE it doesn't seem as though it always wakes up as it's supposed to... So my question is, how bad of design practice do you think it would be for me to have a button on the device that must be pressed before you can send serial data?

I swear, working on this project has been like a lot of smoke and mirrors, a lot of things didn't seem to work the way I originally thought they would! hah.

Thanks again
Tim
« Last Edit: January 26, 2009, 02:10:49 pm by imagitronics » Logged

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

We had the same problem with our C# app and Arduino so we ended up inserting a 47K resistor between the 5v and Reset pinouts. It appears to have cured the problem.

Trev
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 626
Posts: 34184
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so we ended up inserting a 47K resistor between the 5v and Reset pinouts

That will do nothing.

There is already a 10K resistor there so all you are doing is slightly lowering the value of pull up. This will have no effect what so ever.
Logged

Pages: [1]   Go Up
Jump to: