EDN首页   博客首页 用户登陆  |  注册

日志档案

发表于 2006/10/19 13:08:43

26

标签: 无标签

linux下的串口通讯源程序--(测试版)

根据前面文章讲到的内容,为了说明问题,下面给出测试程序来理解linux下的串口操作流程,例程receive.c用来接收从串口发来的数据,而例程send.c用来发送数据到串口。二者成功建立串口连接后,串口接收端会收到串口发送端发来的字符串数据“Hellothis is a Serial Port test!”。

1.      receive.c程序清单:

/*******************************************************

*ilenamereceive.c

* DescriptionReceive 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 fds;

int open_serial(int k)

{

  if(k==0)       /*串口选择*/

   {

     fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY);  /*读写方式打开串口*/

     perror("open /dev/ttyS0");

   }

  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"); /*读状态标志判断*/

}

/*************************开始接收数据******************************/

while(*rbuf!='\n')       /*判断数据是否接收完毕*/

 {

      ncount+=1;

      rbuf++;

      retv="read"(fd,rbuf,1);

      if(retv==-1)

      {

perror("read");

    }

 }

/*******************************************************************/

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程序清单

/*******************************************************

* 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/ttyS0",O_RDWR|O_NOCTTY);  /*读写方式打开串口*/

     perror("open /dev/ttyS0");

   }

  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(&opt,B9600); /*波特率设置为9600bps*/

cfsetospeed(&opt,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;

}

/****************************结束***********************************/

分别将上面的俩个程序编译之后就可以运行了,如果是在两个不同的平台上运行,比如,在开发板上运行数据发送程序writewrite.c编译后得到),在宿主机上运行结收数据程序readread.c编译得到),采用串口线将二者正确连接之后,就可以运行来看实际的效果了:

首先在宿主机端运行数据接收程序receive

[zhang@localhost]# ./receive

[zhang@localhost]#open /dev/ttyS0 Success

ready for receiving data...

The data received is

   Hello,this is a Serial_Port test!

[zhang@localhost]#

在接收端运行完程序之后再到发送端运行数据发送程序send

#./send

ready for sending data...

the number of char sent is 35

#

运行完发送程序之后就可以在接收端看到接收的数据了。

也可以在一台PC机上来运行这两个程序,这时需要将串口线的23脚短路连接即可(自发自收),实际运行的步骤与上面相同。

系统分类: ARM   |   用户分类: 嵌入式linux   |   来源: 无分类   |   【推荐给朋友】   |   【添加到收藏夹】

阅读(12208)  |  评论(4)  |  收藏(0)  |  举报  

投一票您将和博主都有获奖机会!

最新评论

  • lanyijing

    2009/5/16 12:08:42

    我是楼上的那个,忘了给邮箱了……
    lanyijing@126.com
    楼主大大,一定要帮帮我啊~~~

  • lanyijing

    2009/5/16 12:03:35

    #include "math.h"

    为什么没有"math.h"文件?还有write、read函数?

    跪求!!!

    急!!!

  • 有没有改进版??!!

    2008/1/27 17:08:59

    也给我发个改进方法吧!!THANK YOU VERY MUCH!!!

    tiandiwoyong@163.com

  • haha

    2007/12/6 19:43:46

    我试了  接收不到数据

    如何改进 谢谢!sxylzyn@126.com