#include "gprs.h"
#include "ui_gprs.h"
#include <QMessageBox>
#include <QStackedWidget>
#include <fcntl.h>
#include <unistd.h>
#include "ui_telnum_input.h"
Gprs::Gprs(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Gprs)
{
ui->setupUi(this);
ui->stackedWidget->setCurrentIndex(0);
telnum = new Telnum_input(this, this);
}
Gprs::~Gprs()
{
delete ui;
}
/***************PhoneCall实现**********/
int Gprs::PhoneCall(int fd,char *phonenum)
{
char *cmd="ATD";
char *cmd_end = ";\xd\0";
int i;
char buf[200];
i=5;
memset(buf,0,200);
while( strstr(buf,"OK")==NULL || i<0)
{
::write(fd,cmd,strlen(cmd));
::write(fd,phonenum,strlen(phonenum));
::write(fd,cmd_end,strlen(cmd_end));
tcdrain(fd);//等待发送完成
tcflush(fd,TCIOFLUSH);//清空收发缓冲区
sleep(1);
::read(fd,buf,200);
i--;
}
if(i<0)
return 0;
if(i>0)
return 1;
}
/********************PhoneCut实现*****************************/
void Gprs::PhoneCut(int fd)
{
char *cmd="ATH";
char *cmd_end = "\xd\0";
int i = 3;
while(i){
::write(fd,cmd,strlen(cmd));
::write(fd,cmd_end,strlen(cmd_end));
sleep(1);
i--;
}
::close(fd);
}
/*******************呼叫保安槽函数**************************************/
void Gprs::on_call_sec_clicked()
{
char *sec_phonenum="15915867727";
int result;
ui->stackedWidget->setCurrentIndex(1);//正在连接label
fdgprs1 = ::open( "/dev/s3c2410_serial1", O_RDWR|O_NOCTTY | O_NDELAY);
if (-1 == fdgprs1){
ui->stackedWidget->setCurrentIndex(3);
}
if(fdgprs1> 0){
// QMessageBox::warning(this,tr("open success"),tr("try again"),QMessageBox::Yes);
struct termios termios_new;
bzero( &termios_new, sizeof(termios_new));
cfmakeraw(&termios_new);
termios_new.c_cflag=(B9600);
termios_new.c_cflag |= CLOCAL | CREAD;
termios_new.c_cflag &= ~CSIZE;
termios_new.c_cflag |= CS8;
termios_new.c_cflag &= ~PARENB;
termios_new.c_cflag &= ~CSTOPB;
tcflush(fdgprs1,TCIFLUSH);
termios_new.c_cc[VTIME] = 0;
termios_new.c_cc[VMIN] = 0;
tcflush (fdgprs1, TCIFLUSH);
tcsetattr(fdgprs1,TCSANOW,&termios_new);
result=PhoneCall(fdgprs1,sec_phonenum);
if(result==1){
ui->stackedWidget->setCurrentIndex(2);
}else{
ui->stackedWidget->setCurrentIndex(3);
}
}
thread = new Mythread(fdgprs1,1,this);
thread->start();
}
/****************************呼叫管理员槽函数********************************************/
void Gprs::on_call_man_clicked()
{
char *man_phonenum="15915867727";
int result;
ui->stackedWidget->setCurrentIndex(1);//正在连接label
fdgprs1 = ::open( "/dev/s3c2410_serial1", O_RDWR|O_NOCTTY | O_NDELAY);
if (-1 == fdgprs1){
ui->stackedWidget->setCurrentIndex(3);
//QMessageBox::warning(this,tr("warning"),tr("try again"),QMessageBox::Yes);
}
if(fdgprs1> 0){
//QMessageBox::warning(this,tr("open success"),tr("try again"),QMessageBox::Yes);
struct termios termios_new;
bzero( &termios_new, sizeof(termios_new));
cfmakeraw(&termios_new);
termios_new.c_cflag=(B9600);
termios_new.c_cflag |= CLOCAL | CREAD;
termios_new.c_cflag &= ~CSIZE;
termios_new.c_cflag |= CS8;
termios_new.c_cflag &= ~PARENB;
termios_new.c_cflag &= ~CSTOPB;
tcflush(fdgprs1,TCIFLUSH);
termios_new.c_cc[VTIME] = 0;
termios_new.c_cc[VMIN] = 0;
tcflush (fdgprs1, TCIFLUSH);
tcsetattr(fdgprs1,TCSANOW,&termios_new);
result=PhoneCall(fdgprs1,man_phonenum);
if(result==1){
ui->stackedWidget->setCurrentIndex(2);
}else{
ui->stackedWidget->setCurrentIndex(3);
}
}
thread = new Mythread(fdgprs1,1,this);
thread->start();
}
void Gprs::on_back_main_clicked()//welcom返回主界面槽函数
{
parentWidget()->show();
close();
}
void Gprs::on_phone_cut_clicked()//连接中挂断电话槽函数
{
ui->stackedWidget->setCurrentIndex(0);
thread->flag= 0;
thread->exit();
do
{
}
while(thread->isRunning());
delete thread;
PhoneCut(fdgprs1);
}
void Gprs::on_back_main5_clicked()//被挂断电话界面 返回主界面槽函数
{
ui->stackedWidget->setCurrentIndex(0);
}
void Gprs::on_connected_cut_clicked()////成功连接后自行挂断槽函数
{
ui->stackedWidget->setCurrentIndex(0);
thread->flag= 0;
thread->exit();
do
{
}
while(thread->isRunning());
delete thread;
PhoneCut(fdgprs1);
}
void Gprs::on_back_main4_clicked()//连接失败后返回槽函数
{
ui->stackedWidget->setCurrentIndex(0);
}
void Gprs::on_call_other_clicked()//呼叫其他用户槽函数
{
//telnum = new Telnum_input(this);
telnum->show();
telnum->ui->stackedWidget->setCurrentIndex(0);
hide();
}
void Gprs::signal1_del()//自己挂断信号处理槽函数
{
ui->stackedWidget->setCurrentIndex(0);
}
void Gprs::signal2_del()//保安挂断信号处理槽函数
{
ui->stackedWidget->setCurrentIndex(4);
}