Setting the baud rate to read /dev/ttyO1

2,094 views
Skip to first unread message

Floof

unread,
Oct 4, 2012, 4:05:16 AM10/4/12
to beagl...@googlegroups.com
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?

jose antonio rubio

unread,
Oct 4, 2012, 4:44:08 PM10/4/12
to beagl...@googlegroups.com
You needs read the serial howto over linux

2012/10/4 Floof <jaz...@gmail.com>
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?

--
 
 

Floof

unread,
Oct 5, 2012, 1:09:44 AM10/5/12
to beagl...@googlegroups.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:
______________________________________________________
make all
Building target: UARTComm
Invoking: GCC C++ Linker
arm-linux-gnueabi-g++ -L/usr/arm-linux-gnueabi/lib -o "UARTComm"  ./src/UARTComm.o   -lserial
/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../../arm-linux-gnueabi/bin/ld: skipping incompatible /usr/lib/libserial.so when searching for -lserial
/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../../arm-linux-gnueabi/bin/ld: cannot find -lserial
______________________________________________________

Can anyone tell me what this means? I'm willing to use the aforementioned page, but I really want to use a library that makes it easy for me.
Message has been deleted

Floof

unread,
Oct 8, 2012, 4:00:37 PM10/8/12
to beagl...@googlegroups.com
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 <iostream>
#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!

Jan Axelson

unread,
Oct 8, 2012, 5:11:07 PM10/8/12
to beagl...@googlegroups.com
Use tcsetattr?

I have a serial port example here:

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

Jan Axelson
www.Lvr.com
>--
>
>

jose antonio rubio

unread,
Oct 8, 2012, 5:36:05 PM10/8/12
to beagl...@googlegroups.com
You read a arduino? (tincantools expansion board?

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



Floof

unread,
Oct 8, 2012, 7:46:57 PM10/8/12
to beagl...@googlegroups.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. 

Floof

unread,
Oct 9, 2012, 6:25:44 AM10/9/12
to beagl...@googlegroups.com
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?


liyaoshi

unread,
Oct 9, 2012, 7:11:47 AM10/9/12
to beagl...@googlegroups.com
where is tcsetattr ?


2012/10/9 Floof <jaz...@gmail.com>
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?


--
 
 

Floof

unread,
Oct 9, 2012, 11:45:35 AM10/9/12
to beagl...@googlegroups.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.


liyaoshi

unread,
Oct 9, 2012, 9:20:11 PM10/9/12
to beagl...@googlegroups.com
You are welcome

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


--
 
 

Message has been deleted
Message has been deleted

Floof

unread,
Oct 17, 2012, 4:47:29 PM10/17/12
to beagl...@googlegroups.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:
if ((fd = open("/dev/ttyO1", O_RDWR | O_NOCTTY)) < 0){
                return false;
        }
        if (tcgetattr(fd, &PORT) != 0){ //Obtain current terminal device settings in order to modify them at a later time.
                return false;
        }
        if (cfsetispeed(&PORT, B115200) < 0){
                return false;
        }
        PORT.c_iflag = 0;
        PORT.c_oflag = 0;
        PORT.c_lflag = 0;
        PORT.c_cc[VMIN] = 15;
        PORT.c_cc[VTIME] = 0;
        tcsetattr(fd, TCSANOW, &UM6); //Set newly-modified attributes

        bytes
-read = read(fd, buffer, buffer_size); //This is where it hangs the first time it's used.
        close
(fd);

Floof

unread,
Oct 17, 2012, 6:21:12 PM10/17/12
to beagl...@googlegroups.com
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? 
Reply all
Reply to author
Forward
0 new messages