Csharp playground

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: Arduino Playground - HomePage
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.

So, I have tried this: Arduino Playground - HomePage
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.

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?

Did I miss anything here?

Obviously. More than one thing, likely.

Is there another handshake?

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

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.

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.

PaulS:

So, I have tried this: Arduino Playground - HomePage
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.

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?

Did I miss anything here?

Obviously. More than one thing, likely.

Is there another handshake?

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

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.

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:

Bummer. What variable?

when exactly in my post I stated:

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.

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.

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.

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: gist:4649085 · GitHub and this is my C# code: gist:4649120 · GitHub . 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.

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?

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.

AWOL:
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.

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.

edit: I have attached the full code here:

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

PaulS:

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: serialTest rar.html
here it is on an internation filehost: What Happened to the old DataFileHost?

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:

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.

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

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.

  • 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.

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

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.

  • 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.

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

fulminator:
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.

As I said in my first post... "Hello, I am new to forum, so please be indulgent.". But I was going to ask that, so thank you. I have attached the working project to this post. It works thanks to Paul. It was DtrEnable, like he said.

I have included a pic how my C# GUI looks like. It's easy to custom afterwards. Mayby someone could use it. I forgot to include a comment, though, in the DtrEnable line, because that can be a real frustration.

Not necessary. The Playground is a public wiki.

Now I get it why there were a buzzlion errors, and the nature of those errors! There are errors like pin_4 instead of pin_3 and commented lines, brackets missing... a jackass modified with bad intentions the code. I didn't really observe it was a wiki. I'll try to insert a link to this topic.

Thanks again, Paul and fellas.

edit: I have modified the article, but I didn't touch any of the code, I didn't really wanted to edit the man's code. So I just posted a link at the bottom of the page to this topic. Is that good? This is my first wiki that I've edited ever. See here: Arduino Playground - HomePage

serialTest.rar (53.6 KB)

csharpGUI.png

sketch_jan27a.ino (1.64 KB)