/****************************************************************************
** Form implementation generated from reading ui file 'Form1.ui'
**
** Created: 五 5月 11 18:42:26 2012
** by: The User Interface Compiler ($Id: qt/main.cpp 3.1.1 edited Nov 21 17:40 $)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#include "Form1.h"
#include <qvariant.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include "com.h"
#include <string.h>
#include <stdlib.h>
#include "Form2.h"
class QListBox;
int speed_arr[]={B115200,B57600,B38400,B19200,B9600,B4800,B2400,B1200,B300};
int name_arr[]={115200,57600,38400,19200,9600,4800,2400,1200,300};
/*
* Constructs a Form1 as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*/
Form1::Form1( QWidget* parent, const char* name, WFlags fl )
: QWidget( parent, name, fl )
{
if ( !name )
setName( "Form1" );
create_COMSetting();
PushButton1 = new QPushButton( this, "PushButton1" );
PushButton1->setGeometry( QRect( 90, 50, 108, 32 ) );
QObject::connect(PushButton1, SIGNAL(clicked()), this, SLOT(Set_COM()));
TextLabel1_2 = new QLabel( this, "TextLabel1_2" );
TextLabel1_2->setGeometry( QRect( 50, 90, 61, 20 ) );
PushButton1_2_2 = new QPushButton( this, "PushButton1_2_2" );
PushButton1_2_2->setGeometry( QRect( 130, 170, 70, 32 ) );
QObject::connect(PushButton1_2_2, SIGNAL(clicked()), this, SLOT(Clear()));
PushButton1_2_3 = new QPushButton( this, "PushButton1_2_3" );
PushButton1_2_3->setGeometry( QRect( 220, 170, 70, 32 ) );
QObject::connect(PushButton1_2_3, SIGNAL(clicked()), this, SLOT(Quit()));
QObject::connect(COM_Setting->PushButton5, SIGNAL(clicked()), COM_Setting, SLOT(Set_COM_Back()));
PushButton1_2 = new QPushButton( this, "PushButton1_2" );
PushButton1_2->setGeometry( QRect( 30, 170, 70, 32 ) );
QObject::connect(PushButton1_2, SIGNAL(clicked()), this, SLOT(exitMain()));
TextLabel1 = new QLabel( this, "TextLabel1" );
TextLabel1->setGeometry( QRect( 30, 10, 61, 20 ) );
LineEdit1 = new QLineEdit( this, "LineEdit1" );
LineEdit1->setGeometry( QRect( 30, 120, 250, 23 ) );
languageChange();
resize( QSize(312, 224).expandedTo(minimumSizeHint()) );
}
/*
* Destroys the object and frees any allocated resources
*/
Form1::~Form1()
{
// no need to delete child widgets, Qt does it all for us
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void Form1::Set_COM()
{
COM_Setting->show();
}
void Form1::exitMain()
{
int fd,nset;
int nwrite;
char *dev="/dev/ttySAC0";
fd=OpenDev(dev);
Speed=COM_Setting->Speed_COM;
Databits=COM_Setting->Databits_COM;
if(fd>0)
{
set_speed(fd,Speed);
nset=set_Parity(fd,Databits,1,'N');
}
QString str2=LineEdit1->text();
int lenbuff2=str2.length();
const char *buff2;
if(lenbuff2<=0)
{
buff2="(NO COMMENT INPUTTED)";
lenbuff2=20;
}
else
buff2=str2.latin1();
while((nwrite=write(fd,buff2,lenbuff2)<=0))
{;};
::close(fd);
}
void Form1::Clear()
{
LineEdit1->clear();
}
void Form1::Quit()
{
//QApplication::exit();
}
void Form1::create_COMSetting()
{
COM_Setting = new Form2;
COM_Setting -> hide();
}
void Form1::languageChange()
{
setCaption( tr( "Form1" ) );
PushButton1->setText( tr( "Set COM" ) );
TextLabel1_2->setText( tr( "Input->" ) );
PushButton1_2_2->setText( tr( "Clear" ) );
PushButton1_2_3->setText( tr( "Quit" ) );
PushButton1_2->setText( tr( "Send" ) );
TextLabel1->setText( tr( "Setting->" ) );
}
void set_speed(int fd, int speed)
{
unsigned int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for (i=0;i<sizeof(speed_arr)/sizeof(int); i++)
{
if(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 set_Parity(int fd, int databits, int stopbits, int parity)
{
struct termios options;
if(tcgetattr(fd,&options)!=0)
{
perror("SetupSerial 1");
return(false);
}
options.c_cflag &= ~CSIZE;
switch (databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr, "Unsupported data size\n");
return (false);
}
switch(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;
case 'S':
case 's':
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr, "Unsupported parity\n");
return (false);
}
switch (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 (parity != 'n')
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 150;
options.c_cc[VMIN] = 0;
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd, TCSANOW, &options) !=0 )
{
perror("SetupSerial 3");
return (false);
}
return (true);
}
int OpenDev(char *Dev)
{
int fd = open(Dev, O_RDWR);
if (-1 == fd)
{
perror("Can not Open Serial Port");
return -1;
}
else
return fd;
}