Posts: 1
Joined: Thu Jan 21, 2016 8:17 pm

Serial device reads CR and overwrites data

Thu Jan 21, 2016 8:40 pm

First off I'm new to raspberrypi/linux etc, I've been working with arduino and trying to make my program faster with the Pi.

I'm working with UART device sending and receiving data. I can open the port, send data, receive data, and close the port without a problem. I'm having trouble with ignoring the CR. So I get data like this from the device:

AA BB CC \r>

But what is getting stored is:


Here is the code snippet I'm working with.

Code: Select all

#include <iostream>
#include <cstdio>
#include <cerrno>
#include <string>
#include <cstdlib>
#include <wiringPi.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

int fd;
char rxData[256];

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

tcgetattr(fd, &options);
options.c_iflag |= IGNCR;

write(fd, sendString , sizeof(sendString));
read (fd, rxData, sizeof(rxData));
cout << rxData << endl;

Any advice would be appreciated.

Posts: 35
Joined: Sun Feb 02, 2014 5:41 pm

Re: Serial device reads CR and overwrites data

Fri Jan 22, 2016 1:26 pm

You will need to set the termios options.

Code: Select all

tcsetattr( fd, TCSANOW, &options );

Return to “C/C++”