AT91SAM9260串口编程


AT91SAM9260串口编程

  1. /************************************************* 
  2. * File name   : 9260-uart-v1.0.c 
  3. * Description :  
  4. * Author      : sg131971@qq.com 
  5. * Version     : V1.0 
  6. * Date        :  
  7. * Compiler    : arm-linux-gcc-4.4.3 
  8. * Target      : 9260EK(Linux-2.6.30) 
  9. * History     :  
  10. *   <author>  <time>   <version >   <desc> 
  11. *************************************************/  
  12. #include<unistd.h>   
  13. #include<sys/types.h>   
  14. #include<sys/stat.h>   
  15. #include<fcntl.h>   
  16. #include<stdlib.h>   
  17. #include<stdio.h>   
  18. #include<string.h>   
  19. #include<errno.h>   
  20. #include<termios.h>   
  21.   
  22. /************************************************* 
  23. * Function    :  
  24. * Description : 注意/dev/ttyAT1~6对应于电路图上的0~5 
  25. * Calls       :  
  26. * Called By   :  
  27. * Input       : int comport = 0~6  
  28. * Output      :  
  29. * Return      :  
  30. *************************************************/  
  31. int open_port(int fd, int comport)  
  32. {  
  33.     if (comport == 0)  
  34.     {  
  35.         fd = open("/dev/ttyAT0", O_RDWR | O_NOCTTY | O_NDELAY);  
  36.         if (-1 == fd)  
  37.         {  
  38.             perror("can't open serial port");  
  39.             return (-1);  
  40.         }  
  41.         else  
  42.             printf("open ttyAT0\n");  
  43.     }  
  44.     if (comport == 1)  
  45.     {  
  46.         fd = open("/dev/ttyAT1", O_RDWR | O_NOCTTY | O_NDELAY);  
  47.         if (-1 == fd)  
  48.         {  
  49.             perror("can't open serial port");  
  50.             return (-1);  
  51.         }  
  52.         else  
  53.             printf("open ttyAT1\n");  
  54.     }  
  55.     if (comport == 2)  
  56.     {  
  57.         fd = open("/dev/ttyAT2", O_RDWR | O_NOCTTY | O_NDELAY);  
  58.         if (-1 == fd)  
  59.         {  
  60.             perror("can't open serial port");  
  61.             return (-1);  
  62.         }  
  63.         else  
  64.             printf("open ttyAT2\n");  
  65.     }  
  66.     if (comport == 3)  
  67.     {  
  68.         fd = open("/dev/ttyAT3", O_RDWR | O_NOCTTY | O_NDELAY);  
  69.         if (-1 == fd)  
  70.         {  
  71.             perror("can't open serial port");  
  72.             return (-1);  
  73.         }  
  74.         else  
  75.             printf("open ttyAT3\n");  
  76.     }  
  77.     if (comport == 4)  
  78.     {  
  79.         fd = open("/dev/ttyAT4", O_RDWR | O_NOCTTY | O_NDELAY);  
  80.         if (-1 == fd)  
  81.         {  
  82.             perror("can't open serial port");  
  83.             return (-1);  
  84.         }  
  85.         else  
  86.             printf("open ttyAT4\n");  
  87.     }  
  88.     if (comport == 5)  
  89.     {  
  90.         fd = open("/dev/ttyAT5", O_RDWR | O_NOCTTY | O_NDELAY);  
  91.         if (-1 == fd)  
  92.         {  
  93.             perror("can't open serial port");  
  94.             return (-1);  
  95.         }  
  96.         else  
  97.             printf("open ttyAT5\n");  
  98.     }  
  99.     if (comport == 6)  
  100.     {  
  101.         fd = open("/dev/ttyAT6", O_RDWR | O_NOCTTY | O_NDELAY);  
  102.         if (-1 == fd)  
  103.         {  
  104.             perror("can't open serial port");  
  105.             return (-1);  
  106.         }  
  107.         else  
  108.             printf("open ttyAT6\n");  
  109.     }  
  110.   
  111.     if (fcntl(fd, F_SETFL, 0) < 0)  
  112.         printf("fcnt failed!\n");  
  113.     else  
  114.         printf("fcntl=%d\n", fcntl(fd, F_SETFL, 0));  
  115.   
  116.     if (isatty(STDIN_FILENO) == 0)  
  117.         printf("standard input is not a terminal device\n");  
  118.     else  
  119.         printf("isatty success!\n");  
  120.   
  121.     return fd;  
  122. }  
  123.   
  124. /************************************************* 
  125. * Function    :  
  126. * Description :  
  127. * Calls       :  
  128. * Called By   :  
  129. * Input       :  
  130. * Output      :  
  131. * Return      :  
  132. *************************************************/  
  133. int set_opt(int fd, int nSpeed, int nBits, int nFctl, char nEvent, int nStop)  
  134. {  
  135.     struct termios newtio, oldtio;  
  136.     if (tcgetattr(fd, &oldtio) != 0)  
  137.     {  
  138.         perror("SetupSerial 1");  
  139.         exit(1);  
  140.     }  
  141.     bzero(&newtio, sizeof(newtio));  
  142.     newtio.c_cflag |= CLOCAL | CREAD;     
  143.     newtio.c_cflag &= ~CSIZE;  
  144.       
  145.     switch (nBits)  
  146.     {  
  147.       case 7:  
  148.           newtio.c_cflag |= CS7;  
  149.           break;  
  150.       case 8:  
  151.           newtio.c_cflag |= CS8;  
  152.           break;  
  153.     }  
  154.     switch (nFctl)  
  155.     {  
  156.       case 0:  
  157.           newtio.c_cflag &= ~CRTSCTS;  
  158.           break;  
  159.       case 1:  
  160.           newtio.c_cflag |= CRTSCTS;  
  161.           break;  
  162.       case 2:  
  163.           newtio.c_cflag |= IXON | IXOFF | IXANY;  
  164.           break;  
  165.     }  
  166.     switch (nEvent)  
  167.     {  
  168.       case 'O':  
  169.           newtio.c_cflag |= PARENB; //enble   
  170.           newtio.c_cflag |= PARODD;  
  171.           newtio.c_iflag |= (INPCK | ISTRIP);  
  172.           break;  
  173.       case 'E':  
  174.           newtio.c_iflag |= (INPCK | ISTRIP);  
  175.           newtio.c_cflag |= PARENB;  
  176.           newtio.c_cflag &= ~PARODD;  
  177.           break;  
  178.       case 'N':  
  179.           newtio.c_cflag &= ~PARENB;  
  180.           break;  
  181.     }  
  182.     switch (nSpeed)  
  183.     {  
  184.       case 2400:  
  185.           cfsetispeed(&newtio, B2400);  
  186.           cfsetospeed(&newtio, B2400);  
  187.           break;  
  188.       case 4800:  
  189.           cfsetispeed(&newtio, B4800);  
  190.           cfsetospeed(&newtio, B4800);  
  191.           break;  
  192.       case 9600:  
  193.           cfsetispeed(&newtio, B9600);  
  194.           cfsetospeed(&newtio, B9600);  
  195.           break;  
  196.       case 115200:  
  197.           cfsetispeed(&newtio, B115200);  
  198.           cfsetospeed(&newtio, B115200);  
  199.           break;  
  200.       default:  
  201.           cfsetispeed(&newtio, B9600);  
  202.           cfsetospeed(&newtio, B9600);  
  203.           break;  
  204.     }  
  205.     if (nStop == 1)  
  206.         newtio.c_cflag &= ~CSTOPB;  
  207.     else if (nStop == 2)  
  208.         newtio.c_cflag |= CSTOPB;  
  209.           
  210.     /* 修改此处参数,以保证正常接收 */  
  211.     newtio.c_cc[VTIME] = 0;  
  212.     newtio.c_cc[VMIN] = 0;  
  213.   
  214.     newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);  
  215.     newtio.c_oflag &= ~OPOST;  
  216.     tcflush(fd, TCIFLUSH);  
  217.     if ((tcsetattr(fd, TCSANOW, &newtio)) != 0)  
  218.     {  
  219.         perror("com set error");  
  220.         return -1;  
  221.     }  
  222.     printf("set done!\n");  
  223.     return 0;  
  224. }  
  225.   
  226. /************************************************* 
  227. * Function    :  
  228. * Description : 注意/dev/ttyAT1~6对应于电路图上的0~5 
  229. * Calls       :  
  230. * Called By   :  
  231. * Input       :  
  232. * Output      :  
  233. * Return      :  
  234. *************************************************/  
  235. int main(void)  
  236. {  
  237.     int fd = -1;  
  238.     int i, nread;  
  239.     char buff[64];  
  240.     if ((fd = open_port(fd, 5)) < 0)  
  241.     {  
  242.         perror("open_port erro");  
  243.         exit(1);  
  244.     }  
  245.     if ((i = set_opt(fd, 115200, 8, 0, 'N', 1)) < 0)  
  246.     {  
  247.         perror("set_opt error");  
  248.         return 1;  
  249.     }  
  250.     printf("ID IS: %d", fd);  
  251.     while (1)  
  252.     {  
  253.          memset(buff, 0, sizeof(buff));  
  254.          while ((nread = read(fd, buff, 64)) > 0)  
  255.          {  
  256.              write(fd, "Send Over!", 10);  
  257.              printf("\n%s\n", buff);  
  258.          }  
  259.     }  
  260.   
  261.     if (close(fd) < 0)  
  262.     {  
  263.         perror("close:");  
  264.         exit(1);  
  265.     }  
  266.     else  
  267.         printf("close ok");  
  268.     exit(0);  
  269. }  

相关内容