4

我正在编写一个程序来打开、设置和写入 rs485 的 tty。我用tty设备玩了一下,现在我似乎无法再打开它了。

以下是相关代码:

int rs485_enable(const char *dev_name, const speed_t speed)
{
    int fd = open(dev_name, O_RDWR);
    if (fd < 0) {
        perror("open");
        return -1;
    }

    if (rs485_speed_set(fd, speed) < 0) {
        perror("rs485_speed_set");
        return -1;
    }

    struct serial_rs485 rs485conf = {
        .flags = SER_RS485_ENABLED,
        .delay_rts_before_send = 0x00000004
    };

    if (ioctl(fd, TIOCSRS485, &rs485conf) < 0) {
        perror("ioctl");
        return -1;
    }

    return fd;
}

int rs485_speed_set(const int fd, const speed_t speed)
{
    struct termios tty;
    if (tcgetattr(fd, &tty) < 0) {
        perror("tcgetattr");
        return -1;
    }

    cfsetispeed(&tty, speed);
    cfsetospeed(&tty, speed);

    if (tcsetattr(fd, TCSADRAIN, &tty) < 0) {
        perror("tcsetattr");
        return -1;
    }
    return 0;
}

,从测试程序中调用,如下所示:

int rs485_fd = rs485_enable("/dev/ttyUSBserial", B9600);

函数ioctl中的. 说:rs485_enableNOTTYstrace

open("/dev/ttyUSBserial", O_RDWR)       = 3
ioctl(3, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, {B9600 opost isig icanon echo ...}) = 0
ioctl(3, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, {B9600 opost isig icanon echo ...}) = 0
ioctl(3, SNDCTL_TMR_STOP or SNDRV_TIMER_IOCTL_GINFO or TCSETSW, {B9600 opost isig icanon echo ...}) = 0
ioctl(3, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, {B9600 opost isig icanon echo ...}) = 0
ioctl(3, TIOCSRS485, 0x7fff8046f0c0)    = -1 ENOTTY (Inappropriate ioctl for device)

和 stty 说:

$ stty -F /dev/ttyUSBserial -a
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

感觉就像我/dev/ttyUSBserial在玩它时打破了它,但我不知道如何或如何修复它。由于它是一个 USB 设备,我可以轻松地拔下并重新插入它,并希望它重置为某种默认值,但我宁愿了解发生了什么。

4

0 回答 0