除数不适用于Android设备上串行端口的波特率

时间:2018-08-10 07:10:34

标签: android c android-ndk serial-port java-native-interface

我正在尝试使用ndk和jni在Android设备(6.0)上以3.25Mb / s的速度运行串行端口。它不是电话,而是物联网设备,具有两个内置UART,能够以6.5Mb / s的速度运行,但是当我使用此代码时:

#include <stdio.h>
#include <termio.h>
#include <jni.h>
#include <android/log.h>
#include <termios.h>
#include <sys/fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <linux/serial.h>

#define APPNAME "App"
#define logcat(...)  __android_log_print(ANDROID_LOG_VERBOSE, APPNAME, __VA_ARGS__)


static int rate_to_constant(int baudrate) {
#define B(x) case x: return B##x
    switch(baudrate) {
        B(50);     B(75);     B(110);    B(134);    B(150);
        B(200);    B(300);    B(600);    B(1200);   B(1800);
        B(2400);   B(4800);   B(9600);   B(19200);  B(38400);
        B(57600);  B(115200); B(230400); B(460800); B(500000);
        B(576000); B(921600); B(1000000);B(1152000);B(1500000);
        B(3000000);B(3500000);
        default: return 0;
    }
#undef B
}

/* Open serial port in raw mode, with custom baudrate if necessary */
int serial_open(const char *device, int rate)
{
    struct termios options;
    struct serial_struct serinfo;
    int fd;
    int speed = 0;
/* Open and configure serial port */
if ((fd = open(device,O_RDWR|O_NOCTTY)) == -1)
    return -1;

speed = rate_to_constant(rate);

if (speed == 0) {
    /* Custom divisor */
    serinfo.reserved_char[0] = 0;
    if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
        return -1;
    serinfo.flags &= ~ASYNC_SPD_MASK;
    serinfo.flags |= ASYNC_SPD_CUST;
    serinfo.custom_divisor = (serinfo.baud_base + (rate / 2)) / rate;
    if (serinfo.custom_divisor < 1)
        serinfo.custom_divisor = 1;
    if (ioctl(fd, TIOCSSERIAL, &serinfo) < 0)
        return -1;
    if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
        return -1;
    if (serinfo.custom_divisor * rate != serinfo.baud_base) {
        logcat("actual baudrate is %d / %d = %f",
              serinfo.baud_base, serinfo.custom_divisor,
              (float)serinfo.baud_base / serinfo.custom_divisor);
    }
}

fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &options);
cfsetispeed(&options, speed ?: B38400);
cfsetospeed(&options, speed ?: B38400);
cfmakeraw(&options);
options.c_cflag |= (CLOCAL | CREAD | BOTHER);
options.c_cflag &= ~CRTSCTS;
if (tcsetattr(fd, TCSANOW, &options) != 0)
    return -1;

return fd;
}



JNIEXPORT jint JNICALL
Java_carlson_fix1_SerialThread_server( JNIEnv* env,
                                      jobject thiz ) {

    char *portname = "/dev/ttyS1";
    int fd, wlen;

    fd = serial_open(portname, 3250000);
    logcat("%i", fd);
    wlen = write(fd, "CLST\n", 6);

    // simple noncanonical input
    while (1) {
        tcdrain(fd);    // delay for output
        unsigned char buf[100];
        int rdlen;
        rdlen = read(fd, buf, sizeof(buf) - 1);
        logcat("reading: %i", rdlen);
        if (rdlen > 0) {
            for (int i = 0; i<rdlen; i++){
                logcat("%c", buf[i]);
            }
        } else if (rdlen < 0) {
            logcat("Error from read: %d: %s\n", rdlen, strerror(errno));
            return -1;
        }
        // repeat read to get full message
    }

}

串行端口以6.5Mb / s(串行端口的基本速率)运行。似乎除数未正确应用。有我缺少的设置吗?无论我尝试以哪种自定义速度运行,都会发生这种情况,但是值得注意的是,预定义的速度可以工作,并且在输出端有一个示波器来确认速度。

/sys/class/tty/ttyS1/custom_divisor设置为2,但没有正确应用。

0 个答案:

没有答案