Pages: [1]   Go Down
Author Topic: Arduino Serial Monitor and C++ stand along app reading serial print  (Read 1605 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've looked around for a few days and have come up empty in trying to find an example or explanation to answer my questions:

I am writing a C++ application that will read the serial print data from my Arduino. My program will then parse that data and run a simulator from the parsed commands. I have successfully been able to read the serial data from the USB port but only when I click on the Serial Monitor icon when I open the Arduino software.

I would like to know what is being sent to the Arduino so that it will reset and begin the Serial print.

I would also like some pointers as to which options I need to set with my program for the USB port. I am getting some strange characters on the print from the Arduino instead of just plain text. I thought it would probably be something that I set wrong in the settings in the termios structure.

The idea I am building is, due to a failure to make my drone fly with the communication I have been able to send over WiFi, that I will build a simulator to show that the program I wrote for my Arduino functions as described. So for this application I decided it would be easiest, and probably best displayed, using the Arduino board to run the program with the sensors attached to it and feeding all the commands that were to be sent to the drone through Serial.println statements. Then parsing those statements and having a graphical representation built in QT with C++ in Ubuntu display tilt, rotation, and flight direction. Ideally this will run on my laptop as a stand along application so I don't have to boot up the Arduino IDE and start the serial monitor from there, although I won't get too particular about it since I only have about 2 weeks, with about an hour or 2 a day at most, to devote to this before I hit the deadline!

Any help or direction is appreciated,
Postholes
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've been able to connect to and get the Serial data from the Arduino. When I use, in my Ubuntu terminal, cu -l /dev/ttyUSB0 it prints the information correctly. When I run my c++ program no matter how I have managed to control the USB settings in the termios struct I get a whole mess of gibberish trying to print chars to the screen using something like:

cout << data << endl;
OR
(In a for loop)
cout << (char)data;
cout << data;
printf("%s", data);
printf("%c", data);

I'm getting the same junk/gibberish chars. I guess it could be possible to have it run, write to a file then read that file but reading and writing at the same time would call for much greater difficulty requiring mutex locks to make sure I don't run into the end of the file.

I will paste the code I am using below if you care to have a look see:

Code:
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <stdio.h>
using namespace std;

#define bufferSize 100

int open_port(void)
{
    int fd;

    fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY| O_NDELAY);

    if (fd == -1)
    {
        // Could not open port so send error
        perror("open_port: Unable to open /dev/ttyS0 - ");
    }
    else
    {
        // port is open and connected to fd
        fcntl(fd, F_SETFL, 0); // if don't want to block use FNDELAY
        return (fd);
    }

    return (fd);
}

int set_port(int fd)
{
    struct termios options;
    tcgetattr(fd, &options);            // get current options for port

    cfsetispeed(&options, B9600);        // set input baud rate
    cfsetospeed(&options, B9600);        // set output baud rate
    options.c_cflag |= (CLOCAL | CREAD); // enable receiver and set local mode

    // No parity and 8 bit char definition with 1 stop bit
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;

//    options.c_cflag &= ~CRTSCTS;         // Turn off flow of hardware control

    return tcsetattr(fd, TCSANOW, &options);
}

int main() {

unsigned char data[bufferSize];

int fd = open_port();
set_port(fd);

while (read(fd, &data[0], sizeof(data)) != -1)
{
//     cout << data << endl; // this prints the same gibberish as the for loop print statement.

    int lastCheck = sizeof(data);
    int i = 0;

    for (i = 0; i < lastCheck; i++)
    {
      cout << data[i];
    }
}

if (read(fd, &data[0], sizeof(data)) == -1)
{
    cout << "Reached end of USB read" << endl;
}

return 0;
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you solved this? Or found a way around it perhaps?  I've run into (a similar) problem where the input digits (from the ADC) are all delayed after 2seconds of running, making my code useless. Nonetheless, when verified with Arduino's Serial Monitor the data's displayed correctly.

Changing baud rate settings doesn't affect anything - same result.
I'm quite sure it has something to do with the termios settings being set.

Much appreciated
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would like to know what is being sent to the Arduino so that it will reset and begin the Serial print.

 This part I can answer. Your application needs to toggle either the DTR or RTS control signals on the com port the application is using to talk to the attached arduino board. That will activate the auto-reset function on the board and it will restart the sketch after the bootloader times out waiting to see if there is a new upload sketch pending.

Lefty


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks  A LOT for your reply .. i'll see what I can do.

MY problem might be slightly different than the original post (not sure) since my values would be delayed, and by delayed I mean ..even if the Arduino reset button is pressed, values are still output on screen.
Then again, I still have to start the Arduino IDE for the first run after plugging in the Arduino, otherwise the strange characters appear. I'll see what I can do with DTR / RTS.

Kind Regards
Logged

UK
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6392
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote a demo in this Thread showing how to communicate with an Arduino using Python. If you get your C program to act like the Python program it should work.

...R
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Setting   
Code:
options.c_cc[VMIN]  = BufferSize;
(100 in this case) solves the problem and data is now displayed in chunks, of 100 chars obviously (not a problem, but for some reason now i can't print the output to file - using fstream (fout)  - .txt file remains empty)

Gonna have a look at your Python code just now... Thanks immensely for your help!
« Last Edit: May 02, 2014, 05:49:51 am by nichbug » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

All working - i managed btw smiley-wink

Anyone knows what's the maximum buffer size of an Arduino Duemilanove? ..and a reference to the source perhaps

Thanks !
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Anyone knows what's the maximum buffer size of an Arduino Duemilanove?
Yes.

If you want to know the actual maximum size, it helps to define which buffer you want to know the size of.

Quote
and a reference to the source perhaps
As opposed to the source itself? Why? You already have the source.
Logged

Pages: [1]   Go Up
Jump to: