首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何检测驱动程序是否不支持ioctl命令TIOCSSERIAL

如何检测驱动程序是否不支持ioctl命令TIOCSSERIAL
EN

Stack Overflow用户
提问于 2020-09-21 04:11:56
回答 1查看 470关注 0票数 0

我正在使用USB转RS-232串行适配器,无法使用以下命令在linux (fedora 26或fedora 32)上设置线路属性以使用自定义波特率:

代码语言:javascript
运行
复制
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <termio.h>
#include <linux/serial.h>
#include <err.h>

#include "portutils.h"

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);
    default: return 0;
    }
#undef B
}

int main(int argc, char** argv) {

    struct termios options;
    struct serial_struct serinfo;
    int fd;
    int speed = 0;
    int baudrate = 625000;
    char * port_name;

      // Check arguments for correct usage
    if (argc != 3){
        printf("\n  Usage: %s port baudrate!\n\n", argv[0]);
        return -1;
    }

    print_bar();

    baudrate = atoi(argv[2]);
    port_name = argv[1];

    /* Open and configure serial port */
    if ((fd = open(port_name, O_RDWR|O_NOCTTY)) == -1)
    {
        printf("\n  Could not open port %s\n", port_name);
        goto HELL;
    }

    printf("  Trying to set baud rate to %d\n  On port %s\n", baudrate, port_name);

    // if you've entered a standard baud the function below will return it
    speed = rate_to_constant(baudrate);

    if (speed == 0) {
        /* Custom divisor */
        serinfo.reserved_char[0] = 0;
        if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0) {
            printf("\n  ioctl: Failed to get #1\n  serial line information of %s\n", port_name);
            goto HELL;
        }
        serinfo.flags &= ~ASYNC_SPD_MASK;
        serinfo.flags |= ASYNC_SPD_CUST;
        serinfo.custom_divisor = (serinfo.baud_base + (baudrate / 2)) / baudrate;
        if (serinfo.custom_divisor < 1) {
            serinfo.custom_divisor = 1;
        }
        printf("  Baud_base: %d\n", serinfo.baud_base);
        printf("  Devisor: %d\n", serinfo.custom_divisor);
        printf("  Resulting baudrate: %f\n", serinfo.baud_base/(1.0*serinfo.custom_divisor));        
        if (ioctl(fd, TIOCSSERIAL, &serinfo) < 0) {
            printf("\n  ioctl: Failed to set\n  serial line information of %s\n", port_name);
            goto HELL;

        }
        if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0) {
            printf("\n  ioctl: Failed to get #2\n  serial line information of %s\n", port_name);
            goto HELL;
        }
        if (serinfo.custom_divisor * baudrate != serinfo.baud_base) {
            warnx("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);
    options.c_cflag &= ~CRTSCTS;
    if (tcsetattr(fd, TCSANOW, &options) != 0)
    {
        printf("\n  Failed to set final attributes of %s\n\n", port_name);
        goto HELL;
    }

    close(fd);
    printf("  Success on port %s\n", port_name);
    print_bar();

    return 0;

HELL:
 
    print_bar();
    return -1;
}

我正在使用一个使用ASIX芯片组的适配器和一个使用FTDI芯片组的适配器,基于FTDI的设备没有问题,但当我尝试使用第一个TIOCSSERIAL命令设置它时,另一个只从ioctl返回-1。那么,有没有一种方法可以检测使用的驱动程序是否支持TIOCSSERIAL命令?

又来了!我正在使用标签地狱:作为我的测试程序错误的常见返回点;-)

EN

回答 1

Stack Overflow用户

发布于 2020-09-26 01:34:30

USB转串行适配器不支持也不需要这些setserial ioctls。

如果您想在use适配器上设置自定义速度,您应该使用新的TCSETS2TCSETSW2TCSETSF2 ioctls,它们采用struct termios2,您应该在.c_cflag中设置BOTHER标志,并直接使用.c_ispeed.c_ospeed字段。看看/usr/include/asm-generic/termbits.h吧。

不需要设置除数或其他类似的事情。

上次我这样做时,为了能够包含这些头文件,我不得不使用一些termios (它们与来自glibc的#define头文件冲突)。

示例包装器(来自一些旧的来源,未经测试):

代码语言:javascript
运行
复制
#include <sys/ioctl.h>
#define termios termios_HIDE
#define termio termio_HIDE
#define winsize winsize_HIDE
#include <asm/termios.h>
#undef termios
#undef termio
#undef winsize
#define termios termios2
#define tcsetattr tcsetattr2
#define tcgetattr tcgetattr2

int tcsetattr2(int fd, int act, struct termios *ts){
        return act == TCSANOW ? ioctl(fd, TCSETS2, ts) :
                act == TCSADRAIN ? ioctl(fd, TCSETSW2, ts) :
                ioctl(fd, TCSETSF2, ts);
}
int tcgetattr2(int fd, struct termios *ts){
        return ioctl(fd, TCGETS2, ts);
}
int cfsetspeed(struct termios *ts, speed_t s){
        ts->c_cflag |= BOTHER;
        ts->c_ispeed = ts->c_ospeed = s;
        return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63983232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档