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?
--
#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;
}
�
x
`
�
�
H
�
�
�
�
X
�
�
?
Done
*** stack smashing detected ***: ./Test terminated
options.c_cflag |= (CLOCAL | CREAD);
Jan, I'm currently looking at usb_serial_port.c. I see that you have configured the termios structure such thatoptions.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?
--
config.c_cflag |= (CLOCAL | CREAD);
tcsetattr(fd, TCSANOW, &config);
buffer ={'s','n','p', dat1, dat2, dat3, dat4};buffer ={randomdata1,randomdata2,randomdat3,'s', 'n','p', dat1}; 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);