Arduino and PC two way communication in C++.

Hey!

I am trying to communicate with arduino. I want to use C++ in Ubuntu. I can send strings to Arduino, but not from Arduino to pc. To send strings to Arduino. I use code like this.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream arduino("/dev/ttyACM0");
    arduino << "Led on\n";
    arduino.close();

    return 0;
}

And for arduino I used SerialEvent.http://arduino.cc/en/Tutorial/SerialEvent

And the question is?

Mark

How to read arduino sent string from pc in c++?

mikk: How to read arduino sent string from pc in c++?

In a similar way to reading the incoming data with the Arduino I would think.

If there is serial data available then read a byte and process or store it until either a fixed number of bytes have been read or preferably until an end of message marker, perhaps CR or LF, is encountered. Your question is not really about the Arduino. more about C++ on the PC.

Please post your Arduino code for reading the incoming serial data and transmitting the outgoing serial data, which could be as simple as

Serial.println(data);

This is my Arduino code for reading.

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString); 
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    } 
  }
}

This is code for sending string.

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println("Hello");
  delay (100);
}

How do I know when serial data is available?

This forum deals with programs running ON the arduino's and does NOT deal with stuff running on the PC. But theres lots of stuff around about reading from comms ports and other devices on the PC. But it has nothing to do with arduino programming, the source of the data is irrelevant!.

Mark