0

Hey I have problems with receiving strings from Arduino. I am running on linux and I want to use C++ fotr that. I an easily send strings from C++ code to arduino. For that I use C++ code like this.

#include <iostream>
#include <fstream>

using namespace std;

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

    return 0;
}

So how can I receive strings from Arduino?

mikk
  • 1
  • 1
  • 4

3 Answers3

2

I'm not an Arduino expert, but from your code I concluded:

  • You are using a serial interface to send data
  • You should connect the serial interface to your computer (with a traditional serial cable, or USB)
  • Write a C++ app, that opens and received data from serial port. See this!
  • Find out from Arduino specs, what serial communication parameters are used by Arduino (stop bit, parity bits, baudrate etc) and use these parameters to configure the serial port in your C++ application!

Hope that helps!

Adam Bartha
  • 373
  • 1
  • 12
  • I dont't see anywhere where he/she receives data. I can send data to Arduino, but I can't receive data from Arduino. – mikk Jun 13 '14 at 12:26
  • Yes, you are right! That code, only detects that, there are bytes to be received (using select system call). Use read(fd, buffer, n) in order to receive the bytes. See read spec: [read](http://linux.about.com/od/commands/l/blcmdl2_read.htm). Your receive c++ code should call select and read methods in a loop – Adam Bartha Jun 13 '14 at 12:50
0

Use boost.asio to communicate with a serial device and C++. It works like a charm and is pretty easy to use. See: http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/overview/serial_ports.html and this: Reading from serial port with Boost Asio

Community
  • 1
  • 1
clambake
  • 772
  • 4
  • 15
  • If I use boost asio, then my console output is every time different. I send "Data\n" from Arduino to pc. But in console I get sometimes nothing, sometimes only some letters and so on. – mikk Jun 13 '14 at 14:38
-1

The folowing code waits for a response from an arduino. If the response contains "Done" it returns 1. If it didn't find it within the given timeout, it returns -1.

It should not prove that hard to change this code to fit your needs.

int Serial::waitForResponse()
{
    const int buffSize = 1024;
    char bufferChar[buffSize] = {'\0'};
    int counter = 0;
    std::string wholeAnswer = "";

    int noDataTime = 0;

    while(wholeAnswer.find("Done") == std::string::npos) //Done string was found.
    {
        if(noDataTime > 10000)
        {
            std::cout << "timeout" << std::endl;
            return -1;
        }
        counter = read(this->hSerial, bufferChar, buffSize - 1);

        if(counter > 0)
        {
            noDataTime = 0;
            bufferChar[counter] = '\0';
            wholeAnswer += std::string(bufferChar);
        } else
        {
            noDataTime++;
            usleep(1000);
        }
    }
    if(!wholeAnswer.empty())
    {
        return 1;
    } else
    {
        return -1;
    }
Nallath
  • 2,100
  • 20
  • 37
  • What libraries have you included? – mikk Jun 13 '14 at 13:33
  • #include #include #include #include #include But its part of a larger whole, so could well be that there some of em are redundant. – Nallath Jun 13 '14 at 13:35
  • I get error: invalid use of 'this' in non-member function. – mikk Jun 13 '14 at 13:39
  • Like i said; its part of a bigger piece of code. The hserial in this case is an int, which is returned by the open command (which is used to open the connection). – Nallath Jun 13 '14 at 13:50
  • If I use boost asio, then my console output is every time different. I send "Data\n" from Arduino to pc. But in console I get sometimes nothing, sometimes only some letters and so on. – mikk Jun 13 '14 at 15:50