Serial ports and visual C++

this->serialPort1->PortName = "COM5";
this->serialPort1->Open();
this->serialPort1->Write( "7" );
this->serialPort1->Close();

I am trying to make this visual c++ program turn on and off an LED. The program is sending a letter to the arduino and the arduino is using case statements to decide what to do. Well my program worked but when I switch to my desktop it didnt. I originally wrote my program for COM4 because that was the one I use to send my sketches to the board. Then I looked at my desktop and COM4 wasnt a choice. It only went up to COM3.

Is there anyway to make the COM port a variable. Like a have a drop down list and there are four COM ports to choose from or radio buttons? Or maybe there is a COM port that works on any computer regardless. Any ideas. I need it for the visual C++ program. Like I said it works but as I change computers the COM ports change and the program stops working

In C#, the SerialPort class has a GetPortNames() method. I'm pretty sure that C++ and C# use the same SerialPort class.

The GetPortNames() method returns a list of ports that can be communicated with. Whether anything is actively listening is up to you to determine (or have the user select).

Be aware, though, that opening a serial port to the Arduino causes a reset, so it should not be done every time there is data to send. It should be done only once.

Paul I'm trying to accomplish the same thing. Can you please send me your visual C++ code? Thanks!!!!

Sorry I my last post was intended for @aaronjk0406. Please send me your code. Thanks!

If you are talking about setting COM port as a variable I can not help you. Unfortuantly I still haven't figured it out. Every attempt I do gets compiling errors. If I figure it out I will post it/give it to you.

If you are just talking about combining visual c++ with arduino then its simple. (I will go into detail. Sorry if this isnt what you meant). Its easiest to do with Microsoft Visual c++ 2008 express edition (its free but you need to have a m$ windows)

File >> New >> Project...

"Visual C++" >> "CLR"

Select "Windows Form Application". Enter a name for your project. Click "OK"

Toolbox (right side of the screen ) >> Components >> SerialPort.

Click, Drag and then Drop "SerialPort" onto your form. It will show up as a resource at the bottom of the form window. You may optionally rename "serialport1" to "arduino" and change the code snippets below.

Toolbox >> Common Controls >> Button. Click, Drag and then Drop "Button" onto the form.

Double-Click on your new button. Enter the following code:
this->serialPort1->PortName = "COM5"; // Replace with your COM port!
this->serialPort1->Open();
this->serialPort1->Write( "a" ); // In the future, you'll expand on this
// to write your custom data to the board
this->serialPort1->Close();

that was taken straight from arduino's playground. What I did as a prototype was created a form with two buttons. An "ON" and an "OFF".

So I started the file>>new>>project>>CLR>>Windows Form

it came back with a blank form. I then under toolbar added the serial port from components and also two buttons.

double click on one of the buttons and add the code: (without the '=')

this->serialPort1->PortName = "COM5"; // Replace with your COM port!
this->serialPort1->Open();
this->serialPort1->Write( "a" ); // In the future, you'll expand on this
// to write your custom data to the board
this->serialPort1->Close();

click back on your form and you should see your form with the two buttons. Double click the other button and add the same code but instead of "a" make it "b"

Now you can build/release your project....you should have a working .exe


Arduino sketch:
All I did was modify the example one Examples>>Control>>switchCase2

int ledPin = 13;

void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);

}

void loop() {

if (Serial.available() > 0) {
int inByte = Serial.read();
switch (inByte) {
case 'a':
digitalWrite(ledPin, HIGH);
break;
case 'b':
digitalWrite(ledPin, LOW);
break;
default:
// turn all the LEDs off:
digitalWrite(ledPin, LOW);

}
}
}


This example is to turn an LED connected at pin 13 on or off. I suggest doing the arduino code first because then you can use the built in serial monitor and just type the letter and hit enter. Then worry about the VC++

Like I said you may already be a master at VC++ combined with arduino and were just looking for the variable part but Idk. it wasnt clear. Im sure this will help someone though or some can help me add four radio buttons that assign the COM port as a variable