Offline
Newbie
Karma: 0
Posts: 30
|
 |
« on: January 27, 2013, 09:26:39 am » |
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/SerialCommsCSharpbut 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.
|
|
|
|
« Last Edit: January 27, 2013, 09:28:15 am by fulminator »
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 334
Posts: 36459
Seattle, WA USA
|
 |
« Reply #1 on: January 27, 2013, 10:10:29 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #2 on: January 27, 2013, 11:00:32 am » |
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.
|
|
|
|
« Last Edit: January 27, 2013, 11:02:21 am by fulminator »
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 334
Posts: 36459
Seattle, WA USA
|
 |
« Reply #3 on: January 27, 2013, 11:06:29 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #4 on: January 27, 2013, 11:31:37 am » |
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.
|
|
|
|
« Last Edit: January 27, 2013, 01:46:17 pm by Coding Badly »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19368
I don't think you connected the grounds, Dave.
|
 |
« Reply #5 on: January 27, 2013, 11:38:32 am » |
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?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 334
Posts: 36459
Seattle, WA USA
|
 |
« Reply #6 on: January 27, 2013, 11:49:43 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #7 on: January 27, 2013, 01:31:29 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 334
Posts: 36459
Seattle, WA USA
|
 |
« Reply #8 on: January 27, 2013, 01:35:04 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 334
Posts: 36459
Seattle, WA USA
|
 |
« Reply #9 on: January 27, 2013, 01:36:02 pm » |
edit: I have attached the full code here: I don't think I'll go there for code.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #10 on: January 27, 2013, 02:18:53 pm » |
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.htmlMy 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.
|
|
|
|
« Last Edit: January 27, 2013, 02:29:07 pm by fulminator »
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 334
Posts: 36459
Seattle, WA USA
|
 |
« Reply #11 on: January 27, 2013, 02:27:52 pm » |
"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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #12 on: January 27, 2013, 02:36:42 pm » |
"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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 129
Posts: 10379
|
 |
« Reply #13 on: January 27, 2013, 02:40:19 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 129
Posts: 10379
|
 |
« Reply #14 on: January 27, 2013, 02:45:10 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|