0

I have a small problem with read-in function in the c++. I'm trying to read in data available on UART's RxD port.

when running this code I'm getting result (read <0) hence it displays me an error msg error in read. what should be done to be able read in and display the data from read. I'm sure that there's data available on RxD port I have checked it with oscilloscope.

#include <iostream>
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <stdlib.h>
#include <sys/ioctl.h>

#define BAUDRATE B19200
#define PORT "/dev/ttyO4"
#define _POSIX_SOURCE 1

#define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;

void signal_handler_IO(int status);
int wait_flag = TRUE;



main ()
{
    int fd=0, res=0, result=0;


    char SYNC  [] = {0x55};
    char PID [] = {0x6A};

    struct termios oldtio, newtio;
    struct sigaction saio;
    char buff[255];

    fd = open(PORT, O_RDWR | O_NOCTTY | O_NONBLOCK);
    if (fd<0) {perror(PORT); exit(-1);}

    saio.sa_handler=signal_handler_IO;
    saio.sa_flags=0;
    saio.sa_restorer = NULL;
    sigaction(SIGIO, &saio,NULL);
    fcntl(fd, F_SETFL, FASYNC);
    tcgetattr(fd, &oldtio);

    newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
    newtio.c_iflag = IGNPAR;
    newtio.c_oflag = 0;
    newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    //newtio.c_lflag = 0;
    newtio.c_cc[VMIN]=1;
    newtio.c_cc[VTIME]=0;
    tcflush(fd, TCIFLUSH);
    tcsetattr(fd, TCSANOW, &newtio);

    ioctl(fd, TIOCSBRK);
    usleep(1300);

    ioctl(fd,TIOCCBRK);
    usleep(200);

    write(fd, SYNC, sizeof(SYNC));
    write(fd,PID, sizeof(PID));

    res = read(fd, buff,255);

    if (res < 0)
    {
        printf("read error\n");
    }
    else if (res ==0)
    {
        printf("read = 0 \n");
    }
   else
    {
        sprintf(result, "%x",res);
        buff[res]=0;
        printf(": %s :%d :%d\n", buff,result,res);

    }
    close (fd);

}

void signal_handler_IO(int status)
{
    printf("received the signal\n");
wait_flag=FALSE;
}
Anders
  • 35
  • 1
  • 3
  • 8
  • is erno set when the read fails? – Michael J May 05 '14 at 11:54
  • hello, no it's not set when it reads port. errno is only set when the program is opening the port with the open call, followed by error(PORT);. – Anders May 05 '14 at 12:10
  • What error message do you get? Do you have access rights to the device? – Alan Stokes May 05 '14 at 12:13
  • I thought you said open() was succeeding. Is the open() call failing? What is errno after open()? – Michael J May 05 '14 at 12:16
  • Yes I have access right to the device. I can easily write to the device. By doing write function in this code : write(fd, SYNC, sizeof(SYNC)); write(fd,PID, sizeof(PID)); I request from device to return it's current settings back to me. It's just when I do read it returns read < 0; meaning that I wasn't able to read anything in. – Anders May 05 '14 at 12:21
  • open is succeeding, and is not blocking. then I can write to the device as well, but I have a problems of reading from the device. – Anders May 05 '14 at 12:23
  • You've asked for non-blocking IO, but you don't seem to be waiting for data to arrive. – Alan Stokes May 05 '14 at 12:24
  • errno at the read is Interrupted system call. – Anders May 05 '14 at 12:26
  • so to wait for data to arrive I need to make a wait loop similar to http://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html – Anders May 05 '14 at 12:29
  • Structure **newtio** has not been properly initialized by a call to **tcgetattr()** prior to use. Your code performs full assignments. Refer to [Setting Terminal Modes Properly](http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_12.html#SEC237) on the proper technique. You need to check the return code of all system calls, such as **tc[gs]etattr(()** and **write()**, just like **open()** and **read()**. If you're going to add wait loops, then instead remove the O_NONBLOCK option. That link that you mention has poorly written examples; use at your own peril. – sawdust May 05 '14 at 17:51
  • Thank you for the help @sawdust, i have corrected the newtio settings. But I still cannot read in anything from the port, when it doesn't display errno=EINTR error i still see a blank screen. is there any guide online that could help me to get the read function working. – Anders May 06 '14 at 18:46
  • Why do you think you need asynchronous I/O? Your program defines and installs a signal handler, yet **wait_flag** is never used in **main**. Seems like you're just copying/reusing code without studying what the code does. Remove the `fcntl(fd, F_SETFL, FASYNC)` call, and things might work closer to what you expect. If not, then you should append the new version of your code to your original question. – sawdust May 06 '14 at 21:35

0 Answers0