Sivalingaraja
Sivalingaraja

Reputation: 91

How to set parity, stop bit and data bits for serial port in Android using android-serialport-api library?

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.

Upvotes: 3

Views: 4344

Answers (2)

acassis
acassis

Reputation: 10405

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.

Upvotes: 2

disc
disc

Reputation: 21

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

Upvotes: 2

Related Questions