下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
linux下的串口通信程序详解2009-07-1912:37为了说明问题,下面给出测试程序来理解linux下的串口操作流程,例程receive.c用来接收从串口发来的数据,而例程send.c用来发送数据到串口。二者成功建立串口连接后,串口接收端会收到串口发送端发来的字符串数据“Hello,thisisaSerialPorttest!”。
1.receive.c程序清单:\o"viewplain"viewplain/*******************************************************
*ilename:receive.c
*
Description:Receive
data
from
Serial_Port
*
Date:
*******************************************************/
/*********************头文件定义***********************/
#include
<stdio.h>
#include
<string.h>
#include
<malloc.h>
#include
<sys/types.h>
#include
<sys/stat.h>
#include
<fcntl.h>
#include
<unistd.h>
#include
<termios.h>
#include
"math.h"
#define
max_buffer_size
100
/*定义缓冲区最大宽度*/
/*********************************************************/
int
fd,s;
int
open_serial(int
k)
{
if(k==0)
/*串口选择*/
{
fd
=
open("/dev/ttyUSB0",O_RDWR|O_NOCTTY);
/*读写方式打开串口*/
perror("open
/dev/ttyUSB0");
}
else
{
fd
=
open("/dev/ttyS1",O_RDWR|O_NOCTTY);
perror("open
/dev/ttyS1");
}
if(fd
==
-1)
/*打开失败*/
return
-1;
else
return
0;
}
/********************************************************************/
int
main()
{
char
hd[max_buffer_size],*rbuf;
/*定义接收缓冲区*/
int
flag_close,
retv,i,ncount=0;
struct
termios
opt;
int
realdata=0;
/*******************************************************************/
open_serial(0);
/*打开串口1*/
/*******************************************************************/
tcgetattr(fd,&opt);
cfmakeraw(&opt);
/*****************************************************************/
cfsetispeed(&opt,B9600);
/*波特率设置为9600bps*/
cfsetospeed(&opt,B9600);
/*******************************************************************/
tcsetattr(fd,TCSANOW,&opt);
rbuf=hd;
/*数据保存*/
printf("ready
for
receiving
data.../n");
retv=read(fd,rbuf,1);
/*接收数据*/
if(retv==-1)
{
perror("read
bad1");
/*读状态标志判断*/
}
/*************************开始接收数据******************************/
while(*rbuf!='/n')
/*判断数据是否接收完毕*/
{
ncount+=1;
rbuf++;
retv=read(fd,rbuf,1);
if(retv==-1)
{
perror("read
bad2");
}
}
/*******************************************************************/
printf("The
data
received
is:/n");
/*输出接收到的数据*/
for(i=0;i<ncount;i++)
{
printf("%c",hd[i]);
}
printf("/n");
flag_close
=close(fd);
if(flag_close
==
-1)/*判断是否成功关闭文件*/
printf("Close
the
Device
failur!/n");
return
0;
}
/****************************结束***********************************/
2.send.c程序清单\o"viewplain"viewplain/*******************************************************
*
File
Name:
send.c
*
Description:
send
data
to
serial_Port
*
Date:
*******************************************************/
/******************头文件定义******************/
#include
<stdio.h>
#include
<string.h>
#include
<malloc.h>
#include
<sys/types.h>
#include
<sys/stat.h>
#include
<fcntl.h>
#include
<unistd.h>
#include
<termios.h>
#define
max_buffer_size
100
/*定义缓冲区最大宽度*/
/*******************************************/
int
fd;
/*定义设备文件描述符*/
int
flag_close;
int
open_serial(int
k)
{
if(k==0)
/*串口选择*/
{
fd
=
open("/dev/ttyUSB0",O_RDWR|O_NOCTTY);
/*读写方式打开串口*/
perror("open
/dev/ttyUSB0");
}
else
{
fd
=
open("/dev/ttyS1",O_RDWR|O_NOCTTY);
perror("open
/dev/ttyS1");
}
if(fd
==
-1)
/*打开失败*/
return
-1;
else
return
0;
}
/********************************************************************/
int
main(int
argc,
char
*argv[
]
)
{
char
sbuf[]={"Hello,this
is
a
Serial_Port
test!/n"};/*待发送的内容,以/n为结束标志*/
int
sfd,retv,i;
struct
termios
option;
int
length=sizeof(sbuf);/*发送缓冲区数据宽度*/
/*******************************************************************/
open_serial(0);
/*打开串口1*/
/*******************************************************************/
printf("ready
for
sending
data.../n");
/*准备开始发送数据*/
tcgetattr(fd,&option);
cfmakeraw(&option);
/*****************************************************************/
cfsetispeed(&option,B9600);
/*波特率设置为9600bps*/
cfsetospeed(&option,B9600);
/*******************************************************************/
tcsetattr(fd,TCSANOW,&option);
retv=write(fd,sbuf,length);
/*接收数据*/
if(retv==-1)
{
perror("write");
}
printf("the
number
of
char
sent
is
%d/n",retv);
flag_close
=close(fd);
if(flag_close==-1)
/*判断是否成功关闭文件*/
printf("Close
the
Device
failur!/n");
return
0;
}
/****************************结束***********************************/
分别将上面的俩个程序编译之后就可以运行了,如果是在两个不同的平台上运行,比如,在开发板上运行数据发送程序write(write.c编译后得到),在宿主机上运行结收数据程序read(read.c编译得到),采用串口线将二者正确连接之后,就可以运行来看实际的效果了:
首先在宿主机端运行数据接收程序receive:
[zhang@localhost]#./receive
[zhang@localhost]#open/dev/ttyS0:Success
readyforreceivingdata...
Thedatarece
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 事业编制解除聘用合同模板
- 融资合同模板锦集
- 木材购销合同范本
- 工程用工合同
- 见义勇为英雄无偿房屋租赁合同
- 玻璃隔断制作安装合同版
- 洗车场承包合同
- 劳务外包服务合同
- 拖拉机租赁合同书
- 车辆过户转让合同
- SuperBench大模型综合能力评测报告
- 留守儿童关爱服务关爱留守儿童的应急方案
- 2024年湖北省宏泰国有资本投资运营集团有限公司招聘笔试参考题库含答案解析
- 多源异构数据融合关键技术研究
- 中外政治思想史-形成性测试三-国开(HB)-参考资料
- 信托业的普惠金融发展研究
- 2024年城市轨道交通运营与维护竞赛考试题库(职校用)
- 创意改变生活智慧树知到期末考试答案2024年
- 小学语文中高年级单元整体教学设计的实践研究(开题报告)
- 智慧楼宇大数据应用建设和运营整体解决方案
- 菜品研发方案
评论
0/150
提交评论