Go Down

Topic: Csharp playground (Read 1 time) previous topic - next topic

Jan 27, 2013, 03:26 pm Last Edit: Jan 27, 2013, 03:28 pm by fulminator Reason: 1
Hello, I am new to forum, so please be indulgent.

I am trying to write a WPF in C# that by a press of a button automatically iterate trough all ports available, detects the arduino board via a 5 byte message handshake, and then connects to that port (more exactly, saves the port name for later use in my WPF).

So, I have tried this: http://playground.arduino.cc//Csharp/SerialCommsCSharp
but did you notice it has like a buzzilion errors!?

After I've corrected the code and tryied again, however, it isn't connecting, I mean, it's not assigning the arduino port name to the variable! Which is for sure a design error, and this code is on official Arduino site!

Did I miss anything here? Is there another handshake? Can you guys please help me?

ps. please do not say:"why won't you connect manually?" Because everytime I am pluging off and in my board it has another port name. And I am using multiple computers, too.

PaulS

Quote
So, I have tried this: http://playground.arduino.cc//Csharp/SerialCommsCSharp
but did you notice it has like a buzzilion errors!?

Yes, I have. No, it didn't. Post YOUR code, and (some of) the buzzilion errors if you expect help.

Quote
After I've corrected the code and tryied again, however, it isn't connecting, I mean, it's not assigning the arduino port name to the variable!

Bummer. What variable?

Quote
Did I miss anything here?

Obviously. More than one thing, likely.

Quote
Is there another handshake?

Yes, there is a secret handshake. Someday, we'll teach it to you.

Quote
Can you guys please help me?

With what? You haven't posted any code, defined which Arduino you are using, or what you need help with.

Quote
ps. please do not say:"why won't you connect manually?" Because everytime I am pluging off and in my board it has another port name. And I am using multiple computers, too.

Well, I am going to say that. What I do is provide a drop down list to let the user select the port that the Arduino is attached to. You could, too.

If you want the C# application to select the correct port itself, AFTER you know that communications work, you are free to add that. But, don't make that the FIRST thing you do.

#2
Jan 27, 2013, 05:00 pm Last Edit: Jan 27, 2013, 05:02 pm by fulminator Reason: 1

Quote
So, I have tried this: http://playground.arduino.cc//Csharp/SerialCommsCSharp
but did you notice it has like a buzzilion errors!?

Yes, I have. No, it didn't. Post YOUR code, and (some of) the buzzilion errors if you expect help.

Quote
After I've corrected the code and tryied again, however, it isn't connecting, I mean, it's not assigning the arduino port name to the variable!

Bummer. What variable?

Quote
Did I miss anything here?

Obviously. More than one thing, likely.

Quote
Is there another handshake?

Yes, there is a secret handshake. Someday, we'll teach it to you.

Quote
Can you guys please help me?

With what? You haven't posted any code, defined which Arduino you are using, or what you need help with.

Quote
ps. please do not say:"why won't you connect manually?" Because everytime I am pluging off and in my board it has another port name. And I am using multiple computers, too.

Well, I am going to say that. What I do is provide a drop down list to let the user select the port that the Arduino is attached to. You could, too.

If you want the C# application to select the correct port itself, AFTER you know that communications work, you are free to add that. But, don't make that the FIRST thing you do.


Whoa, you sure showed me!
I do not know what, but you sure showed me!

Ty for you help, which equals with 0 so far, especially when I am dealing with 'wheels' that I, logically, didn't wanted to reinvent. My post was (translation for idiots): please guide me to a correct or another 'wheel' (serial port communication example).

And the best part is this:
Quote
Bummer. What variable?
when exactly in my post I stated:
Quote
more exactly, saves the port name for later use in my WPF
I guess you were too smart, a genius perhaps, that you couldn't link together 2 stupidities of mine.

PaulS

If you don't want to post your code, that is fine. How you can expect us to help you if you don't, though, escapes me. All I can do is wish you luck.

#4
Jan 27, 2013, 05:31 pm Last Edit: Jan 27, 2013, 07:46 pm by Coding Badly Reason: 1

If you don't want to post your code, that is fine. How you can expect us to help you if you don't, though, escapes me. All I can do is wish you luck.

Ok, you win. I have no intention hiding my code, because it's almost the same with the one from arduino csharp playground. It would be redundant, but, here it is:
this is the corrected arduino code from csharp playground: https://gist.github.com/4649085 and this is my C# code: https://gist.github.com/4649120 . C# GUI has only one button. And it's just the click of that button.


Ty in advance. Please guide me to a simplier or workable c# - arduino handshake if you know. I've scoured the internet and found nada.


Moderator edit: link removed.

AWOL

Code: [Select]
if (Serial.available() == 5) I'm not keen on that construct, particularly when there are long delays in a sketch.
Is that one of the bazillion errors?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

