zoukankan      html  css  js  c++  java
  • 嵌入式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);

  • 相关阅读:
    媒介管理系统权限设计方案
    document.ready和window.onload区别,顺带jQuery的ready方法
    nvm管理node版本
    async await一些小结
    Git学习
    详解JavaScript中的正则表达式
    JavaScript中的事件循环机制跟函数柯里化
    前端面试遇到的问题
    ES6在工作中会用到的核心知识点讲解
    JavaScript中的事件委托机制跟深浅拷贝
  • 原文地址:https://www.cnblogs.com/lidabo/p/5391067.html
Copyright © 2011-2022 走看看