PC机与FL2440的串口通信编程

  1. #include     <stdio.h>      /*标准输入输出定义*/  
  2. #include     <stdlib.h>     /*标准函数库定义*/  
  3. #include     <unistd.h>     /*Unix标准函数定义*/  
  4. #include     <sys/types.h>  /**/  
  5. #include     <sys/stat.h>   /**/  
  6. #include     <fcntl.h>      /*文件控制定义*/  
  7. #include     <termios.h>    /*PPSIX终端控制定义*/  
  8. #include     <errno.h>      /*错误号定义*/  
  9. #include     <string.h>  
  10. #define TRUE    0  
  11. #define FALSE   -1  
  12.   
  13. #define BUFFSIZE 1024  
  14.   
  15. int open_dev(charchar *dev_name );  
  16. void set_serial_port_speed(int fd, int speed);  
  17. void Set_parityofport(int fd,int databits,int stopbits,char parity);  
  18.   
  19. int main(void)  
  20. {    
  21.     int  fd;  
  22.     int  read_size;  
  23.     struct timeval  timeout;  
  24.     char   buff[BUFFSIZE];  
  25.     pid_t   pid;  
  26.           
  27.   
  28.     fd = open_dev("/dev/ttyS1");  
  29.     set_serial_port_speed(fd, 115200);  
  30.     Set_ParityOfPort(fd, 81'n');  
  31.       
  32. /*子进程执行读操作*/  
  33. if (0 == (pid = fork()))  
  34.     {  
  35.         char w_buff[BUFFSIZE];  
  36.         while (1)  
  37.         {  
  38.             scanf("%s",w_buff);  
  39.            /*write为不带缓冲的IO函数*/  
  40.             if (0 > write(fd, w_buff, strlen(w_buff)))  
  41.             {  
  42.                 perror("Write Serial Fail:");  
  43.                 exit(-1);  
  44.             }  
  45.             memset(w_buff, 0x00, BUFFSIZE);  
  46.         }  
  47.   
  48.     }  
  49.   
  50.     /*父进程执行写操作*/  
  51.     while (1)  
  52.     {  
  53.         memset(buff, 0x00sizeof(buff));  
  54.         if (0 > (read_size = read(fd, buff, BUFFSIZE)))  
  55.         {  
  56.             perror("Read Serial Fail:");  
  57.             exit(-1);  
  58.         }  
  59.         if (read_size > 0)  
  60.             printf("%s", buff);  
  61.         fflush(stdout);    
  62.     }  
  63.      
  64.     close(fd);  
  65.     return 0;  
  66. }  

以上是头文件,宏定义,函数声明以及主函数

