4

I am writing a program to open, setup, and write to a tty for rs485. I have played a bit with the tty device, and now I can't seem to open it anymore.

Here is the relevant code:

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;
}

, which is called from a test program as such:

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

The ioctl in the rs485_enable function complains with NOTTY. strace says:

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)

and stty says:

$ 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

It feels like I have broken the /dev/ttyUSBserial while playing with it, but I don't know how, or how to fix it. Since it is a USB device, I could easily unplug and replug it and hope it resets to some kind of defaults, but I'd rather understand what is going on.

Gauthier
  • 40,309
  • 11
  • 63
  • 97
  • There are only a handful of drivers in the (mainline) Linux kernel that support RS-485 and the TIOCSRS485 ioctl(). And none of those drivers are for USB serial adapters: http://lxr.free-electrons.com/ident?i=TIOCSRS485 What USB adapter are you using? Is it capable of RS-485? What driver are you using? – sawdust Jan 17 '15 at 22:31
  • I am using this: http://www.digitus.info/en/products/accessories/adapter-and-converter/r-usb-serial-adapter-usb-20-da-70157/ , chip is FTDI / FT232RL (it says it supports RS485 serial interface). I got it working for a while, so it looks like this setup has the capability. – Gauthier Jan 19 '15 at 07:59
  • I'm not sure about migrating this - see http://meta.unix.stackexchange.com/questions/2875/is-the-unix-c-api-still-on-topic, but it seems to be equally on-topic here. – Flexo Jan 19 '15 at 09:49
  • One more time: What linux device driver are you using? The [FTDI driver](http://lxr.free-electrons.com/source/drivers/usb/serial/ftdi_sio.c#L2444) in the mainline kernel does not support TIOCSRS485. It does not matter that the hardware is capable. If the driver does not support that feature, then you cannot use that HW capability using that ioctl(). – sawdust Jan 19 '15 at 22:42
  • Obviously, I have no clue. It looks like I can live without the `ioctl()`, and have enough control with the `termios` struct. Thanks. – Gauthier Jan 20 '15 at 10:03

0 Answers0