Aadishri Aadishri - 1 month ago 14
C Question

cannot read but can write on serial port through Android Emulator

I am working on a program that is communicating with serial port over USB through Android emulator.

emulator -qemu -serial /dev/ttyUSB0


The emulator is able to open the port and write into it. However, read is not happening.

The program has a timeout for read maintained by a timer. The read happens in a separate 'read' thread. The main thread has a
socketpair
fd pair to signal the read-thread that the serial port is closed post timeout. In the read-thread, polling happens(
poll()
function call) over the 2 file-descriptors: one is serial port fd, the other is one of the socketpair.

The board that I have connected to works fine with sending commands over 'cutecom'

The poll never succeeds for serial port. However, poll succeeds for 'socketpair'ed fd and the thread ends on a close-signal sent from main-thread post timeout.

Ouestions:


  1. Are there any special settings for read as even loop-back fails

  2. Are there differences between settings for read and write on a serial port?


Answer

struct termios settings are a must with ioctl calls. The following changes got my read call working! Still unsure how write call was successful...

struct termios termios;
int ret;

tcflush(fd, TCIOFLUSH);

tcgetattr(fd, &termios);

cfmakeraw(&termios);

termios.c_cflag |= (CLOCAL | CREAD | CRTSCTS | CS8);
tcsetattr(fd, TCSANOW, &termios);

/* tcgetattr(fd, &termios); */

tcflush(fd, TCIOFLUSH);