Go Down

Topic: Serial commands to Arduino mega 2560 (Read 7235 times) previous topic - next topic


Jan 21, 2011, 02:19 pm Last Edit: Jan 21, 2011, 02:20 pm by Xander112 Reason: 1
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:
Code: [Select]
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 = Serial.read(); // 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:

Code: [Select]
#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?




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


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.

Coding Badly

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

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131