0

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;    
}

3
  • Welcome to Stack Exchange! Could you please elaborate a little on what is happening when "it doesn't work" and what you expect to happen? – Martin Konrad May 13 '20 at 1:28
  • I have never heard of ttyOx - shouldn't it say ttySx if you want to open the serial port? – Martin Konrad May 13 '20 at 1:28
  • Sorry I will change the questions title, I am not pretty sure I am new in working with linux, I think that ttyO4 its okay since when I run 'screen /dev/ttyO4' I can see the data send from the other board, I read that screen keeps running in BG, and after this if I run my programa I can see the data. What I would Like to know is why this happens? – nicolas espla May 13 '20 at 1:32
0

I found the solution: when running screen, it enables the raw mode. I changed the code of my program to enable it and it works.

Btw, ttyOx is a symbolic link to ttySx.

Thank you

1
  • Welcome to the site, and congratulations on solving the problem yourself. For the sake of others facing a similar problem, would you mind editing your answer to include an explanation how you had to change your code to accomplish this? – AdminBee May 13 '20 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.