串口上不常见的波特率 - Linux

问题描述 投票:0回答:1

我正在尝试制作一个程序来读取串口。在这个端口上,我收到波特率为875000的数据。这种情况并不常见,我没有成功修改它。我做了一个小C程序来做到这一点,但它不适用于875000 ...这里的代码的一部分与串口的编程:

    #include <stdio.h>
    #include <stdlib.h>
    #include <asm/termios.h>
    #include <sys/fcntl.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include "test.h"

    void read_Serial_Port(const char* DEVICE_PORT)
    {
        int file;
        struct ktermios options;
        unsigned int nCountMax = 60;
        bool b;

        file = open(DEVICE_PORT, O_RDONLY | O_NOCTTY | O_NDELAY);

        if(file == -1){perror("Unable to open the serial port\n");}
        //printf("Serial port open successful !\n");
        int speed = atoi("875000");

        ioctl(file, TCGETS2, &options);     


        options.c_ispeed = speed;
        options.c_ospeed = speed;

        options.c_cflag |= (CLOCAL | CREAD);              
        options.c_cflag |= PARENB;                  
        options.c_cflag |= PARODD; 
        options.c_cflag &= ~CBAUD;

        options.c_cflag |= BOTHER;                                          
        options.c_cflag &= ~CSIZE;                      
        options.c_cflag |= CS8;     

        ioctl(file, TCSETS2, &options); 


        //printf("Reading serial port ...\n\n"); 
        b = readMessage(file, nCountMax);

        if (b == 0){printf("Error while reading serial port\n");}
        //else printf("\nSerial port read successful\n");

        close(file);
        //printf("Serial port closed\n");
    };
c linux serial-port baud-rate
1个回答
-1
投票

最后我发现了另一个关于stackoverflow的话题,它已经完成并解决了我的问题:How to set baud rate to 307200 on Linux?

这是我修改的代码:

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(2000000);B(2500000);B(3000000);B(3500000);B(4000000); 
default: return 0;
}
#undef B
}    


int Custom_Baudrate(const char* Device, int rate)
  {

/*Declaration of all the variables needed*/
struct termios2 options;
struct serial_struct serinfo;
int file=-1;
int speed = 0;
int r=rate;



/* Open and configure serial port */
file = open(Device,O_RDWR|O_NOCTTY);

if(file==-1){printf("\nERROR : Unable to open the serial port\n\n");return 1;}

speed = rate_to_constant(r);



/*Find best Baudrate*/
if (speed == 0) {

    /* Custom divisor */
    serinfo.reserved_char[0] = 0;
    if (ioctl(file, TIOCGSERIAL, &serinfo) < 0) file=-1;
    serinfo.flags &= ~ASYNC_SPD_MASK;
    serinfo.flags |= ASYNC_SPD_CUST;

    serinfo.custom_divisor = ((serinfo.baud_base + (r / 2)) / r);

    if (serinfo.custom_divisor < 1) 
        serinfo.custom_divisor = 1;
    if (ioctl(file, TIOCSSERIAL, &serinfo) < 0) file=-1;
    if (ioctl(file, TIOCGSERIAL, &serinfo) < 0) file=-1;
    if (serinfo.custom_divisor * r != serinfo.baud_base) {
        warnx("actual baudrate is %d / %d = %f",
              serinfo.baud_base, serinfo.custom_divisor,
              (float)
              serinfo.baud_base / serinfo.custom_divisor);
    }
}



/*Set the best Baudrate (if desired baudrate is unvailable, it's set automatically at 38400)*/
fcntl(file, F_SETFL, 0);
tcgetattr(file, &options);
cfsetispeed(&options, speed ?: B38400);
cfsetospeed(&options, speed ?: B38400);
cfmakeraw(&options);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~CRTSCTS;
if (tcsetattr(file, TCSANOW, &options) != 0) file=-1;


/*Read the serial port*/
communicate_Serial_Port(file);

close(file);
return 1;
}

在此代码中,您只需要精确的波特率,它就可以找到最接近的值。此值基于设备的“基本波特率”,并搜索除数以设置最佳波特率。但是,某些波特率应始终不可用,因此该程序将38400作为基础(这是一个选择)。我用几个波特率测试它,它总是有效。

我是stackoverflow的新手,我希望这篇文章能正确完成这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.