0

We are testing serial port communication. There are two tty nodes /dev/ttyUSB8 and /dev/ttyUSB9 on the device.

When I transmit buffer from /dev/ttyUSB8 to /dev/ttyUSB9 I don't receive data on the /dev/ttyUSB9 read call if the buffer doesn't contain new line.

Transmit Code

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>

void write_func()
{
    int fdw;
    int i;
    fdw = open("/dev/ttyUSB8", O_RDWR);
    printf("fdw : %d\n",fdw);

    printf("%ld\n", write(fdw, "Hello", 6));
    close(fdw);
}

int main()
{
    int i;
    write_func();
    return 0;
}

Receive Code

void read_thread()
{

    int fdr;
    char buf[] = "NoData";

    fdr = open("/dev/ttyUSB9", O_RDWR);
    printf("fdr : %d\n",fdr);

    printf("%s: %ld\n", __func__, read(fdr, buf, 6));

    printf("%s\n", buf);
    close(fdr);
}

int main()
{
    int i;
    read_thread();
    return 0;
}

I don't receive data with the above call, but when I add '\n in the write call i get data in the read block call.

 printf("%ld\n", write(fdw, "Hello\n", 7));

What is the significance of new line character in this..

Update:

I added the code to reset canonical mode, still it didn't work:

void write_thread()
{

    int fdw;
    int i;
    struct termios config;
    fdw = open("/dev/ttymib24", O_RDWR);
    printf("fdw : %d\n",fdw);
    tcgetattr(fdw, &config);
    config.c_lflag &= ~ICANON;
    tcsetattr(fdw, TCSANOW, &config);    
    printf("%ld\n", write(fdw, "Hello", 6));
    close(fdw);
}
md.jamal
  • 4,067
  • 8
  • 45
  • 108

1 Answers1

1

Your tty is probably in canonical mode.

Try to reset ICANON by using tcsetattr(). Something like this:

struct termios termiosv;
tcgetattr(fd, &termiosv);
termiosv.c_lflag &= ~ICANON;
tcsetattr(fd, TCSANOW, &termiosv);

More information in man page of termios:

   In canonical mode:

   * Input is made available line by line.  An input line is available
     when one of the line delimiters is typed (NL, EOL, EOL2; or EOF at
     the start of line).  Except in the case of EOF, the line delimiter
     is included in the buffer returned by read(2).
SKi
  • 8,007
  • 2
  • 26
  • 57
  • @md.jamal Did you read the snippet from the man page? **Input** is affected. You need to add it into the read thread. – Gerhardh Nov 09 '18 at 07:31