这个运营商做什么?

时间:2015-10-22 22:17:25

标签: c linux unix arduino driver

我正在为我的中文arduino编写一个Linux驱动程序。有一刻我需要改变波特率。我找了一些例子,发现上市:

Listing 2 - Setting the baud rate.

struct termios options;

/*
 * Get the current options for the port...
 */

tcgetattr(fd, &options);

/*
 * Set the baud rates to 19200...
 */

cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);

/*
 * Enable the receiver and set local mode...
 */

options.c_cflag |= (CLOCAL | CREAD);

/*
 * Set the new options for the port...
 */

tcsetattr(fd, TCSANOW, &options);

最后一行代码的下一行有|=运算符。它有什么作用?我以前从未见过它。

1 个答案:

答案 0 :(得分:3)

options.c_cflag |= (CLOCAL | CREAD);

通常相当于

options.c_cflag = options.c_cflag | (CLOCAL | CREAD);

除了options.c_cflag只评估一次,这在上面的表达式中并不重要,但如果options.c_cflag有任何副作用(例如,如果它是{{1 }})