在 MAC OS X (10.10) 程序中,我很难为 RS-485 串行通信正确设置 termios(我使用带有 FTDI 芯片的 starcom USB → RS-485)
我需要设置以下内容:
- 1 个起始位(0 位)
- 8 个数据位
- 1 奇校验位
- 1 个停止位(1 位)
- 19200 波特
通信是二进制的,所以没有停止字符。
目前,我已经找到了如何设置大部分设置(见下面的代码),但我没有找到告诉 termios 我想要一个开始位的地方。
@property(readwrite) int fileDescriptor;
_fileDescriptor = open(bsdPath, O_RDWR | O_NOCTTY | O_NONBLOCK);
ioctl(_fileDescriptor, TIOCEXCL)
fcntl(_fileDescriptor, F_SETFL, 0)
tcgetattr(_fileDescriptor, &gOriginalTTYAttrs)
struct termios options = gOriginalTTYAttrs;
cfmakeraw(&options);
struct termios* rawOptions = &options;
// Timeout 100 ms
rawOptions->c_cc[VTIME] = 1;
rawOptions->c_cc[VMIN] = 0;
// 8 bits
rawOptions->c_cflag |= CS8;
// Parity ODD
rawOptions->c_cflag |= PARENB;
rawOptions->c_cflag |= PARODD;
// Stop bit (I hope it means one stop bit)
rawOptions->c_cflag = rawOptions->c_cflag & ~CSTOPB;
// Flow control none
rawOptions->c_cflag = rawOptions->c_cflag & ~CRTSCTS;
rawOptions->c_cflag = rawOptions->c_cflag & ~(CDTR_IFLOW | CDSR_OFLOW);
rawOptions->c_cflag = rawOptions->c_cflag & ~CCAR_OFLOW;
// Turn on hangup on close (NO IDEA WHAT IT DOES)
rawOptions->c_cflag |= HUPCL;
// Set local mode on (NO IDEA WHAT IT DOES)
rawOptions->c_cflag |= CLOCAL;
// Enable receiver (USEFUL, I GUESS)
rawOptions->c_cflag |= CREAD;
// Turn off canonical mode and signals (NO IDEA WHAT IT DOES)
rawOptions->c_lflag &= ~(ICANON /*| ECHO*/ | ISIG);
// Raw (binary output)
rawOptions->c_oflag &= ~OPOST;
// 19200 baud
cfsetspeed(rawOptions, 19200);
// Applying settings
tcsetattr(_fileDescriptor, TCSANOW, rawOptions);