linux系统与51单片机实现串口数据交互

// 以下是 用于 linux IO INPUT  AND OUT PUT
参考代码详细 参考一下网站  写得都很详细  其中函数代码和所遇问题测试摘自一下网站,感谢他们提供的函数资源
大家可以先去看看函数模型以及方法  个人觉得他们归纳的比较好
//http://www.cnblogs.com/meronzhang/archive/2012/11/24/2786165.html
//http://www.cnblogs.com/meronzhang/archive/2012/11/24/2786166.html
//http://www.doc88.com/p-9045754154843.html


以下是代码  其中,mian函数和51函数为自己写的测试代码 测试通过,不过仍有bug,就是数据没清空,导致数据一直在接收


#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <errno.h>
#include <strings.h
#include <string.h>
#define TRUE 1
#define FALSE -1




int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,


                    B38400, B19200, B9600, B4800, B2400, B1200, B300,


                  };


int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300, 38400,


                  19200,  9600, 4800, 2400, 1200,  300,
                 };


struct termio


{
    unsigned short  c_iflag; /* 输入模式标志 */


    unsigned short  c_oflag;     /* 输出模式标志 */


    unsigned short  c_cflag;     /* 控制模式标志*/


    unsigned short  c_lflag;     /* local mode flags */


    unsigned char    c_line;           /* line discipline */


    unsigned char    c_cc[NCC];    /* control characters */


};






/**
*@brief  设置串口通信速率
*@param  fd     类型 int  打开串口的文件句柄
*@param  speed  类型 int  串口速度
*@return  void
*/






void set_speed(int fd, int speed)
{
    int   i;
    int   status;
    struct termios   Opt;
    tcgetattr(fd, &Opt);
    for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)
    {
        if  (speed == name_arr[i])
        {
            /**
             * tcflush函数刷清(抛弃)输入缓存(终端驱动程序已接收到,但用户程序尚未读)或输出缓存(用户程序已经写,但尚未发送)。queue参数应是下列三个常数之一:
     
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值