I read the SerialHOWTO, and it was of great help. I also looked up the termios, Serial Programming tutorial in wikibooks, and based my code on what I saw there. I think it’s appropriate to provide some background on my plight:
I’m trying to read bytes serially from a gyroscope on a chip. The gyro sends out the bytes ‘s’, ‘n’, ‘p’ (i.e., start new packet) to signal incoming data. The gyro transmission details: 115200 baud, 1 stop bit, and no parity.
The uartInit() function works properly. I used the minicom command in terminal to both set the incoming baud rate and observe the data going into Bone’s receiver port, and I see data that I like. Problem is, I don’t know how to use the minicom command in the program to set the baud rate, so I tried writing this code instead. I am sure the problem lies with the readIMU() function, but I don’t know how to fix it.
`
#include
#include <stdio.h>
#include <string.h>
#include <termios.h> //Enables us to set baud rate for RX/TX seperately
#include <fcntl.h> //Enables use of flags to modify open(), read(), write() functions
#include <unistd.h> //Enables use of open(), read(), write()
#define STRING_MAX 64
#define BUFFER_MAX 155
#define BAUD 115200
using namespace std;
int uartInit(void);
int readIMU(void);
int main() {
readIMU();
cout << “Done” << endl;
return 0;
}
int readIMU(void){
struct termios config;
char BUFFER[50];
int fd, bytes_read;
if ((fd = open(“/dev/ttyO1”, O_RDWR | O_NOCTTY)) < 0){ //Opening receiver binary file.
cout << “Could not open port.” << endl;
return fd;
}
if (tcgetattr(fd, &config) != 0){ //Seen this done in other programs, decided to include it.
return fd;
}
cout << “File opened is " << sizeof(fd) << " bytes long” << endl;
if (cfsetispeed(&config, B115200) < 0){
cout << “Input baud rate not successfully set.” << endl;
}
config.c_iflag = 0;
config.c_oflag = 0;
config.c_cflag |= CS8; //Read 8 bits at a time, 1 stop bit.
config.c_lflag = 0;
config.c_cc[VMIN] = 15; //Minimum # of characters before reading.
config.c_cc[VTIME] = 0;
bytes_read = read(fd, BUFFER, 20); //Read incoming bytes
close(fd);
for (int i = 0; i < 20; i++){
cout << “\t” << BUFFER[i] << endl;
}
return 0;
}
`
After I ran this code a couple of times, it would take a lot of time before spitting some characters out, along with a lil blurb at the end:
The buffer contains:
�
x
`
�
�
H
�
�
�
�
X
�
�
?
Done
*** stack smashing detected ***: ./Test terminated
If anyone can provide insight on what I’m doing wrong, or how to fix this problem, I would greatly appreciate it!