【发布时间】:2020-09-20 20:11:56
【问题描述】:
我正在使用 USB 转 RS-232 串行适配器,无法设置线路属性以在 linux(fedora 26 或 fedora 32)上使用自定义波特率:
#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 的设备没有问题,但是当我尝试使用第一个 TIOCSERIAL 命令进行设置时,另一个只是从 ioctl 返回 -1 .那么有没有办法检测使用的驱动程序是否不支持 TIOCSERIAL 命令?
PS!我正在使用标签 HELL: 作为测试程序错误的常见返回点;-)
【问题讨论】:
-
TIOC*SERIAL ioctls 本身是在串行内核中实现的(因此所有驱动程序都支持),但您设置的特定选项或标志可能并非所有驱动程序都支持。看看
dmesg有没有说什么。 -
感谢@ErkiAring 所有 dmesg 给出的是:[4.684266]usb 3-1:FTDI USB 串行设备转换器现在连接到 ttyUSB0 [4.684524]usb 3-2:FTDI USB 串行设备转换器现在连接到 ttyUSB1 [4.687079]usb 1-1.2:Moschip 7840/7820 USB 串行驱动程序转换器现在连接到 ttyUSB2 [4.689288]usb 1-1.2:Moschip 7840/7820 USB 串行驱动程序转换器现在连接到 ttyUSB3
-
"另一个简单地从 ioctl 返回 -1" -- 阅读 man 页面。通常,当系统调用返回
-1时,需要获取全局变量 errno 以获取详细信息。 “如何检测驱动程序(不)是否支持 ioctl 命令” -- 通常没有系统调用来查询另一个系统调用或 ioctl 功能。相反,只需发出请求,并检查它是成功还是被拒绝(例如,返回码是-1,errno 是ENOIOCTLCMD)。
标签: linux serial-port ioctl baud-rate