我正在编写一个程序来打开,设置和写入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_enable
向NOTTY
投诉。 strace
说:
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设备,我可以很容易地插拔并重新插入它,并希望它重置为某种默认值,但我还是要了解发生了什么。