我正在使用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的设备没有问题,但当我尝试使用第一个TIOCSSERIAL命令设置它时,另一个只从ioctl返回-1。那么,有没有一种方法可以检测使用的驱动程序是否支持TIOCSSERIAL命令?
又来了!我正在使用标签地狱:作为我的测试程序错误的常见返回点;-)
发布于 2020-09-26 01:34:30
USB转串行适配器不支持也不需要这些setserial
ioctls。
如果您想在use适配器上设置自定义速度,您应该使用新的TCSETS2
、TCSETSW2
和TCSETSF2
ioctls,它们采用struct termios2
,您应该在.c_cflag
中设置BOTHER
标志,并直接使用.c_ispeed
和.c_ospeed
字段。看看/usr/include/asm-generic/termbits.h
吧。
不需要设置除数或其他类似的事情。
上次我这样做时,为了能够包含这些头文件,我不得不使用一些termios (它们与来自glibc的#define
头文件冲突)。
示例包装器(来自一些旧的来源,未经测试):
#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;
}
https://stackoverflow.com/questions/63983232
复制相似问题