Pages: [1]   Go Down
Author Topic: Arduino Serial Monitor and C++ stand along app reading serial print  (Read 611 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

Pages: [1]   Go Up
Jump to: