串行通信

/* * 3-1.c * * Created on: 2011-1-4 * Author: jinyong * 串行通信 * 在Linux中,所有设备文件都位于/dev下,其中COM1,COM2对应的设备名依次为 * "/dev/ttyS0","/dev/ttyS1".Linux对设备的操作方法和对文件的操作方法相同, * 因此,对串口的读写就可以使用简单"read","write"函数来完成,所不同的是要对串口 * 的一些参数配置。 * 基本流程: * 打开串口资源-》配置资源参数-》通信读写-》关闭串口资源 * */ //打开PC的COM1串行通信端口 #include <stdio.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <termios.h> int main(void) { int fd; fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NONBLOCK); /** * O_RDWR读写模式 * O_NOCTTY告诉Linux系统,这个程序会成为对应这个端口的控制终端, * 如果没有这个标志,那么任何一个输入,诸如键盘终止信号等,都将会影响你的进程。 * O_NONBLOCK告诉Linux系统这个程序以非阻塞的方式打开。 */ if( fd == -1 ) perror("open error/n"); else printf("open success/n"); return(fd); }  

你可能感兴趣的:(串行通信)