The .cs file that you posted is only one of the files in a C# project. I'm not inclined to want to reproduce (by guessing) those other files. You can zip up your whole solution directory (then delete the pdb files from the zip file) and attach that zip file to your post.

Is that C# file the code with the errors or the fixed code? If it is the fixed code, what changes did you need to make?

Adding some calls to Console.WriteLine() and running the C# code in debug mode would tell you whether the Arduino's serial port is detected and what, if any response is received.

Which Arduino are you trying to detect? That code would detect a Leonardo, for instance.


Code: [Select]
if (Serial.available() == 5) I'm not keen on that construct, particularly when there are long delays in a sketch.
Is that one of the bazillion errors?


No, actually it works. The code posted by me is corrected and it's running with no errors. But it doesn't provide results.
It's comparing the serial data length (which is 5 bytes), I guess. There is no debug mode in Arduino GUI, so I can't really know what's going on   on that line. BTW, in my previous post I have added a zip containing the source.

PaulS

AWOL's point (very subtle, but very important) is that == is not a good choice. What would happen if 6 characters arrived? Your code would never do anything. The test for == 5 should be >= 5, so that 6 characters will cause some to be read, too.

PaulS

Quote
edit: I have attached the full code here:

I don't think I'll go there for code.

#10
Jan 27, 2013, 08:18 pm Last Edit: Jan 27, 2013, 08:29 pm by fulminator Reason: 1

Quote
edit: I have attached the full code here:

I don't think I'll go there for code.

It's just a local site for file shareing. Here, on another, this doesn't have kinky name or pics: http://www.filehost.ro/29219539/serialTest_rar/
here it is on an internation filehost: http://www.datafilehost.com/download-98c72190.html

My opinion that comparing with == it's correct. What would happen if more bytes are sent? Nothing, as expected, because our Arduino sends ONLY 5 bytes.

I forgot to mention:
- it's a Leonardo Board.
- debugging it I have found that returnMessage   has the value of a cross, a sqare, 2 end of line character and an upper corner ASCII character. returnMessge += Convert.ToChar(currentPort.ReadByte());
- returnMessage doesn't contain HELLO FROM ARDUINO as wanted

So, in my opinion, this part of the code is wrong:
Quote
int count = currentPort.BytesToRead;
  string returnMessage = "";
  while (count > 0)
  {
     intReturnASCII = currentPort.ReadByte();
     returnMessage = returnMessage + Convert.ToChar(intReturnASCII);
     count--;
   }

Can you guys please help me? If you will post me link to another handshake that you know or already use, that will be awesome! Thank you in advance.

PaulS

"Find a real sex partner in California". Really, can't you just attach the damned code?

Quote
My opinion that comparing with == it's correct. What would happen if more bytes are sent? Nothing, as expected, because our Arduino sends ONLY 5 bytes.

Then, testing for 5 or more shouldn't be an issue.

Quote
- it's a Leonardo Board.

Earlier, a not disappeared from my statement. The C# code you posted earlier does not open the serial port correctly to talk to a Leonardo. To communicate with a Leonardo, the DtrEnable property of the SerialPort object must be set to true. For other boards, setting DtrEnable to true (the default is false) does not affect the Arduino.


"Find a real sex partner in California". Really, can't you just attach the damned code?

Quote
My opinion that comparing with == it's correct. What would happen if more bytes are sent? Nothing, as expected, because our Arduino sends ONLY 5 bytes.

Then, testing for 5 or more shouldn't be an issue.

Quote
- it's a Leonardo Board.

Earlier, a not disappeared from my statement. The C# code you posted earlier does not open the serial port correctly to talk to a Leonardo. To communicate with a Leonardo, the DtrEnable property of the SerialPort object must be set to true. For other boards, setting DtrEnable to true (the default is false) does not affect the Arduino.


I am terrible sorry for that, I replied already to the admin that I wasn't aware of that. Sincerely, that site it's only filehost site, but whateva. And about the question, you right, dude. I can't recall did something good. And I haven't upload a file in ages.

Thank you. It 'kinda' works right now. I get the right port name saved in a variable as I wanted for further use. Problem solved.

One thing remains, though... How can we contact the admins to repair the code in the csharp playground? I am sure a lot of people check that page first, so it would be nice if it will be corrected. And ofcourse, a DtrEnable information with bold and red fonts.

Coding Badly


To attach a file to a post...

• Start a post (click Reply) or modify one of your existing posts
• Click Additional Options... (below the edit window to the left)
• Click the Browse button
• Select the file
• Click Post

Coding Badly

One thing remains, though... How can we contact the admins to repair the code in the csharp playground?


Not necessary.  The Playground is a public wiki.

Go Up