Serial commands to Arduino mega 2560

Hey all,
I have read a lot of post about serial communication but its still not working here. On my arduino mega i have the following running:

int ledPin = 13;
int state=0;
void setup() {
    pinMode(ledPin, OUTPUT); // pin will be used to for output
    Serial.begin(9600); // same as in your c++ script

void loop() {
  if (Serial.available() > 0)
    state =; // used to read incoming data

    switch(state)// see what was sent to the board
      case '1': // if the the one was sent
      case '0': // if 0 was sent

On my pc I have the Serial Class like its found here: see reply below

Then I wrote the next simple code:

#include "SerialClass.h"
#include < iostream >

int main( int argc, char **argv )
      Serial mySerial("COM4");
    std::cout << mySerial.IsConnected() << std::endl;
    return 0;

it compiles fine but when i run it it tells me it cant connect to COM4, while the ArduinoIDE can upload the code trough COM4.

Anybody know why its not working?


I was not allowed to post the link in the first post:

Do you have the arduino serial port "on" when you try the code? Try running the code without the Arduino IDE running.

Yes, but I have already tried it when its not running. I know the its not going to work with the serial monitor on but I don't have that on either.

I believe there are problems with that serial wrapper. Folks have reported success with this one...