嵌入式linux串口通信自发自收测试程序
2024-08-31 19:01:46
/*串口自收自发程序主函数*/
#include"uart_api.h"
int main()
{
int fd;
char buff[BUFFER_SIZE];
char buff2[]="Hello NUAA2440!\n";
int nread,nwrite;
if((fd=open_port(TARGET_COM_PORT))<0)
{
perror("open serial error");
return 1;
}
printf("open ok!\n");
if(set_com_config(fd,115200,8,'N',1)<0) /*配置串口*/
{
perror("set_com_config error");
return 1;
}
printf(" set ok!\n");
/*将缓冲区buff2中的数据写入到串口1中去*/
nwrite=write(fd,buff2,sizeof(buff2));
printf("nwrite=%d\n",nwrite); /*打印写入的数据*/
/*接下来就是读数据*/
while(1)
{
if((nread = read(fd,buff,1024))>0)
{
buff[nread] = '\0';
printf("\nrecv:%d\n",nread);
printf("%s",buff);
}
}
close(fd);
return 0;
}
#include"uart_api.h"
int main()
{
int fd;
char buff[BUFFER_SIZE];
char buff2[]="Hello NUAA2440!\n";
int nread,nwrite;
if((fd=open_port(TARGET_COM_PORT))<0)
{
perror("open serial error");
return 1;
}
printf("open ok!\n");
if(set_com_config(fd,115200,8,'N',1)<0) /*配置串口*/
{
perror("set_com_config error");
return 1;
}
printf(" set ok!\n");
/*将缓冲区buff2中的数据写入到串口1中去*/
nwrite=write(fd,buff2,sizeof(buff2));
printf("nwrite=%d\n",nwrite); /*打印写入的数据*/
/*接下来就是读数据*/
while(1)
{
if((nread = read(fd,buff,1024))>0)
{
buff[nread] = '\0';
printf("\nrecv:%d\n",nread);
printf("%s",buff);
}
}
close(fd);
return 0;
}
最新文章
- django 操作 下载 excel xls xlsx csv
- beego中orm关联查询使用解析
- 36.Android之多线程和handle更新UI学习
- 【题解】【字符串】【Leetcode】Valid Palindrome
- Tomcat在eclipse中起动成功,主页却打不开
- 文件操作 - NSFileManager
- 解决poi导出Excel异常org.openxmlformats.schemas.spreadshe
- linux下C++对线程的封装
- 【转】MUD教程--巫师入门教程4
- Json.Net系列教程 4.Linq To JSON
- Java后端开发书架
- 04flask_scripts使用
- [SQL]事务回滚详解及示例
- php php-fpm安装 nginx配置php
- swift计算 switch case
- python的索引问题
- linux下uptime命令详解
- java泛型讲解
- argunlar 1.0.1 【数据绑定】
- 用Keras搭建神经网络 简单模版(一)——Regressor 回归
热门文章
- BZOJ3530: [Sdoi2014]数数(Trie图,数位Dp)
- 关于搭建Session服务器(转载)
- [Vue + TS] Use Properties in Vue Components Using @Prop Decorator with TypeScript
- A glance on VDBI
- android中常见声音操作方式(Ringtone,SoundPool,MediaPlayer)小结
- ORA-16009 remote archive log destination must be a STANDBY database
- Cocos2d-x学习笔记(一)HelloWorld
- 谈谈vector容器的三种遍历方法
- AJAX有关的请求协议及HTTP报文
- startActivityForResult()方法具体解释