读取串口文件

C语言open()打开文件

函数:open() --><io.h>(头文件)
函数原型:int open(char *path,int access[,int auth]);
函数解析:
char *path —文件路径
int access —打开方式
返回值:成功 返回文件句柄(大于0,不同于access) ,失败 返回-1

#include<stdio.h>
#include<fcntl.h>
int main(void){
	/*以读写或者创建方式打开串口*/
	int fd = open("/dev/ttyS0", O_RDWR);
    if(fd < 0){
    /* 不能打开串口*/
      printf("can not open the port\n");
      return fd;
   }
   printf("fd:-------%d------\n",fd);
   printf("successful to open the port\n");
   return 0;
}

C语言read()读取文件

函数:read()–><io.h>(头文件)
函数原型: int read(int handle,void *buf,int len);
函数解析:
int handle 为要读取的文件
void *buf 为要将读取的内容保存的缓冲区
int len 读取文件的长度
返回值:返回实际读取的字节数

程序例:创建文件,内容为 I like potato!

//打开文件,读取文件的内容
#include<stdio.h>
#include<io.h>
#include<fcntl.h>
int main(void){
   int fd=open("D:\\a.txt",O_RDONLY);
   if(fd==-1){
      printf("can not open the file\n");
      return 1;
   }
   char buf[1024]={"\0"};
   int len=read(fd,buf,1024);
   printf("%s\nlen=%d\n",buf,len);
   close(fd);
   return 0;
}

运行结果:
I like potato!
len:14

LINUX控制终端tcgetattr和tcsetattr

函数功能函数形式返回值
tcgetattr获取终端参数int tcgetattr(int fd, struct termios *termios_p)成功0 失败-1
tcsetattr设置终端参数int tcsetattr(int fd, int optional_actions, const struct termios *termios_p)成功0 失败-1
fd为终端的文件描述符,也就是上面打开文件
tcgetattr:返回的结果保存在termios结构体中

LINUX波特率设置函数cfsetospeed和cfsetispeed,波特率获取函数cfgetispeed和cfgetospeed

函数功能函数形式返回值
cfsetospeed设置输出波特率int cfsetospeed(struct termios *termptr, speed_t speed)成功0 失败-1
cfsetispeed设置输入波特率int cfsetispeed(struct termios *termptr, speed_t speed)成功0 失败-1
cfgetospeed获取输出波特率speed_t cfgetospeed(const struct termios *termptr)成功0 失败-1
cfgetispeed获取输入波特率speed_t cfgetispeed(const struct termios *termptr)成功0 失败-1

串口编程 函数tcflush

函数功能函数形式返回值
tcflush刷清输入或输出缓存int tcflush(int fd, int queue_selector)成功0 失败-1
queue_selector参数:
TCIFLUSH // 清除正收到的数据,且不会读取出来。
TCOFLUSH // 清除正写入的数据,且不会发送至终端。
TCIOFLUSH // 清除所有正在发生的I/O数据。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Maccy37

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值