23,125
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h> /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix 标准函数定义*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX 终端控制定义*/
#include <errno.h> /*错误号定义*/
#include <termios.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
//应该把中断都屏蔽到
int main()
{
int fd;
struct termios Options;
int buff[512];
int nread;
/*以读写方式打开串口*/
fd = open( "/dev/ttyS1", O_RDWR);
if (-1 == fd)
{
/* 不能打开串口一 ,则提示错误,*/
perror(" 提示错误!");
}
tcgetattr(fd, &Options);
cfsetispeed(&Options,B9600); /*设置为波特率为9600Bps*/
cfsetospeed(&Options,B9600);
Options.c_cflag &= ~CSIZE;
Options.c_cflag |= CS8; //设置为8个字节
Options.c_cflag &= ~PARENB;
Options.c_cflag &= ~CSTOPB ; //无校验位
Options.c_cflag &= ~CSTOPB; //一个停止位
tcsetattr(fd,TCSANOW,&Options) ; //使设置的波特率生效
/*下面是读串口的程序*/
int status;
ioctl(fd, FIONREAD, &status);
if(status>0)
nread = read(fd, buff, status);
printf("\nLen %d\n",nread);
close(fd);
exit (0);
}