嵌入式linux串口编程(二)
/*com_writer.c*/
#include "uart_api.h"
int main(void)
{
int fd;
char buff[BUFFER_SIZE];
if((fd=open_port(HOST_COM_PORT))<0) /*打开串口*/
{
perror("open serial error");
return 1;
}
if(set_com_config(fd,115200,8,'N',1)<0) /*配置串口*/
{
perror("set_com_config error");
return 1;
}
do
{
printf("Input some words(enter 'quit' to exit):");
memset(buff,0,BUFFER_SIZE);
if(fgets(buff,BUFFER_SIZE,stdin)==NULL)
{
perror("fgets");
break;
}
write(fd,buff,strlen(buff));
}while(strncmp(buff,"quit",4));
close(fd);
return 0;
}
/*com_reader.c*/
#include "uart_api.h"
#define COM_TYPE GNR_COM
int main(void)
{
int fd;
char buff[BUFFER_SIZE];
if((fd=open_port(TARGET_COM_PORT))<0)
{
perror("open serial error");
return 1;
}
if(set_com_config(fd,115200,8,'N',1)<0) /*配置串口*/
{
perror("set_com_config error");
return 1;
}
do
{
memset(buff,0,BUFFER_SIZE);
if(read(fd,buff,BUFFER_SIZE)>0)
{
printf("the receive words are:%s",buff);
}
}while(strncmp(buff,"quit",4));
close(fd);
return 0;
}
/*uart_api.h*/
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include <termios.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <ctype.h>
#define HOST_COM_PORT 1
#define BUFFER_SIZE 4096
#define MAX_COM_NUM 5
#define TARGET_COM_PORT 1
extern int open_port(int com_port);
extern int set_com_config(int fd,int baud_rate, int data_bits,char parity,int stop_bits);
最新文章
- CS193P - 2016年秋 第一讲 课程简介
- 背水一战 Windows 10 (5) - UI: 标题栏
- 复制选中的listbox内容
- mvc Html.BeginForm 生成 ?Length=
- ios开发UI篇—在ImageView中添加按钮以及Tag的参数说明
- 每日一dp(2)——龟兔赛跑(hdu 2059)
- HDU1166 数状数组
- jquery系列教程3-DOM操作全解
- 人体姿态的相似性评价基于OpenCV实现最近邻分类KNN K-Nearest Neighbors
- ubuntu安装fat32和exfat文件系统支持
- (NO.00004)iOS实现打砖块游戏(三):游戏主场景和砖块
- SpringMVC+Apache Shiro+JPA(hibernate)案例教学(三)给Shiro登录验证加上验证码
- redis主从同步故障切换及集群配置
- 看不懂霍尔效应的直接看视频https://www.bilibili.com/video/av11446173/
- SharePoint 2013 Workflow Manager 1.0 远程服务器返回错误: (400) 错误的请求。 不支持查询字符串中的 api-version
- Installation of Scylla on CentOS 7
- [svc]linux日志和安全日志
- shell脚本基本用法
- linux 常用命令总结(三)
- iview(DatePicker)时间转入后台少一天 解决方案