以下是各个子函数的实现

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /******************************************************** 
  2.  *                打开串口 
  3.  *******************************************************/  
  4.  int open_dev(charchar *dev_name)  
  5.  {     
  6.      int fd;  
  7.      if((fd = open(dev_name, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0)  
  8.      {  
  9.          perror("open error");  
  10.          return FALSE;  
  11.      }  
  12.      return fd;  
  13.  }  
  14.   
  15.   
  16. /*********************************************************** 
  17.  *              设置波特率 
  18.  **********************************************************/  
  19. void Set_Serial_Port_Speed(int fd, int speed)  
  20. {  
  21.         struct termios  term;  
  22.   
  23.     if(tcgetattr(fd, &term) != 0)  
  24.     {  
  25.        perror("tcgetattr error");  
  26.   
  27.     }  
  28.      
  29.     tcflush(fd, TCIOFLUSH);  
  30.     switch(speed)  
  31. {   
  32.       
  33.     case 2400:  
  34.           cfsetispeed(&term, B2400);//输入波特率  
  35.           cfsetospeed(&term, B2400);//输出波特率  
  36.           break;  
  37.       case 4800:  
  38.           cfsetispeed(&term, B4800);  
  39.           cfsetospeed(&term, B4800);  
  40.           break;  
  41.       case 9600:  
  42.           cfsetispeed(&term, B9600);  
  43.           cfsetospeed(&term, B9600);  
  44.           break;  
  45.       case 57600:  
  46.           cfsetispeed(&term, B57600);  
  47.           cfsetospeed(&term, B57600);  
  48.           break;  
  49.       case 115200:  
  50.           cfsetispeed(&term, B115200);  
  51.           cfsetospeed(&term, B115200);  
  52.       default:  
  53.           cfsetispeed(&term, B115200);  
  54.           cfsetospeed(&term, B115200);  
  55.           break;  
  56. }  
  57.     if(tcsetattr(fd, TCSANOW, &term) != 0)  
  58.     {  
  59.         perror("tcsetattr error");  
  60.     }  
  61. }  
  62.   
  63.   
  64. /****************************************************************** 
  65.  *           设置奇偶校验 
  66.  
  67. *****************************************************************/             
  68. void Set_ParityOfPort(int fd,int databits,int stopbits,char parity)  
  69. {  
  70.     struct termios options;  
  71.     if(tcgetattr(fd,&options) != 0)  
  72.     {  
  73.         perror("tcgetattr error");  
  74.       
  75.     }  
  76.         // 不屏蔽字符大小  
  77.     options.c_cflag &= ~CSIZE;  
  78.         //非规范输入,不进行回送,非可见擦除符,不启用终端产生的信号  
  79.     options.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);  
  80.         //不进行输出处理  
  81.     options.c_oflag  &= ~OPOST;  
  82.   
  83.     switch (databits) /*设置数据位数*/  
  84.     {  
  85.       case 7:    
  86.         options.c_cflag |= CS7;  
  87.         break;  
  88.            case 8:  
  89.         options.c_cflag |= CS8;  
  90.             break;  
  91.        default:   
  92.         fprintf(stderr,"Unsupported data size/n");  
  93.       
  94.   
  95.     }  
  96.   
  97.      
  98.    switch (parity)  
  99.    {  
  100.        case 'n':  
  101.        case 'N':  
  102.                       options.c_cflag &= ~PARENB;   
  103.               options.c_iflag &= ~INPCK;   
  104.               break;  
  105.            case 'o':  
  106.        case 'O':  
  107.                       options.c_cflag |= (PARODD | PARENB);  
  108.               options.c_iflag |= INPCK;  
  109.               break;  
  110.        case 'e':  
  111.            case 'E':  
  112.                   options.c_cflag |= PARENB;   
  113.               options.c_cflag &= ~PARODD;  
  114.               options.c_iflag |= INPCK;   
  115.               break;  
  116.         case 's':   
  117.         case 'S':  
  118.                    options.c_cflag &= ~PARENB;  
  119.                options.c_cflag &= ~CSTOPB;   
  120.                break;   
  121.         default:  
  122.                    fprintf(stderr,"Unsupported parity/n");   
  123.       
  124.    }  
  125.   
  126.   
  127.    switch (stopbits)  
  128.    {  
  129.        case 1:   
  130.                       options.c_cflag &= ~CSTOPB;   
  131.               break;  
  132.        case 2:  
  133.                       options.c_cflag |= CSTOPB;   
  134.               break;  
  135.        default:  
  136.                       fprintf(stderr,"Unsupported stop bits/n");   
  137.       
  138.    }  
  139.    /*打开奇偶校验*/  
  140.    if (parity != 'n')  
  141.        options.c_iflag |= INPCK;  
  142.       
  143.    tcflush(fd,TCIFLUSH);  
  144.    /*read 立即返回[0,nbytes]*/  
  145.    options.c_cc[VTIME] = 0;  
  146.    options.c_cc[VMIN] = 0;  
  147.   
  148.    if (tcsetattr(fd,TCSANOW,&options) != 0)   
  149.    {  
  150.       perror("tcsetattr error");   
  151.        
  152.    }  
  153.      
  154. }  


将c代码在交叉编译器以编译,生成a.out文件

然后将a.out文件下载到FL2440开发板上运行就可以了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值