sl
命令是一个有趣的小程序,它模拟一个火车穿过终端屏幕。使用 sl
命令可以在终端中显示一列火车动画。c,#include,#include,#include,#include,,int main() {, int serial_port = open("/dev/ttyS0", O_RDWR);, if (serial_port< 0) {, perror("Failed to open the serial port");, return 1;, },, struct termios tty;, if (tcgetattr(serial_port, &tty) != 0) {, perror("Failed to get attributes of the serial port");, close(serial_port);, return 1;, },, cfsetospeed(&tty, B9600);, cfsetispeed(&tty, B9600);,, tty.c_cflag &= ~PARENB; // No parity bit, tty.c_cflag &= ~CSTOPB; // 1 stop bit, tty.c_cflag &= ~CSIZE;, tty.c_cflag |= CS8; // 8 bits per byte, tty.c_cflag &= ~CRTSCTS;// No hardware flow control, tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines,, tty.c_lflag &= ~ICANON;, tty.c_lflag &= ~ECHO; // Disable echo, tty.c_lflag &= ~ECHOE; // Disable erasure, tty.c_lflag &= ~ECHONL; // Disable new-line echo, tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP,, tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl, tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable special handling of received bytes,, tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars), tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed,, tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received., tty.c_cc[VMIN] = 0;,, if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {, perror("Failed to set attributes of the serial port");, close(serial_port);, return 1;, },, char msg[] = "Hello, serial port!";, write(serial_port, msg, sizeof(msg));,, close(serial_port);, return 0;,},
``Powered By Z-BlogPHP 1.7.3