RaspberryPi serial reading CR and causing begging of char array to be overwritten.

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:

>A BB CC

Here is the code snippet I'm working with.

#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];

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

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

write(fd, sendString , sizeof(sendString));
dealy(1);
read (fd, rxData, sizeof(rxData));
cout << rxData << endl;
Don't send the whole buffer, just send what you need. If you need to strip off \n, then ... I'm asuming sendString is a char array.
1
2
3
4
5
6
// Look for \n, replace it with null
if (const char* end = strchr(sendString, '\n'))
    *end = '\0';

// send the string with the null terminator
write(fd, sendString, strlen(sendString) + 1);
I was told I needed to use the following:
tcsetattr( fd, TCSANOW, &options );
and the input now ignores the CR.
Maybe, but there's nothing stopping you from stripping it off. As you can see, it's trivial.
Topic archived. No new replies allowed.