天嵌 ARM开发社区

 找回密码
 注册
查看: 2164|回复: 2

关天串口助手的问题

[复制链接]
liyayao 发表于 2010-8-9 18:41:05 | 显示全部楼层 |阅读模式
我参考了serial_text
弄了个初步的
编译通过了
  1. /****************************************************************************
  2. ** Form implementation generated from reading ui file 'first.ui'
  3. **
  4. ** Created: Mon Aug 9 13:16:40 2010
  5. **      by:  The User Interface Compiler (uic)
  6. **
  7. ** WARNING! All changes made in this file will be lost!
  8. ****************************************************************************/
  9. #include "first.h"

  10. #include <qlineedit.h>
  11. #include <qpushbutton.h>
  12. #include <qtextbrowser.h>
  13. #include <qlayout.h>
  14. #include <qvariant.h>
  15. #include <qtooltip.h>
  16. #include <qwhatsthis.h>
  17. #include "serial_test.h"
  18. #include <fcntl.h>
  19. #include <termios.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include <time.h>
  28. #include <unistd.h>


  29. struct serial_config serialread;
  30. int speed_arr[] = {B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300,
  31.                    B38400, B19200, B9600, B4800, B2400, B1200, B300};

  32. int name_arr[] = {230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300,
  33.                   38400, 19200, 9600, 4800, 2400, 1200, 300};
  34. static int serial_fd;

  35. /*
  36. *  Constructs a first which is a child of 'parent', with the
  37. *  name 'name' and widget flags set to 'f'
  38. */
  39. first::first( QWidget* parent,  const char* name, WFlags fl )
  40.     : QWidget( parent, name, fl )
  41. {
  42.     if ( !name )
  43.         setName( "first" );
  44.     resize( 584, 480 );
  45.     setCaption( tr( "EmbedSky_First_Qt_Program" ) );

  46.     serial_init();
  47.     serial_rw();

  48.     usr_b = new QPushButton( this, "usr_b" );
  49.     usr_b->setGeometry( QRect( 290, 30, 91, 31 ) );
  50.     usr_b->setText( tr( "send" ) );

  51.     TextBrowser1 = new QTextBrowser( this, "TextBrowser1" );
  52.     TextBrowser1->setGeometry( QRect( 60, 30, 191, 104 ) );

  53.     LineEdit2 = new QLineEdit( this, "LineEdit2" );
  54.     LineEdit2->setGeometry( QRect( 60, 160, 190, 31 ) );

  55.     // signals and slots connections
  56.     connect( usr_b, SIGNAL( clicked() ), this, SLOT( user_botton() ) );
  57. }

  58. /*  
  59. *  Destroys the object and frees any allocated resources
  60. */
  61. first::~first()
  62. {
  63.     // no need to delete child widgets, Qt does it all for us
  64. }

  65. void first::user_botton()
  66. {
  67.     qWarning( "first::user_botton(): Not implemented yet!" );
  68. }

  69. //--------------------------------------------------

  70. //--------------------------------------------------
  71. void first::readserialcfg()
  72. {
  73.       FILE *serial_fp;
  74.       char j[10];
  75.       printf("readserailcfg\n");
  76.       serial_fp = fopen("/etc/tq2440_serial.cfg","r");
  77.       if(NULL == serial_fp)
  78.       {
  79.             printf("can't open /etc/tq2440_serial.cfg");
  80.       }
  81.       else
  82.   {
  83.        fscanf(serial_fp, "SPEED=%s\n", j);
  84.        serialread.serial_speed = atoi(j);
  85.        fscanf(serial_fp, "DATABITS=%s\n", j);
  86.        serialread.databits = atoi(j);
  87.        fscanf(serial_fp, "STOPBITS=%s\n", j);
  88.        serialread.stopbits = atoi(j);
  89.        fscanf(serial_fp, "PARITY=%s\n", j);
  90.        serialread.parity = j[0];
  91.   }
  92.   fclose(serial_fp);
  93. }
  94. //-----------------------------------------------

  95. //-----------------------------------------------
  96. void first::set_speed(int fd)
  97. {
  98.       int i;
  99.       int status;
  100.       struct termios Opt;
  101.       tcgetattr(fd, &Opt);
  102. // printf("serialread.speed is %d\n",serialread.serial_speed);
  103.       for( i = 0; i < sizeof(speed_arr)/sizeof(int); i++)
  104.       {
  105.              if(serialread.serial_speed == name_arr[i])
  106.              {
  107.                    tcflush(fd, TCIOFLUSH);
  108.                    cfsetispeed(&Opt, speed_arr[i]);
  109.                    cfsetospeed(&Opt, speed_arr[i]);
  110.                    status = tcsetattr(fd, TCSANOW, &Opt);
  111.                    if(status != 0)
  112.                    {
  113.                          perror("tcsetattr fd1");
  114.                          return;
  115.                    }
  116.                    tcflush(fd, TCIOFLUSH);
  117.              }
  118.       }
  119. }
  120. //-----------------------------------------------
  121. //-----------------------------------------------
  122. int first::set_Parity(int fd)
  123. {
  124.       struct termios options;
  125.       if(tcgetattr(fd, &options) != 0)
  126.       {
  127.              perror("SetupSerial 1");
  128.              return(FALSE);
  129.       }
  130.    options.c_cflag &=~CSIZE;
  131. // printf("serialread.databits is %d\n",serialread.databits);
  132.    switch(serialread.databits)
  133.    {
  134.          case 7:
  135.                options.c_cflag |= CS7;
  136.                break;
  137.          case 8:
  138.                options.c_cflag |= CS8;
  139.                break;
  140.          default:
  141.                fprintf(stderr, "Unsupported data size\n");
  142.                return(FALSE);
  143.    }
  144. // printf("serialread.parity is %c\n",serialread.parity);
  145.    switch(serialread.parity)
  146.    {
  147.          case 'n':
  148.          case 'N':
  149.                options.c_cflag &= ~PARENB;
  150.                options.c_iflag &= ~INPCK;
  151.                break;
  152.          case 'o':
  153.          case 'O':
  154.                options.c_cflag |= (PARODD | PARENB);
  155.                options.c_iflag |= INPCK;
  156.                break;
  157.          case 'e':
  158.          case 'E':
  159.                options.c_cflag |= PARENB;
  160.                options.c_cflag &= ~PARODD;
  161.                options.c_iflag |= INPCK;
  162.                break;
  163.          default:
  164.                fprintf(stderr, "Unsupported parity\n");
  165.                return(FALSE);
  166.    }
  167. // printf("serialread.stopbits is %d\n",serialread.stopbits);
  168.    switch(serialread.stopbits)
  169.    {
  170.          case 1:
  171.                options.c_cflag &= ~CSTOPB;
  172.                break;
  173.          case 2:
  174.                options.c_cflag |= CSTOPB;
  175.                break;
  176.          default:
  177.                fprintf(stderr, "Unsupported stop bits\n");
  178.                return(FALSE);
  179.    }
  180.    if(serialread.parity != 'n')
  181.          options.c_iflag |= INPCK;
  182.    options.c_cc[VTIME] = 150;                 //15 seconds
  183.    options.c_cc[VMIN] = 0;
  184.    tcflush(fd, TCIFLUSH);
  185.    if(tcsetattr(fd, TCSANOW, &options) != 0)
  186.    {
  187.              perror("SetupSerial 3");
  188.             return(FALSE);
  189.       }
  190.       return(TRUE);
  191. }
  192. //-----------------------------------------------
  193. //-----------------------------------------------
  194. int first::OpenDev(char *Dev)
  195. {
  196.       int fd = open(Dev, O_RDWR);
  197.       if(-1 == fd)
  198.       {
  199.             perror("Can't Open Serial Port");
  200.             return -1;
  201.       }
  202.       else
  203.             return fd;
  204. }

  205. void first::serial_init(void)
  206. {
  207.         char *Dev;

  208.         //int i;

  209.         readserialcfg();
  210.         //print_serialread();

  211.         Dev = (char *)serialread.serial_dev;
  212.         //Dev =serialread.serial_dev;//这句不通过。上面那句通过
  213.        
  214.         serial_fd = OpenDev(Dev);

  215.         if(serial_fd > 0)
  216.                 set_speed(serial_fd);               
  217.         else
  218.         {
  219.                 printf("Can't Open Serial Port!\n");
  220.                 exit(0);
  221.         }
  222.        
  223.         if (fcntl(serial_fd, F_SETFL, O_NONBLOCK) < 0)
  224.         {
  225.                 printf("fcntl failed!\n");
  226.                 exit(0);
  227.         }
  228. #if 1       
  229.         if(isatty(STDIN_FILENO)==0)
  230.         {
  231.                 printf("standard input is not a terminal device\n");
  232.         }
  233.         else
  234.                 printf("isatty success!\n");
  235. #endif
  236.        
  237.         if(set_Parity(serial_fd) == FALSE)
  238.         {
  239.                 printf("Set parity Error\n");
  240.                 exit(1);
  241.         }

  242. }

  243. void first::serial_rw()
  244. {
  245.         int i;
  246.         char buff[512];
  247.         char buff2[] = "hello,TQ2440!\n";
  248.         int nread,nwrite;

  249.         nwrite = write(serial_fd,buff2,sizeof(buff2));
  250.         printf("nwrite=%d\n",nwrite);
  251.         while(1)
  252.         {
  253.                 if((nread = read(serial_fd,buff,512))>0)
  254.                 {
  255.                         buff[nread] = '\0';
  256. #if 0       
  257.                         write(serial_fd,buff,nread);
  258. #endif
  259.                         printf("\nrecv:%d\n",nread);
  260. #if 0
  261.                         for(i=0;i<nread;i++)
  262.                         {
  263.                                 printf("%c",buff[i]);
  264.                         }
  265.                         printf("\n");
  266. #else
  267.                         printf("%s",buff);
  268.                         printf("\n");
  269. #endif
  270.                 }
  271.         }

  272.         close(serial_fd);

  273. }



复制代码
但不能启动
帮忙看看
能提点见意吗
3Q先啦
 楼主| liyayao 发表于 2010-8-19 22:40:54 | 显示全部楼层
:'(ddddddddddddddddddddddd
machoe 发表于 2010-8-20 15:58:50 | 显示全部楼层
能把程序用附件发上来吗,我帮你调一下看看。
执行的时候有什么信息打印出来吗?一并帖出来吧。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2024-9-27 21:05 , Processed in 1.031290 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

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