I am working with a Beagle Bone serial port. I made a C program to read some data sent by another board; However, something that I cannot understand is happening.
When I run the program it does not work if I don't run the "screen" command first (screen /dev/ttyOX). I would like to know from the experts, what is happening and why I need to run "screen" first. Can you help me with this?
My code looks like:
#include <stdio.h> #include <fcntl.h> #include <termios.h> #include <unistd.h> #include <errno.h> struct termios SerialPortSettings; static int configureUART_Port(const char* pDevice); void main() { int lSerialPort; lSerialPort = configureUART_Port("/dev/ttyO4"); char buf[2]=""; int bytesRead = 0; printf("Attempt to read\r\n"); bytesRead = read(lSerialPort, buf, 2); printf("BytesRead : %d\r\n",bytesRead); close(lSerialPort); } static int configureUART_Port(const char *pDevice){ static int lSerialPort = 0; lSerialPort = open(pDevice, O_RDWR | O_NOCTTY); if(lSerialPort == -1) printf("\n Error! in Opening ttyO4\n"); else{ printf("Port Successfully oppened\r\n"); tcgetattr(lSerialPort, &SerialPortSettings); /*Setea La velocidad Tx/Rx*/ cfsetispeed(&SerialPortSettings,B9600); cfsetospeed(&SerialPortSettings,B9600); /*Parity Bit 0 HC-05*/ SerialPortSettings.c_cflag &= ~PARENB; SerialPortSettings.c_cflag &= ~CSTOPB; /*Configure 8 bits por byte*/ SerialPortSettings.c_cflag &= CS8; SerialPortSettings.c_cflag &= ~CSIZE; /* Clears the Mask */ SerialPortSettings.c_cflag |= CS8; SerialPortSettings.c_cflag &= ~CRTSCTS; SerialPortSettings.c_cflag |= CREAD | CLOCAL; SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); SerialPortSettings.c_cc[VMIN] = 2; /*Wait for data*/ SerialPortSettings.c_cc[VTIME] = 0; /*Update configuration*/ tcsetattr(lSerialPort,TCSANOW,&SerialPortSettings); } return lSerialPort; }