Pages: [1]   Go Down
Author Topic: Serial commands to Arduino mega 2560  (Read 5466 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
        digitalWrite(ledPin,HIGH);
      break;
      case '0': // if 0 was sent
        digitalWrite(ledPin,LOW);
      break;
      default:
      break;
    }
  }
}

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

Then I wrote the next simple code:

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

int main( int argc, char **argv )
{
      Serial mySerial("COM4");
    std::cout << mySerial.IsConnected() << std::endl;
      system("PAUSE");
    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?

Cheers,
Xander
« Last Edit: January 21, 2011, 08:20:44 am by Xander112 » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was not allowed to post the link in the first post: http://www.arduino.cc/playground/Interfacing/CPPWindows
Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3115
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 178
Posts: 12288
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


I believe there are problems with that serial wrapper.  Folks have reported success with this one...
http://www.codeproject.com/KB/system/serial.aspx
Logged

Pages: [1]   Go Up
Jump to: