- 积分
- 3
贡献25
飞刀2 FD
注册时间2019-1-9
在线时间0 小时
扫一扫,手机访问本帖
|
源码:
gprs.cpp
#include <QtGui/QApplication>
#include <QtCore/QTextCodec>
#include "gprs_main.h"
int main(int argc,char* argv[])
{
QApplication app(argc,argv);
GprsWindow gprsWin;
QFont font = app.font();
font.setPointSize(7);
app.setFont(font);
gprsWin.show();
return app.exec();
}
message.cpp
#include <QtGui>
#include <QtCore>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include "message.h"
#include <termio.h>
int gprs_fd;
int state;
int calling_state;
MesDialog::MesDialog(QWidget* parent):QDialog(parent)
{
setupUi(this);
setWindowTitle(tr("Message and calling"));
connect(num1,SIGNAL(clicked()),this,SLOT(numbtnclicked()));
connect(num2,SIGNAL(clicked()),this,SLOT(numbtnclicked()));
connect(num3,SIGNAL(clicked()),this,SLOT(numbtnclicked()));
connect(num4,SIGNAL(clicked()),this,SLOT(numbtnclicked()));
connect(num5,SIGNAL(clicked()),this,SLOT(numbtnclicked()));
connect(num6,SIGNAL(clicked()),this,SLOT(numbtnclicked()));
connect(num7,SIGNAL(clicked()),this,SLOT(numbtnclicked()));
connect(num8,SIGNAL(clicked()),this,SLOT(numbtnclicked()));
connect(num9,SIGNAL(clicked()),this,SLOT(numbtnclicked()));
connect(num0,SIGNAL(clicked()),this,SLOT(numbtnclicked()));
connect(a,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(b,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(c,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(d,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(f,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(g,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(h,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(i,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(j,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(k,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(l,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(m,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(n,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(o,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(p,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(q,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(r,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(s,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(t,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(u,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(v,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(w,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(xx,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(yy,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(z,SIGNAL(clicked()),this,SLOT(charbtnclicked()));
connect(call,SIGNAL(clicked()),this,SLOT(callbtn()));
connect(send,SIGNAL(clicked()),this,SLOT(sendbtn()));
connect(send_2,SIGNAL(clicked()),this,SLOT(cancel_message()));
connect(send_3,SIGNAL(clicked()),this,SLOT(break_call()));
connect(d_num,SIGNAL(clicked()),this,SLOT(delete_num()));
connect(d_chr,SIGNAL(clicked()),this,SLOT(delete_char()));
connect(open_tty,SIGNAL(clicked()),this,SLOT(open_uart()));
connect(listenButton,SIGNAL(clicked()),this,SLOT(listen_call()));
connect(receive_message,SIGNAL(clicked()),this,SLOT(receive_message1()));
send_3->setEnabled(false);//将挂断按键设置为可用
listenButton->setEnabled(false);//将接电话的按键使能
send_3->setEnabled(false); //将挂断电话的按键使能
state=0;
calling_state=0;
}
MesDialog::~MesDialog()//析构函数
{
::system("killall pppd");
if (gprs_fd >= 0) {
::close(gprs_fd);
gprs_fd = -1;
}
}
void MesDialog::numbtnclicked() //那好几个按键
{
QPushButton *btn=(QPushButton*)sender();//返回发送信号的对象的指针
QString numValue=btn->text();
lineEdit->setText(lineEdit->text()+numValue);
}
void MesDialog::charbtnclicked()//那好几个按键
{
QPushButton *btn=(QPushButton*)sender();//返回发送信号的对象的指针
QString charValue=btn->text();
textEdit->moveCursor(QTextCursor::End);//在文本框中添加字符
textEdit->insertPlainText(charValue);
}
void MesDialog::callbtn()
{
state=CALL_OK;
QString telnum;
telnum=lineEdit->text();//
QString send="ATD"+telnum+";\r";
if (telnum.isEmpty())
{
QMessageBox::information(this,"gprs","plesse input the address",0);//这是提示消息框!!要会用
return ;
}
::write(gprs_fd,"ATE1\r",strlen("ATE1\r"));
sleep(1);
::write(gprs_fd,"AT+COLP=1\r",strlen("AT+COLP=1\r"));
sleep(1);
if(::write(gprs_fd,send.toLatin1(),send.length()))//可能是包含中文字符的意思 这样转换一下来可以,不然上边的不会正常发送的!!!!!!!!!!!!!!!!!!!!!!!!!!!
{
send_3->setEnabled(true);//将挂断按键设置为可用
}
}
void MesDialog::sendbtn()
{
QString mess=textEdit->toPlainText()+"\x1a";//最后的发送指令
QString num;
num=lineEdit->text();//
char getch[15];
QString tell="AT+CMGS=\""+num+"\"\r";
::write(gprs_fd,"AT+CMGF=1\r",strlen("AT+CMGF=1\r"));
sleep(1);
::write(gprs_fd,"AT+CSCS=\"GSM\"\r",strlen("AT+CSCS=\"GSM\"\r"));
sleep(1);
if(::write(gprs_fd,tell.toLatin1(),tell.length()))
{
sleep(1);
//QMessageBox::information(this,"gprs",mess,0);
}
if(::write(gprs_fd,mess.toLatin1(),mess.length()))
{
sleep(1);
state=SEND_OK;
}
}
void MesDialog::cancel_message()
{
QMessageBox::information(this,"gprs","cancel_message",0);
}
void MesDialog::break_call() //挂断电话
{
::write(gprs_fd,"ATH\r",strlen("ATH\r"));
send_3->setEnabled(false); //将挂断电话的按键使能
sleep(1);
calling_state=0;
}
void MesDialog::delete_num()
{
QString telnum;
telnum=lineEdit->text();//获取文本框中的号码
QString t = telnum.left(telnum.length()-1);//长度减去1 就是回删键
lineEdit->setText(t);
}
void MesDialog::delete_char()
{
textEdit->clear();//这个可以
}
void MesDialog::listen_call()
{
::write(gprs_fd,"ATA\r",strlen("ATA\r"));
listenButton->setEnabled(false);//将接电话的按键使能
sleep(1);
calling_state=0;
}
void MesDialog::receive_message1()
{
}
void MesDialog::open_uart()
{
gprs_fd = openSerialPort();
if (gprs_fd < 0) {
QMessageBox::warning(this, tr("Error"), tr("Fail to open serial port!"));
return ;
}
::write(gprs_fd,"AT+CNMI=2,1\r",strlen("AT+CNMI=2,1\r"));
sleep(1);
state=3;
m_notifier = new QSocketNotifier(gprs_fd, QSocketNotifier::Read, this); //监听系统的文件操作
connect (m_notifier, SIGNAL(activated(int)), this, SLOT(remoteDataIncoming())); //即串口有数据回读回来后就会调用这个
}
int MesDialog::openSerialPort()
{
int fd = -1;
fd = ::open("/dev/ttySAC1", O_RDWR|O_NONBLOCK);//驱动设备打开函数
if (fd < 0) {
QMessageBox::warning(this, tr("Error"), tr("cannot open the driver of uart1"));
return -1;
}
termios serialAttr;//qt中关于串口的函数标准的c函数 这是一个结构体!
memset(&serialAttr, 0, sizeof serialAttr);//在一段没纯种添加某一值,通常用作清零 将首地址为serialAttr大小为serialAttr的设为0
cfsetispeed(&serialAttr,B9600);
cfsetospeed(&serialAttr,B115200);//115200
serialAttr.c_cflag &= ~CSIZE;//8位
serialAttr.c_cflag |= CS8;
serialAttr.c_iflag = IGNPAR;//没有奇偶校验
if (tcsetattr(fd, TCSANOW, &serialAttr) != 0) //把我们的设置写进结构体
{
QMessageBox::warning(this, tr("Error"), tr("cannot write into qt_struct"));
return -1;
}
return fd;
}
//获取回显信息
void MesDialog::remoteDataIncoming()
{
char getch[30];
if(::read(gprs_fd,getch,30))//就是这一个
{
//QMessageBox::information(this,"gprs","uart feedback information!",0);
QString mes_or_tell = QString(getch); //数据的首地址 数组
QString r_message=QString(getch);
int m_ok=r_message.indexOf(QRegExp("OK"),0);
int m_err=r_message.indexOf(QRegExp("ERROR"),0);
int tell_iscome=mes_or_tell.indexOf(QRegExp("RING"), 0);//若是找到这个字符串
int mes_iscome=mes_or_tell.indexOf(QRegExp("CMTI"), 0);//找到这个字符串 来信息的字符串
int send_message=mes_or_tell.indexOf(QRegExp("CMGS"),0);
if(send_message>=0)
{state=0;
QMessageBox::information(this,"gprs","message has been send",0);
}
if(m_ok>=0)
{
if(state==3)
{state=0;
QMessageBox::information(this,"gprs","receive setting is ok",0);
}
}
if(m_err>=0)
{
if(state==3)
{
state=0;
QMessageBox::information(this,"gprs","receive setting is error",0);
}
if(state==SEND_OK)
{
state=0;
QMessageBox::information(this,"gprs","error when send message",0);
}
}
if(mes_iscome>=0)
{
QMessageBox::information(this,"gprs"," new message",0);
}
if(tell_iscome>=0)
{
if(calling_state==0)
{
QMessageBox::StandardButton rb=QMessageBox::information(this,"gprs","tell is coming",QMessageBox::Ok);
if(rb==QMessageBox::Ok)
calling_state=1;
else
calling_state=0;
}
listenButton->setEnabled(true);//将接电话的按键使能
send_3->setEnabled(true); //将挂断电话的按键使能
}
}
}
message.h
#ifndef MESSAGE_H_
#define MESSAGE_H_
#include <QtGui>
#include "message_ui.h"
#define CALL_OK 1
#define SEND_OK 2
#define HAVE_SEND 8
class MesDialog:public QDialog,public Ui_message_dialog
{
Q_OBJECT
private:
int openSerialPort();
public:
MesDialog(QWidget* =0);
virtual ~MesDialog();
public slots:
void numbtnclicked();
void charbtnclicked();
void sendbtn();
void callbtn();
void break_call();
void cancel_message();
void delete_num();
void delete_char();
void open_uart();
void remoteDataIncoming();
void listen_call();
void receive_message1();
private:
QSocketNotifier *m_notifier;
};
#endif
|
|