3
votes

I need to read from serial port. And needs to open the serial port with specified baud rate, parity, data bits and stop bit.

I have checked android-serialport-api but it does not have way to initialize with those settings.

Can anybody know how to resolve this?

Any help or suggestion will be very helpful.

Thanks in advance.

2

2 Answers

2
votes

The baud rate can be set in the constructor of the SerialPort class in java.

To set other parameters I found only the way to set it inside the jni code. In the android-serialport-api project the is a folder jni with the c-sources that communicate with the underlying driver. There you can configure the termios structure. E.g. you can enable parity by adding the line cfg.c_cflag |= PARENB;

A description how to set other parameters of the termios interface can be found here: http://en.wikibooks.org/wiki/Serial_Programming/termios

When you have made your changes inside the c code you have to compile it with the android NDK. First download the ndk from the android web site, and start in the android-serialport-api directory the ndk-build programm. This will compile your C code and produce the libraries needed.

A description can be found here: http://developer.android.com/sdk/ndk/index.html

2
votes

I did this way to enable EVEN Parity:

----------------------- bionic/libc/include/termios.h ------------------------
+static __inline__ void cfmakepareven(struct termios *s)
+{
+    s->c_iflag &= ~(IGNPAR | PARMRK); 
+    s->c_iflag |= INPCK;
+    s->c_cflag |= PARENB;
+    s->c_cflag &= ~PARODD;
+}
+

--------------- frameworks/base/core/jni/android_serial_port.cpp ---------------
        cfmakeraw(&cfg);
        cfsetispeed(&cfg, speed);
        cfsetospeed(&cfg, speed);
+       cfmakepareven(&cfg);

        if (tcsetattr(fd, TCSANOW, &cfg))
        {

I hope you get it working.