Setting the baud rate to read /dev/ttyO1

Hello, I’ve been toying with the UARTs, and I have managed to properly set the TX/RX for both UART1 & UART2. I’ve plugged in a sensor to UART1 and can read data; the problem is that I have to manually set the baud rate to 115200 using ‘minicom’ in the BeagleBone console. I want to write a bit of C++ that sets the baud rate automatically, but so far no luck. Does anyone have any insight regarding this matter?

tl;dr: How can I set the baud rate in a .cpp file instead of using ‘minicom’ in the terminal for the 'Bone?

You needs read the serial howto over linux

2012/10/4 Floof <jaztin@gmail.com>

I found some examples after searching for them (on the same site). It contains a wealth of information, and I appreciate your input! However

I also found something called LibSerial which, after looking at its sourceforge site, looks a heck of a lot easier to use. I installed it through Ubuntu Software Center, whipped-up some code, included the .so file and the folder in which the header was located in the G++ linker and got the following error:

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!

Use tcsetattr?

I have a serial port example here:

http://www.lvr.com/beagleboard.htm

Jan Axelson
www.Lvr.com

You read a arduino? (tincantools expansion board?

2012/10/8 Jan Axelson <jan@lvr.com>

Jose, I’m reading this sensor, plugged directly into the BeagleBone.

Jan, thanks for the idea. I’ll try it when I get the chance.

Jan, I’m currently looking at usb_serial_port.c. I see that you have configured the termios structure such that

`
options.c_cflag |= (CLOCAL | CREAD);

`

I understand that CREAD enables the receiver, thought I initially thought that would be redundant, which is why I didn’t include that in the first place. Second, after some further reading, I am unsure if I should include CLOCAL. I’ve read that that if the CLOCAL flag is set, then the call to open() will take place regardless of a connection being present. Does this mean just that: the tty file will be opened regardless if open() returns > 0?

where is tcsetattr ?

2012/10/9 Floof <jaztin@gmail.com>

Liyaoshi,

It’s a couple lines below
`
config.c_cflag |= (CLOCAL | CREAD);

`

I thought I would just inquire about the appropriate usage of those flags, because I don’t like the idea of not knowing how they work, since I see them used only sometimes and not other times. Anyway…

Jan, I included

`
tcsetattr(fd, TCSANOW, &config);

`
in the program, and I am seeing some data that I like! I really appreciate your input. Now I’m off to write a function that polls the sensor for data, that should be fun as well.

You are welcome

2012/10/9 Floof <jaztin@gmail.com>

So it’s been a while, and I’m still tinkering with the program. I’ve had good progress using termios.h, but I’m still encountering problems. I am polling my sensor, and it sends out 15 bytes (exactly what I want). I thought I would set PORT.c_cc[VMIN] = 15 so that the program would read the buffer after it received the data I wanted. The problem is that the program freezes the first time I’m trying to read the buffer, unless I instead set PORT.c_cc[VTIME]= 0, introducing a time delay before reading. This produces the problem that the program ends up reading bytes that shouldn’t be there in the first place. For example, a desired read would fill my read buffer as such:

buffer ={'s','n','p', dat1, dat2, dat3, dat4};

but introducing the time delay causes this:

buffer ={randomdata1,randomdata2,randomdat3,'s', 'n','p', dat1};

Does anyone have an idea of what’s going on or how to avoid this problem?

Portion of program with problem:

`

I should add that I’m executing the program in the Terminal. So the first time I run the program, it polls the sensor for data. Then when it tries to read the receiver buffer, it hangs, then I press CTRL+Z to quit the program. Then I run the program again, and it retrieves the data I want. In a nutshell, it seems that it’s retrieving the data from a previous data request because it reads the buffer too soon.

This is odd because I specified a minimum # of 15 bytes to read from the buffer with PORT.c_cc[VMIN] = 15; After reading the linux.die.net page on termios.h, it seems that PORT.c_cc[VMIN] = 15 is not working the way it should. Is this the case, or did I simply configure the settings incorrectly?