嵌入式爱好者

查看: 15575|回复: 3

[Linux] 求6410b的rs485的测试应用程序(Linux)

[复制链接]
jefferyhappy 该用户已被删除
发表于 2014-8-11 14:32:15 | 显示全部楼层 |阅读模式
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

7

主题

160

帖子

242

积分

发表于 2014-8-12 11:01:48 | 显示全部楼层
  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. #include <string.h>
  11. #include <stdlib.h>

  12. #define TITLE "485 TEST"
  13. #define FALSE 1
  14. #define TRUE 0
  15. #define RS485_SEND "echo 1 > /sys/class/misc/io_ctl/gpio_state"
  16. #define RS485_RECEIVE "echo 0 > /sys/class/misc/io_ctl/gpio_state"
  17. FILE *outfile;
  18. void print_usage();
  19. char buf[512] = {0};/* Recvice data buffer */
  20. char message[104] = {0};
  21. int  fd = 0;
  22. static int  hex = 0;

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

  31. void shownInformation(void)
  32. {
  33.         printf("*********************************************\n");
  34.         printf("\t %s \t\n", TITLE);
  35.         printf("*********************************************\n\n");
  36. }

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

  42.         tcgetattr(fd, &Opt);

  43.         cfmakeraw(&Opt);
  44.         for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++) {
  45.                 if  (speed == name_arr[i]){
  46.                         tcflush(fd, TCIOFLUSH);
  47.                         cfsetispeed(&Opt, speed_arr[i]);
  48.                         cfsetospeed(&Opt, speed_arr[i]);
  49.                         status = tcsetattr(fd, TCSANOW, &Opt);
  50.                         if (status != 0)
  51.                                 perror("tcsetattr fd1");
  52.                         return;
  53.                 }
  54.                 tcflush(fd,TCIOFLUSH);
  55.         }

  56.         if (i == 12){
  57.                 printf("\tSorry, please set the correct baud rate!\n\n");
  58.                 print_usage(stderr, 1);
  59.         }
  60. }
  61. /*
  62.         *@brief   设置串口数据位,停止位和效验位
  63.         *@param  fd     类型  int  打开的串口文件句柄*
  64.         *@param  databits 类型  int 数据位   取值 为 7 或者8*
  65.         *@param  stopbits 类型  int 停止位   取值为 1 或者2*
  66.         *@param  parity  类型  int  效验类型 取值为N,E,O,,S
  67. */
  68. int set_Parity(int fd,int databits,int stopbits,int parity)
  69. {
  70.         struct termios options;

  71.         if  ( tcgetattr( fd,&options)  !=  0) {
  72.                 perror("SetupSerial 1");
  73.                 return(FALSE);
  74.         }
  75.        
  76.         options.c_cflag &= ~CSIZE ;
  77.         options.c_oflag = 0;
  78.         switch (databits) /*设置数据位数*/ {
  79.         case 7:
  80.                 options.c_cflag |= CS7;
  81.                 break;
  82.         case 8:
  83.                 options.c_cflag |= CS8;
  84.                 break;
  85.         default:
  86.                 fprintf(stderr,"Unsupported data size\n");
  87.                 return (FALSE);
  88.         }
  89.        
  90.         switch (parity) {
  91.         case 'n':
  92.         case 'N':
  93.                 options.c_cflag &= ~PARENB;   /* Clear parity enable */
  94.                 options.c_iflag &= ~INPCK;     /* Enable parity checking */
  95.                 break;
  96.         case 'o':
  97.         case 'O':
  98.                 options.c_cflag |= (PARODD | PARENB);  /* 设置为奇效验*/
  99.                 options.c_iflag |= INPCK;             /* Disnable parity checking */
  100.                 break;
  101.         case 'e':
  102.         case 'E':
  103.                 options.c_cflag |= PARENB;     /* Enable parity */
  104.                 options.c_cflag &= ~PARODD;   /* 转换为偶效验*/
  105.                 options.c_iflag |= INPCK;       /* Disnable parity checking */
  106.                 break;
  107.         case 'S':       
  108.         case 's':  /*as no parity*/
  109.                 options.c_cflag &= ~PARENB;
  110.                 options.c_cflag &= ~CSTOPB;
  111.                 break;
  112.         default:
  113.                 fprintf(stderr,"Unsupported parity\n");
  114.                 return (FALSE);
  115.         }
  116.         /* 设置停止位*/  
  117.         switch (stopbits) {
  118.         case 1:
  119.                 options.c_cflag &= ~CSTOPB;
  120.                 break;
  121.         case 2:
  122.                 options.c_cflag |= CSTOPB;
  123.                 break;
  124.         default:
  125.                 fprintf(stderr,"Unsupported stop bits\n");
  126.                 return (FALSE);
  127.         }
  128.         /* Set input parity option */
  129.         if (parity != 'n')
  130.                 options.c_iflag |= INPCK;

  131.         options.c_cc[VTIME] = 150; // 15 seconds
  132.         options.c_cc[VMIN] = 0;

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

  134.         tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
  135.         if (tcsetattr(fd,TCSANOW,&options) != 0) {
  136.                 perror("SetupSerial 3");
  137.                 return (FALSE);
  138.         }
  139.         return (TRUE);
  140. }

  141. /**
  142. *@breif 打开串口
  143. */
  144. int OpenDev(char *Dev)
  145. {
  146.         int fd = open( Dev, O_RDWR );         //| O_NOCTTY | O_NDELAY
  147.         if (-1 == fd) { /*设置数据位数*/
  148.                 perror("Can't Open Serial Port");
  149.                 return -1;
  150.         } else{
  151.                 return fd;
  152.         }
  153. }


  154. /* The name of this program */
  155. const char * program_name;

  156. /* Prints usage information for this program to STREAM (typically
  157. * stdout or stderr), and exit the program with EXIT_CODE. Does not
  158. * return.
  159. */

  160. void print_usage (FILE *stream, int exit_code)
  161. {
  162.         fprintf(stream, "Usage: %s option [ dev... ] \n", program_name);
  163.         fprintf(stream,
  164.                         "\t-h  --help     Display this usage information.\n"
  165.                         "\t-d i --device   The device ttyS[0-3] or ttyO[0-5]\n"
  166.                             "\t-b  --baudrate Set the baud rate you can select\n"
  167.                             "\t               [230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300]\n"
  168.                         "\t-s  --string   Write the device data\n"
  169.                         "\t-l  --hexadecimal  recv in hexadecimal");
  170.         exit(exit_code);
  171. }

  172. void *thread_receive_function(void *arg)
  173. {
  174.         unsigned int i = 0, j = 1,num_read;
  175. //        outfile = fopen("./tmp","w+");
  176.         tcflush(fd, TCIOFLUSH); //清空串口缓冲区数据
  177.         while(1){
  178.                 num_read = read(fd, buf, sizeof(buf));
  179.                 if (num_read > 0){
  180.                         if(hex == 1)
  181.                         {
  182.                                 printf("sum = %4d num = %2d recv = ",j++,num_read);
  183.                                 for(i = 0;i< num_read;i++)
  184.                                 {
  185.                                         printf("%x",buf[i]);
  186.                                 }
  187.                                 printf("\n");
  188.                                 memset(buf,'\0',sizeof(buf));
  189.                         }
  190.                         else
  191.                         {
  192. //                        fprintf(outfile,"receive num = %d ",i++);
  193.                                 buf[num_read] = '\n';
  194. //                        fwrite(buf,num_read,1,outfile);
  195. //                        fprintf(outfile,"%s","\n");
  196.                                 printf("sum = %4d num = %2d recv = %s", j++,num_read, buf);
  197.                                 memset(buf, '\0', sizeof(buf));
  198.                         }
  199.                 }
  200.         }
  201. }

  202. void func_receive()
  203. {
  204.         int res;
  205.         int stop;
  206.         pthread_t a_thread;
  207.        
  208.         system(RS485_RECEIVE);
  209.         usleep(100);

  210.         res = pthread_create(&a_thread, NULL, thread_receive_function, NULL);
  211.         if (res != 0){
  212.                 perror("Thread creation failed");
  213.                 exit(1);
  214.         }
  215.     usleep(100);
  216.         while (1){
  217.                 printf("Select 3 : Stop Receive\n");
  218.                 printf(">\n");
  219.                 scanf ("%d", &stop);
  220.                 getchar();
  221.                 if (stop == 3){
  222.         /*                fclose(outfile);*/
  223.                         res = pthread_cancel(a_thread);

  224.                         if (res != 0){
  225.                                 perror("Thread cancelation failed");
  226.                                 exit(1);
  227.                         }
  228.                         break;
  229.                 }else{
  230.                         printf("Sorry! Please select [3] stop receive\n\n");
  231.                 }
  232.         }
  233. }

  234. void *thread_send_function(void *arg)
  235. {
  236.         unsigned int num_send = 0, j = 0;
  237.         tcflush(fd, TCIOFLUSH); //清空串口缓冲区数据
  238.         while (1){
  239.                   j = write(fd, message, strlen(message));
  240.                                   num_send++;
  241.                                   printf("sum = %4d num = %2d send = %s\n",num_send,j,message );
  242.                   sleep(1);
  243.         }
  244. }

  245. void func_send()
  246. {
  247.         unsigned char len;
  248.         int stop;
  249.         pthread_t a_thread;
  250.         int res;

  251.         printf("\tPlease enter the information to be sent off!\n");

  252.         system(RS485_SEND);//控制GPIO0_13引脚电平使485允许发送,禁止接收。
  253.         usleep(100);

  254.         while (1){
  255.                 memset (message, '\0', sizeof(message));
  256.                 scanf("%[^\n]", message);
  257.                 printf("message = %s\n", message);
  258.                 len = strlen(message);
  259.                 printf("len = %d\n", len);

  260.                 if (len > 0){
  261.                         res = pthread_create(&a_thread, NULL, thread_send_function, (void *)message);
  262.                         if (res != 0){
  263.                                 perror("Thread creation failed");
  264.                                 exit(1);
  265.                         }

  266.                         while (1){
  267.                                 printf("Information is sent......\n");
  268.                                 printf("Select 3 : Stop Send\n");
  269.                                 printf(">");
  270.                                 scanf ("%d", &stop);
  271.                                 getchar();
  272.                                 if (stop == 3){
  273.                                         res = pthread_cancel(a_thread);

  274.                                         if (res != 0){
  275.                                                 perror("Thread cancelation failed");
  276.                                                 exit(1);
  277.                                         }               

  278.                                         break;
  279.                                 }else{
  280.                                         printf("Sorry! Please select [3] stop send\n\n");
  281.                                 }
  282.                         }
  283.                         break;
  284.                 }else{
  285.                         printf("Sorry you can not enter an empty message!\n\n");
  286.                 }
  287.         }
  288. }

  289. /*显示二级菜单*/
  290. void display_two_menu()
  291. {
  292.         int flag_num;

  293.         while (1){
  294.                 printf("\nSelect 1 : Send a message\n");
  295.                 printf("Select 2 : Receive messages\n");
  296.                 printf(">");
  297.        
  298.                 scanf("%d", &flag_num);
  299.                 getchar();
  300.                
  301.                 switch (flag_num){
  302.                 case 1 :
  303.                         func_send();
  304.                         break;
  305.                 case 2 :
  306.                         func_receive();
  307.                         break;
  308.                 default :
  309.                         printf("\n\nSorry! Please select the number of 1 to 2...\n");
  310.                 }
  311.         }
  312. }

  313. void funct_select()
  314. {       
  315.         display_two_menu();
  316. }


  317. /*
  318. *@breif  main()
  319. */
  320. int main(int argc, char *argv[])
  321. {
  322.         int   next_option, havearg = 0;
  323.         char *device;
  324.         int i=0,j=0;
  325.         int nread;                /* Read the counts of data */
  326.         int speed = 0;
  327.         char *xmit = "6";         /* Default send data */

  328.         const char *const short_options = "lhd:s:b:";

  329.         const struct option long_options[] = {
  330.                 { "help",   0, NULL, 'h'},
  331.                 { "device", 1, NULL, 'd'},
  332.                 { "string", 1, NULL, 's'},
  333.                 { "baudrate", 1, NULL, 'b'},
  334.                 {"hexadecimal",0,NULL,'l'},
  335.                 { NULL,     0, NULL, 0  }
  336.         };

  337.         program_name = argv[0];

  338.         do {
  339.                 next_option = getopt_long (argc, argv, short_options, long_options, NULL);
  340.                 switch (next_option) {
  341.                 case 'h':
  342.                         print_usage (stdout, 0);
  343.                 case 'd':
  344.                         device = optarg;
  345.                         havearg = 1;
  346.                         break;
  347.                 case 'b':
  348.                         speed = atoi(optarg);
  349.                         break;
  350.                 case 's':
  351.                         xmit = optarg;
  352.                         havearg = 1;
  353.                         break;
  354.                 case 'l':
  355.                         hex = 1;
  356.                         havearg = 1;
  357.                         break;
  358.                 case -1:
  359.                         if (havearg)  
  360.                                 break;
  361.                 case '?':
  362.                         print_usage (stderr, 1);
  363.                         default:
  364.                         abort ();
  365.                 }
  366.         }while(next_option != -1);

  367.         sleep(1);
  368.         fd = OpenDev(device);

  369.         shownInformation();

  370.         if (fd > 0) {
  371.                 set_speed(fd, speed);
  372.         } else {
  373.                 fprintf(stderr, "Error opening %s: %s\n", device, strerror(errno));
  374.                 exit(1);
  375.         }

  376.         if (set_Parity(fd,8,1,'N')== FALSE) {
  377.                 fprintf(stderr, "Set Parity Error\n");
  378.                 close(fd);
  379.                 exit(1);
  380.         }

  381.         funct_select();

  382.         close(fd);
  383.         exit(0);
  384. }
复制代码
点评回复 支持 反对

使用道具 举报

jefferyhappy 该用户已被删除
 楼主| 发表于 2014-8-26 09:48:13 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
点评回复 支持 反对

使用道具 举报

153

主题

3910

帖子

4207

积分

AM5718通行证AM335x通行证i.MX6UL通行证i.MX RT通行证i.MX6Q通行证XX18通行证TCU通行证FCU1401通行证FCU1301通行证FCU11xx通行证

发表于 2014-8-26 13:46:37 | 显示全部楼层
楼主,我们测试rs485是通过串口测试程序测试的,没有单独针对485的测试例程
点评回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋| 飞凌嵌入式 ( 冀ICP备12004394号-1 )

GMT+8, 2025-4-12 13:19

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表