Show Posts
Pages: [1] 2 3
1  Forum 2005-2010 (read only) / Syntax & Programs / Re: C# GUI gets Error But Arduino Connects Perfectly on: May 04, 2010, 03:40:11 pm
Your assumption is likely correct. Remember that you are iterating through a list that may not be presented in the same order each time you request it. The reason you are getting the "port is not open" error is because your try-catch block isn't including all calls to the port after the point at which you are "ignoring" the exception. This is precisely why ignoring exceptions will cause you more pain than good.

I agree that .NET does have some drawbacks in this regard, but that is somewhat the price you pay for JIT code, if you really want to avoid this kind of problem, write in C++ and avoid .NET. I'm no Microsoft Fanboy (I'm writing this on one my three MACs, hehe) but I write A LOT of C# code, and we use it for long-running, mission critical processes communicating via serial over our comm satellites.

My bet is that the blackberry driver makes a port with a nonstandard identifier to hide it from other applications and to move it where they want it in the device manager.

Since the problem with oddly named ports is a known-issue I would do a quick check for problem names, notify the user, and skip them when executing any code against the ports.
2  Forum 2005-2010 (read only) / Syntax & Programs / Re: C# GUI gets Error But Arduino Connects Perfectly on: May 04, 2010, 02:53:12 pm
PaulS: You can certainly do that. It is, however, not considered best practice to catch an exception to ignore it. I understand this is probably not production software, but you really aught to try to avoid and handle situations that cause common exceptions first, catch exceptions that are unlikely but possible and handle them second and then gracefully catch and handle the unexpected exception last. This gives you the best possibility to feed back to the user the situation that can cause an exception (without using the term "exception" that a user won't understand) and allow them to correct it before continuing with execution.

An example here would be that you could notify (or log) the user that a serial port cannot be accessed because of its name. Otherwise they may have an inaccessible port and not know why... If you simply ignore the exception there is absolutely no way they will even know there is a problem except that their port won't show up.
3  Forum 2005-2010 (read only) / Syntax & Programs / Re: C# GUI gets Error But Arduino Connects Perfectly on: May 04, 2010, 02:33:53 pm
I've seen the very exception you're getting when there is a serial port in the system that doesn't start with COM. You can theoretically name a serial port pretty much anything in Windows, but .NET doesn't like it. For instance you can call an emulator CNC0 instead of COM1... Windows will let you access it but .NET can't connect to it. You could put a condition in you loop that only lets you try to access ports that have names that fit the COM## naming scheme. It would be easy to grab just the first three characters and compare them to "COM" or use a regular expression to validate it.
I'd say that it is pretty likely this is your problem.
4  Forum 2005-2010 (read only) / Syntax & Programs / Re: C# GUI gets Error But Arduino Connects Perfectly on: May 04, 2010, 01:51:48 pm
Ok, this is just a stab in the dark, but could that serial port already be marked as open? This seems like it would be the wrong exception, but sometimes the CLR is not exactly forthcoming about managed resources. I would try to open one of the other ports using a program like realterm and see if you get the same error when trying to open com 8 or 9.

Realterm is a fantastic free program, by the way, for dealing with serial communications. You might also try installing COM0COM which is a serial null-modem pair emulator, it's a great way to emulate devices for testing. Be sure to name your ports starting with COM though or .NET won't be able to access them.
5  Forum 2005-2010 (read only) / Syntax & Programs / Re: C# GUI gets Error But Arduino Connects Perfectly on: May 04, 2010, 01:04:07 pm
Please post the solution to your problem once you find it for other people to learn from smiley
6  Forum 2005-2010 (read only) / Syntax & Programs / Re: C# GUI gets Error But Arduino Connects Perfectly on: May 04, 2010, 01:03:24 pm
I'm looking over your code here a bit more, but as a quick clarification what I mean about the cast is this:

Instead of -
 foreach (var port in ports)

I would have used
 foreach (string port in ports)

now you can check to see if port == "" and you "shouldn't" have to worry about nulls like you would with var.

Can you post the output of the exception? Also, note from my last post that you should probably do a thread.sleep() immediately after calling port.close() because it doesn't close immediately. There was a linked article you may find interesting.

Another thing you probably don't have to do, but might solve your problem would be to dispose the port object after calling close() and then instantiate a new port object at the beginning of the loop. This would help if its a problem with cleanup or object state between passes. As long as you declare the "usb" variable in the global scope it should still be accessible after your loop exits.

7  Forum 2005-2010 (read only) / Syntax & Programs / Re: C# GUI gets Error But Arduino Connects Perfectly on: May 04, 2010, 10:28:30 am
It may be caused by the fact that one computer has more serial ports than the other. If one computer has only one serial port then the loop you are using would only iterate once. The other computer may have more than one (even if it physically only has one the registry may still list old virtual devices). This would cause the loop to run more than once and it looks like you aren't closing the port that you're opening, and you're not waiting for the port to close. The port does not close instantly so you'll need to have it wait. see this article:
Also I note that you are casting "port in ports" as a var... you will have better luck with the debugger if you cast it as a string as port.getPortNames() returns an array of strings.
8  Forum 2005-2010 (read only) / Interfacing / Re: C# serial port trouble on: October 08, 2010, 03:49:22 pm
Are you sure that the arduino's ftdi chip is being presented as the same com port now as it was the first time. Windows XP is notorious for assigning the same device different port numbers nearly every time the device is reconnected.

Are you reading AND writing to the serial port or just writing? Synchronously or Asynchronously?

9  Forum 2005-2010 (read only) / Interfacing / Re: What's the best way to develop a windows applicat on: September 23, 2010, 04:20:50 pm
There are very good developer tools available for free from Microsoft. The Microsoft Visual Studio Express Editions are free

The IDE is actually pretty incredible (I'm a mac developer at heart, but write a lot in c# for the pc world.)

I would suggest using .Net (and you might consider learning c# if you want to cut down your workload over c++). There are some great reasons to stay with c++ though and you can still winforms use the .NET CLR to make winforms or WPF (like but a bit newer) applications that communicate over the built in serial objects.

You can either write your own simple (or complex) protocol to communicate with your Arduino code or load up one of the arduino programs like bitlash that let you control the pins individually over serial.

If you are not committed to the standard Arduino I highly recommend you look into one of these products, both are arduino compatable but actually use the .Net Micro Framework to run. They're a bit (a lot) faster than the arduino and bring some cool features like multi-threading to the picture.


What you're asking to do is really quite easy, but can be implemented in lots of different ways. There are several threads in here for you to search for and read that answer this question in more detail.
10  Forum 2005-2010 (read only) / Interfacing / Re: Is it possible to interface with c++ on mac? on: December 08, 2009, 02:13:51 pm
Interfacing with Serial ports is actually rather trivial if you're using a framework. Cocoa is an excellent framework that encompasses both a "perfectly Apple" user interface as well as a TON of great system hooks that will get you going fast. I've found Objective C with Cocoa to be a very easy change from C++ especially if you have any additional Java (or other Object Oriented programming) background.

Serial Ports on the mac can be read and written like file IO, since OSX is *nix, all you really need to do is create a very basic protocol and implement it on the Arduino and your desktop app.

11  Forum 2005-2010 (read only) / Development / Re: C# ,DC Motors on: July 22, 2010, 04:28:56 pm
Without thinking too hard on it I'm wondering if the newline character you're sending from your c# program may be causing the == not to work..

Is it possibly trying to evaluate 0x4C == 0x4C 0x0A ?
These two would not be equivalent. This is without thinking about it much though and may not be the problem at all. Just an easy thing to check.
12  Forum 2005-2010 (read only) / Development / Re: Arduino Duemilanove programming in Visual Studio on: July 02, 2010, 08:53:41 pm
No, you'll write two programs:

1) The Visual C++ Program that will access a serial port and send commands over

2) A C program that you will upload to the Arduino board and will run on its own. (This code is called a sketch in the world of Arduino)

You should not need the IDE (Development Environment) after you write the code that will be uploaded to the Arduino. The C program you write will simply collect commands coming over the serial port and set the pins accordingly.

If you use Bitlash as one of the posters suggested you may not need to write any C code at all for the Arduino, simply use the IDE to upload the Bitlash Sketch. Then in your C++ program connect via serial to the Arduino board and send it the Bitlash specific strings to set the pins etc.

You are aware that when you connect the Arduino board to your computer that the on board FTDI chip creates a virtual serial port on your computer for you, right? The Arduino basically has a USB to serial converter built in, you connect your Arduino to the computer via USB (and get the benefit of the 5V power) but your computer sees it as an easy-to-talk to standard COM port.
13  Forum 2005-2010 (read only) / Development / Re: Arduino Duemilanove programming in Visual Studio on: July 02, 2010, 10:57:44 am
My recommendation is not to over complicate it.

Write the Arduino code in C in the Arduino dev environment. Have the Arduino listen for serial strings and set the i/o pin accordingly.

For example I have a program that switches between two audio sources and can mute them both.

send *01 and it sets pin 4 low, pin 5 low.
send *02 and it sets pin 4 high, pin 5 high.
send *0M and it sets pin 4 high, pin 5 low.

The audio is N/C (normally connected) through the relay connected to pin 4 thus the audio passes if the device loses power or gets reset. When you apply power to 4 it mutes the audio on that relay. The audio on the relay is connected to N/O (normally open) so when that pin goes high audio is allowed to pass.

The * makes it easy to indicate a new serial command and all commands are two characters long, anything else is ignored. You could use three bytes instead of ascii characters:

[0xFF, 0x00, 0x01] One
[0xFF, 0x00, 0x02] Two
[0xFF, 0x00, 0x03] Mute

Sending bytes from C++ should be dead simple. You could even do queries of the states if it was advantageous.

14  Forum 2005-2010 (read only) / Development / Re: Arduino Duemilanove programming in Visual Studio on: July 02, 2010, 10:27:13 am
I'm not absolutely sure what you're asking. Are you planning on having this connected to a computer running a program you've written in Visual Studio?

I do a lot with VS, primarily in C#, but have found that the Arduino ecosystem is fairly comprehensive and covers my needs for writing the Arduino Boards flash memory.

If you're looking to control relays with a C++ program (Windows forms, WPF or Web) you will probably find that writing a simple serial protocol, or using an already established one, will do what you need.  

I highly recommend you look at "Firmata" at
There is an awesome tutorial here:

If all you want to do is switch and query i/o pins you can write your own protocol in about a dozen lines of code.
15  Forum 2005-2010 (read only) / Troubleshooting / Re: About stepper motors... on: December 27, 2010, 04:22:59 pm
Judging by the number of wires coming out of it I'd say it's a good guess to call it a bipolar stepper motor. Good information here:

And there is a library for steppers that will work with Uno:

I'm not much of one to complain about people not googling first, but all of this information is in the Arduino reference which is a GREAT place to start when you have something new you want to try out.
Pages: [1] 2 3