Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1300250
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类: 嵌入式

2012-05-11 16:01:50





点击(此处)折叠或打开

  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 <getopt.h>

  10. #define FALSE 1
  11. #define TRUE 0

  12. typedef unsigned int __u32;

  13. struct serial_rs485 {
  14.     __u32 flags;                    /* RS485 feature flags */
  15. #define SER_RS485_ENABLED            (1 << 0)
  16. #define SER_RS485_RTS_ON_SEND (1 << 1)
  17. #define SER_RS485_RTS_AFTER_SEND (1 << 2)
  18.     __u32 delay_rts_before_send; /* Milliseconds */
  19.     __u32 delay_rts_after_send; /* Milliseconds */
  20.     __u32 padding[6];                /* Memory is cheap, new structs are a royal PITA .. */
  21. };

  22. #define TIOCGRS485 0x542E //T
  23. #define TIOCSRS485 0x542F     //R


  24. struct serial_rs485 rs485;

  25. char *recchr="We received:\"";

  26. int speed_arr[] = {
  27.     B921600, B460800, B230400, B115200, B57600, B38400, B19200,
  28.     B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600,
  29.     B4800, B2400, B1200, B300,
  30. };
  31. int name_arr[] = {
  32.     921600, 460800, 230400, 115200, 57600, 38400, 19200,
  33.     9600, 4800, 2400, 1200, 300, 38400, 19200, 9600,
  34.     4800, 2400, 1200, 300,
  35. };

  36. void set_speed(int fd, int speed)
  37. {
  38.     int i;
  39.     int status;
  40.     struct termios Opt;
  41.     tcgetattr(fd, &Opt);

  42.     for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
  43.         if (speed == name_arr[i])    {
  44.             tcflush(fd, TCIOFLUSH);
  45.             cfsetispeed(&Opt, speed_arr[i]);
  46.             cfsetospeed(&Opt, speed_arr[i]);
  47.             status = tcsetattr(fd, TCSANOW, &Opt);
  48.             if (status != 0)
  49.                 perror("tcsetattr fd1");
  50.                 return;
  51.         }
  52.         tcflush(fd,TCIOFLUSH);
  53.    }
  54. }
  55. /*
  56.     *@brief 设置串口数据位,停止位和效验位
  57.     *@param fd 类型 int 打开的串口文件句柄*
  58.     *@param databits 类型 int 数据位 取值 为 7 或者8*
  59.     *@param stopbits 类型 int 停止位 取值为 1 或者2*
  60.     *@param parity 类型 int 效验类型 取值为N,E,O,,S
  61. */
  62. int set_Parity(int fd,int databits,int stopbits,int parity)
  63. {
  64.     struct termios options;
  65.     if ( tcgetattr( fd,&options) != 0) {
  66.         perror("SetupSerial 1");
  67.         return(FALSE);
  68.     }
  69.     options.c_cflag &= ~CSIZE ;
  70.     switch (databits) /*设置数据位数*/ {
  71.     case 7:
  72.         options.c_cflag |= CS7;
  73.     break;
  74.     case 8:
  75.         options.c_cflag |= CS8;
  76.     break;
  77.     default:
  78.         fprintf(stderr,"Unsupported data size\n");
  79.         return (FALSE);
  80.     }
  81.     
  82.     switch (parity) {
  83.     case 'n':
  84.     case 'N':
  85.         options.c_cflag &= ~PARENB; /* Clear parity enable */
  86.         options.c_iflag &= ~INPCK; /* Enable parity checking */
  87.     break;
  88.     case 'o':
  89.     case 'O':
  90.         options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
  91.         options.c_iflag |= INPCK; /* Disnable parity checking */
  92.     break;
  93.     case 'e':
  94.     case 'E':
  95.         options.c_cflag |= PARENB; /* Enable parity */
  96.         options.c_cflag &= ~PARODD; /* 转换为偶效验*/
  97.         options.c_iflag |= INPCK; /* Disnable parity checking */
  98.     break;
  99.     case 'S':    
  100.     case 's': /*as no parity*/
  101.         options.c_cflag &= ~PARENB;
  102.         options.c_cflag &= ~CSTOPB;
  103.     break;
  104.     default:
  105.         fprintf(stderr,"Unsupported parity\n");
  106.         return (FALSE);
  107.     }
  108.      /* 设置停止位*/
  109.       switch (stopbits) {
  110.        case 1:
  111.         options.c_cflag &= ~CSTOPB;
  112.       break;
  113.      case 2:
  114.           options.c_cflag |= CSTOPB;
  115.       break;
  116.      default:
  117.           fprintf(stderr,"Unsupported stop bits\n");
  118.           return (FALSE);
  119.      }
  120.       /* Set input parity option */
  121.       if (parity != 'n')
  122.         options.c_iflag |= INPCK;
  123.       options.c_cc[VTIME] = 150; // 15 seconds
  124.     options.c_cc[VMIN] = 0;

  125.     options.c_lflag &= ~(ECHO | ICANON);

  126.       tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
  127.       if (tcsetattr(fd,TCSANOW,&options) != 0) {
  128.         perror("SetupSerial 3");
  129.           return (FALSE);
  130.      }
  131.     return (TRUE);
  132. }

  133. /**
  134.     *@breif 打开串口
  135. */
  136. int OpenDev(char *Dev)
  137. {
  138.     int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
  139.      if (-1 == fd) { /*设置数据位数*/
  140.            perror("Can't Open Serial Port");
  141.            return -1;
  142.     } else
  143.         return fd;
  144. }


  145. /* The name of this program */
  146. const char * program_name;

  147. /* Prints usage information for this program to STREAM (typically
  148.  * stdout or stderr), and exit the program with EXIT_CODE. Does not
  149.  * return.
  150.  */

  151. void print_usage (FILE *stream, int exit_code)
  152. {
  153.     fprintf(stream, "Usage: %s option [ dev... ] \n", program_name);
  154.     fprintf(stream,
  155.             "\t-h --help Display this usage information.\n"
  156.             "\t-d --device The device ttyS[0-3] or ttyEXT[0-3]\n"
  157.             "\t-s --string Write the device data\n");
  158.     exit(exit_code);
  159. }



  160. /*
  161.     *@breif main()
  162.  */
  163. int main(int argc, char *argv[])
  164. {
  165.     int fd, next_option, havearg = 0;
  166.     char *device = "/dev/ttyS1"; /* Default device */

  167.      int nread;            /* Read the counts of data */
  168.      char buff[512];        /* Recvice data buffer */
  169.     pid_t pid;
  170.     char *xmit = "1234567890"; /* Default send data */

  171.     const char *const short_options = "hd:s:";

  172.     const struct option long_options[] = {
  173.         { "help", 0, NULL, 'h'},
  174.         { "device", 1, NULL, 'd'},
  175.         { "string", 1, NULL, 's'},
  176.         { NULL, 0, NULL, 0 }
  177.     };

  178.     program_name = argv[0];

  179.     do {
  180.         next_option = getopt_long (argc, argv, short_options, long_options, NULL);
  181.         switch (next_option) {
  182.             case 'h':
  183.                 print_usage (stdout, 0);
  184.             case 'd':
  185.                 device = optarg;
  186.                 havearg = 1;
  187.                 break;
  188.             case 's':
  189.                 xmit = optarg;
  190.                 havearg = 1;
  191.                 break;
  192.             case -1:
  193.                 if (havearg) break;
  194.             case '?':
  195.                 print_usage (stderr, 1);
  196.             default:
  197.                 abort ();
  198.         }
  199.     }while(next_option != -1);

  200.      sleep(1);
  201.      fd = OpenDev(device);

  202.      if (fd > 0) {
  203.           set_speed(fd, 19200);
  204.      } else {
  205.          fprintf(stderr, "Error opening %s: %s\n", device, strerror(errno));
  206.           exit(1);
  207.      }
  208.     if (set_Parity(fd,8,1,'N')== FALSE) {
  209.          fprintf(stderr, "Set Parity Error\n");
  210.         close(fd);
  211.           exit(1);
  212.        }


  213.     unsigned char tMsg[256], rMsg[256];
  214.     
  215.     memset(tMsg, 0X00, 256);
  216.     memset(rMsg, 0X00, 256);
  217.     
  218.     tMsg[0] = 0x02;
  219.     tMsg[1] = 0x00;
  220.     tMsg[2] = 0x00;
  221.     tMsg[3] = 0x04;
  222.     tMsg[4] = 0x15;
  223.     tMsg[5] = 0x10;
  224.     tMsg[6] = 0x03;
  225.     tMsg[7] = 0x1C;
  226.     tMsg[8] = 0x03;

  227.     pid = fork();    
  228.     if (pid < 0) {
  229.         fprintf(stderr, "Error in fork!\n");
  230.     } else if (pid == 0){
  231.         while(1) {
  232.             printf("SEND: %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",
  233.                     tMsg[0],
  234.                     tMsg[1],
  235.                     tMsg[2],
  236.                     tMsg[3],
  237.                     tMsg[4],
  238.                     tMsg[5],
  239.                     tMsg[6],
  240.                     tMsg[7],
  241.                     tMsg[8]);

  242.     
  243.             write(fd, tMsg, 9);
  244.             sleep(1);
  245.         }
  246.         exit(0);
  247.     } else {
  248.         while(1) {

  249.             nread = read(fd, rMsg, 9);
  250.             if (nread > 0) {
  251.                 printf("READ: %02X %02X %02X %02X %02X %02X %02X %02X %02X\n


点击(此处)折叠或打开

  1. rMsg[0],
  2.                         rMsg[1],
  3.                         rMsg[2],
  4.                         rMsg[3],
  5.                         rMsg[4],
  6.                         rMsg[5],
  7.                         rMsg[6],
  8.                         rMsg[7],
  9.                         rMsg[8]);

  10.             }
  11.         }    
  12.     }
  13.     close(fd);
  14.     exit(0);
  15. }

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