Chinaunix首页 | 论坛 | 博客
  • 博客访问: 520647
  • 博文数量: 56
  • 博客积分: 1136
  • 博客等级: 少尉
  • 技术积分: 1378
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-28 14:02
文章存档

2014年(1)

2013年(7)

2012年(45)

2011年(3)

分类: 嵌入式

2013-05-29 22:59:29




点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <errno.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <termios.h>
  9. #include <stdlib.h>
  10. #include "Serial.h"

  11. int main (void)
  12. {
  13.     int fd;
  14.     int nread, i;
  15.     char buff[] = "Hellon";
  16.     if ((fd = open_port (fd, 1)) < 0)
  17.     {
  18.         perror ("open_port error");
  19.           return;
  20.     }
  21.   
  22.     if ((i = set_opt (fd, 115200, 8, 'N', 1)) < 0)
  23.     {
  24.         perror ("set_opt error");
  25.           return;
  26.     }
  27.   
  28.     printf ("fd=%dn", fd);
  29.   
  30.     while (1)
  31.     {
  32.         nread = write (fd, buff, 5);
  33.           printf ("nread=%d,%sn", nread, buff);
  34.           sleep (1);
  35.     }
  36.     close (fd);
  37.     return;
  38. }



点击(此处)折叠或打开

  1. #ifndef SERI_H
  2. #define SERI_H

  3. int open_port (int fd, int comport);

  4. int set_opt (int fd, int nSpeed, int nBits, char nEvent, int nStop);

  5. #endif


点击(此处)折叠或打开

  1. int set_opt (int fd, int nSpeed, int nBits, char nEvent, int nStop)
  2. {
  3.     struct termios newtio, oldtio;
  4.     if (tcgetattr (fd, &oldtio) != 0)
  5.     {
  6.         perror ("SetupSerial 1");
  7.         return -1;
  8.     }
  9.     bzero (&newtio, sizeof (newtio));
  10.     newtio.c_cflag |= CLOCAL | CREAD;
  11.     newtio.c_cflag &= ~CSIZE;

  12.     switch (nBits)
  13.     {
  14.      case 7:
  15.             newtio.c_cflag |= CS7;
  16.             break;
  17.      case 8:
  18.             newtio.c_cflag |= CS8;
  19.             break;
  20.     }
  21.   
  22.     switch (nEvent)
  23.     {
  24.      case 'O':
  25.             newtio.c_cflag |= PARENB;
  26.             newtio.c_cflag |= PARODD;
  27.             newtio.c_iflag |= (INPCK | ISTRIP);
  28.      break;
  29.      case 'E':
  30.             newtio.c_iflag |= (INPCK | ISTRIP);
  31.             newtio.c_cflag |= PARENB;
  32.             newtio.c_cflag &= ~PARODD;
  33.             break;
  34.     
  35.         case 'N':
  36.             newtio.c_cflag &= ~PARENB;
  37.         break;
  38.     }

  39.     switch (nSpeed)
  40.     {
  41.      case 2400:
  42.             cfsetispeed (&newtio, B2400);
  43.             cfsetospeed (&newtio, B2400);
  44.             break;
  45.      case 4800:
  46.             cfsetispeed (&newtio, B4800);
  47.             cfsetospeed (&newtio, B4800);
  48.             break;
  49.      case 9600:
  50.             cfsetispeed (&newtio, B9600);
  51.             cfsetospeed (&newtio, B9600);
  52.             break;
  53.      case 115200:
  54.             cfsetispeed (&newtio, B115200);
  55.             cfsetospeed (&newtio, B115200);
  56.             break;
  57.      case 460800:
  58.             cfsetispeed (&newtio, B460800);
  59.             cfsetospeed (&newtio, B460800);
  60.             break;
  61.      default:
  62.             cfsetispeed (&newtio, B9600);
  63.             cfsetospeed (&newtio, B9600);
  64.             break;
  65.     }
  66.   
  67.     if (nStop == 1)
  68.         newtio.c_cflag &= ~CSTOPB;
  69.     else if (nStop == 2)
  70.         newtio.c_cflag |= CSTOPB;
  71.     newtio.c_cc[VTIME] = 0;
  72.     newtio.c_cc[VMIN] = 0;
  73.     tcflush (fd, TCIFLUSH);
  74.     if ((tcsetattr (fd, TCSANOW, &newtio)) != 0)
  75.     {
  76.         perror ("com set error");
  77.         return -1;
  78.     }
  79.   
  80.     printf ("set done!n");
  81.     return 0;
  82. }

  83. int open_port (char *dev )
  84. {
  85.     
  86.     fd = open (dev, O_RDWR | O_NOCTTY | O_NDELAY);
  87.     if (-1 == fd)
  88.     {
  89.         perror ("Can't Open Serial Port /dev/ttySAC0");
  90.         return (-1);
  91.     }
  92.     else    
  93.         return fd;
  94. }


阅读(1371) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~