I am trying to read data continuously from serial port and save it to a buf and/or to a file. Also this data is a stream of HEX characters and not ASCII. I am using a CHIP which runs debian. I have connected the the serial port(UART) on /dev/ttyS0 (this is where and how I will be reading data). I am trying to code it in C.

I am able to read some part of data but not sure what it is as i am not able to write it to a text file and also when I try to print in on the console, data is not in readable format as the data is in HEX and console prints strings. Is it possible to print HEX chars ?

Also when i try to read the port continuously, the buffer just holds the first value and displays(prints) the same value for all reads, but when i connect the data output to a putty window, i can see the data stream and also can view HEX characters too when viewing raw data using device monitor tool.

How can I store the data received continuously on serial port ?

#debian version
$ cat /etc/issue
Debian GNU/Linux 8 \n \l

$ cat /etc/debian_version

Code snippet:

char *portname = "/dev/ttyS0" //Serial port connected(UART)
int fd = open (portname, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0)
        perror ("error %d opening %s: %s", errno, portname, strerror (errno));
// *these two functions are used to set attributes for serial communication.*
set_interface_attribs (fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
set_blocking (fd, 0);                // set no blocking

char buf [100];
printf("Reading data on serial port");
int n;
    n = read (fd, &buf, sizeof(buf));  // read up to 100 characters if ready to read
return 0;

interface setting

    tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
    // disable IGNBRK for mismatched speed tests; otherwise receive break
    // as \000 chars
    tty.c_iflag &= ~IGNBRK;         // disable break processing
    tty.c_lflag = 0;                // no signaling chars, no echo,
                                    // no canonical processing
    tty.c_oflag = 0;                // no remapping, no delays
    tty.c_cc[VMIN]  = 0;            // read doesn't block
    tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

    tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

    tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                    // enable reading
    tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
    tty.c_cflag |= parity;
    tty.c_cflag &= ~CSTOPB;                 //one stop bit
    tty.c_cflag &= ~CRTSCTS;

Can anyone help !!!!!

Thanks in advance...

  • You could pipe the output to hexdump or xxd or reimplement what those tools already do in your program. Probably easier to save the raw bytes to a file, then use hexdump when necessary. – thrig Feb 28 '17 at 17:26
  • 1
    The answer below the answer you copied has code to print out bytes as hex digits. – Mark Plotnick Feb 28 '17 at 18:30
  • 1
    @SatoKatsura I am new to this. Sure will try to understand once i start getting familiar with the concepts. – sachin Feb 28 '17 at 23:07
  • @MarkPlotnick Thanks for the info. I just had looked into the accepted answer and was focusing more to find the solution rather than understanding each parameter's fucntionality. Will try to understand it. Thanks for your time and reply. – sachin Feb 28 '17 at 23:10

Your Answer

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

Browse other questions tagged or ask your own question.