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.