|
我参考了serial_text
弄了个初步的
编译通过了- /****************************************************************************
- ** Form implementation generated from reading ui file 'first.ui'
- **
- ** Created: Mon Aug 9 13:16:40 2010
- ** by: The User Interface Compiler (uic)
- **
- ** WARNING! All changes made in this file will be lost!
- ****************************************************************************/
- #include "first.h"
- #include <qlineedit.h>
- #include <qpushbutton.h>
- #include <qtextbrowser.h>
- #include <qlayout.h>
- #include <qvariant.h>
- #include <qtooltip.h>
- #include <qwhatsthis.h>
- #include "serial_test.h"
- #include <fcntl.h>
- #include <termios.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <ctype.h>
- #include <string.h>
- #include <time.h>
- #include <unistd.h>
- struct serial_config serialread;
- int speed_arr[] = {B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300,
- B38400, B19200, B9600, B4800, B2400, B1200, B300};
- int name_arr[] = {230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300,
- 38400, 19200, 9600, 4800, 2400, 1200, 300};
- static int serial_fd;
- /*
- * Constructs a first which is a child of 'parent', with the
- * name 'name' and widget flags set to 'f'
- */
- first::first( QWidget* parent, const char* name, WFlags fl )
- : QWidget( parent, name, fl )
- {
- if ( !name )
- setName( "first" );
- resize( 584, 480 );
- setCaption( tr( "EmbedSky_First_Qt_Program" ) );
- serial_init();
- serial_rw();
- usr_b = new QPushButton( this, "usr_b" );
- usr_b->setGeometry( QRect( 290, 30, 91, 31 ) );
- usr_b->setText( tr( "send" ) );
- TextBrowser1 = new QTextBrowser( this, "TextBrowser1" );
- TextBrowser1->setGeometry( QRect( 60, 30, 191, 104 ) );
- LineEdit2 = new QLineEdit( this, "LineEdit2" );
- LineEdit2->setGeometry( QRect( 60, 160, 190, 31 ) );
- // signals and slots connections
- connect( usr_b, SIGNAL( clicked() ), this, SLOT( user_botton() ) );
- }
- /*
- * Destroys the object and frees any allocated resources
- */
- first::~first()
- {
- // no need to delete child widgets, Qt does it all for us
- }
- void first::user_botton()
- {
- qWarning( "first::user_botton(): Not implemented yet!" );
- }
- //--------------------------------------------------
- //--------------------------------------------------
- void first::readserialcfg()
- {
- FILE *serial_fp;
- char j[10];
- printf("readserailcfg\n");
- serial_fp = fopen("/etc/tq2440_serial.cfg","r");
- if(NULL == serial_fp)
- {
- printf("can't open /etc/tq2440_serial.cfg");
- }
- else
- {
- fscanf(serial_fp, "SPEED=%s\n", j);
- serialread.serial_speed = atoi(j);
- fscanf(serial_fp, "DATABITS=%s\n", j);
- serialread.databits = atoi(j);
- fscanf(serial_fp, "STOPBITS=%s\n", j);
- serialread.stopbits = atoi(j);
- fscanf(serial_fp, "PARITY=%s\n", j);
- serialread.parity = j[0];
- }
- fclose(serial_fp);
- }
- //-----------------------------------------------
- //-----------------------------------------------
- void first::set_speed(int fd)
- {
- int i;
- int status;
- struct termios Opt;
- tcgetattr(fd, &Opt);
- // printf("serialread.speed is %d\n",serialread.serial_speed);
- for( i = 0; i < sizeof(speed_arr)/sizeof(int); i++)
- {
- if(serialread.serial_speed == name_arr[i])
- {
- tcflush(fd, TCIOFLUSH);
- cfsetispeed(&Opt, speed_arr[i]);
- cfsetospeed(&Opt, speed_arr[i]);
- status = tcsetattr(fd, TCSANOW, &Opt);
- if(status != 0)
- {
- perror("tcsetattr fd1");
- return;
- }
- tcflush(fd, TCIOFLUSH);
- }
- }
- }
- //-----------------------------------------------
- //-----------------------------------------------
- int first::set_Parity(int fd)
- {
- struct termios options;
- if(tcgetattr(fd, &options) != 0)
- {
- perror("SetupSerial 1");
- return(FALSE);
- }
- options.c_cflag &=~CSIZE;
- // printf("serialread.databits is %d\n",serialread.databits);
- switch(serialread.databits)
- {
- case 7:
- options.c_cflag |= CS7;
- break;
- case 8:
- options.c_cflag |= CS8;
- break;
- default:
- fprintf(stderr, "Unsupported data size\n");
- return(FALSE);
- }
- // printf("serialread.parity is %c\n",serialread.parity);
- switch(serialread.parity)
- {
- case 'n':
- case 'N':
- options.c_cflag &= ~PARENB;
- options.c_iflag &= ~INPCK;
- break;
- case 'o':
- case 'O':
- options.c_cflag |= (PARODD | PARENB);
- options.c_iflag |= INPCK;
- break;
- case 'e':
- case 'E':
- options.c_cflag |= PARENB;
- options.c_cflag &= ~PARODD;
- options.c_iflag |= INPCK;
- break;
- default:
- fprintf(stderr, "Unsupported parity\n");
- return(FALSE);
- }
- // printf("serialread.stopbits is %d\n",serialread.stopbits);
- switch(serialread.stopbits)
- {
- case 1:
- options.c_cflag &= ~CSTOPB;
- break;
- case 2:
- options.c_cflag |= CSTOPB;
- break;
- default:
- fprintf(stderr, "Unsupported stop bits\n");
- return(FALSE);
- }
- if(serialread.parity != 'n')
- options.c_iflag |= INPCK;
- options.c_cc[VTIME] = 150; //15 seconds
- options.c_cc[VMIN] = 0;
- tcflush(fd, TCIFLUSH);
- if(tcsetattr(fd, TCSANOW, &options) != 0)
- {
- perror("SetupSerial 3");
- return(FALSE);
- }
- return(TRUE);
- }
- //-----------------------------------------------
- //-----------------------------------------------
- int first::OpenDev(char *Dev)
- {
- int fd = open(Dev, O_RDWR);
- if(-1 == fd)
- {
- perror("Can't Open Serial Port");
- return -1;
- }
- else
- return fd;
- }
- void first::serial_init(void)
- {
- char *Dev;
- //int i;
- readserialcfg();
- //print_serialread();
- Dev = (char *)serialread.serial_dev;
- //Dev =serialread.serial_dev;//这句不通过。上面那句通过
-
- serial_fd = OpenDev(Dev);
- if(serial_fd > 0)
- set_speed(serial_fd);
- else
- {
- printf("Can't Open Serial Port!\n");
- exit(0);
- }
-
- if (fcntl(serial_fd, F_SETFL, O_NONBLOCK) < 0)
- {
- printf("fcntl failed!\n");
- exit(0);
- }
- #if 1
- if(isatty(STDIN_FILENO)==0)
- {
- printf("standard input is not a terminal device\n");
- }
- else
- printf("isatty success!\n");
- #endif
-
- if(set_Parity(serial_fd) == FALSE)
- {
- printf("Set parity Error\n");
- exit(1);
- }
- }
- void first::serial_rw()
- {
- int i;
- char buff[512];
- char buff2[] = "hello,TQ2440!\n";
- int nread,nwrite;
- nwrite = write(serial_fd,buff2,sizeof(buff2));
- printf("nwrite=%d\n",nwrite);
- while(1)
- {
- if((nread = read(serial_fd,buff,512))>0)
- {
- buff[nread] = '\0';
- #if 0
- write(serial_fd,buff,nread);
- #endif
- printf("\nrecv:%d\n",nread);
- #if 0
- for(i=0;i<nread;i++)
- {
- printf("%c",buff[i]);
- }
- printf("\n");
- #else
- printf("%s",buff);
- printf("\n");
- #endif
- }
- }
- close(serial_fd);
- }
复制代码 但不能启动
帮忙看看
能提点见意吗
3Q先啦 |
|