-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathguimodulo.cpp
executable file
·221 lines (186 loc) · 6.51 KB
/
guimodulo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <time.h>
#include <QTime>
#include "guimodulo.h"
using namespace std;
GUImodulo::GUImodulo(QString port,QWidget *parent) : QDialog(parent)
{
readyToSend = false;
// Se crea y configura la clase serial:
// SE INICIALIZA EL PUERTO SERIAL:
serial = new QSerialPort(this);
// SE CONFIGURA EL MISMO:
serial->setPortName(port);
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->open(QIODevice::ReadWrite);
connect(serial,SIGNAL(readyRead()),this,SLOT(recepcionDatos()));
// Se incia el módulo en modo SMS
serial->write("AT+CMGF=1");
serial->write("\r\n");
serial->write("AT+CMGD=1,49");
// Se inicializan los objetos de la interfaz grafica:
miTitulo = new QLabel("Módulo de comunicación");
miLeyenda1 = new QLabel("Comando AT");
miLeyenda2 = new QLabel("Ultimos mensaje del Modulo");
leyendaNumero = new QLabel("Numero");
leyendaMensaje = new QLabel("Mensaje");
for(int i = 0;i<MAX_LINEAS-1;i++)
misMensajesAnteriores[i]= new QLabel("empty");
mensajeAEnviar = new QLineEdit;
mensajeRecibido = new QLineEdit;
ingresoNumero = new QLineEdit;
ingresoMensaje = new QLineEdit;
enviar = new QPushButton("Enviar comando AT");
enviarUSSD = new QPushButton("Enviar USSD");
llamar = new QPushButton("Llamar");
colgar = new QPushButton("Colgar");
enviarMensajeBoton = new QPushButton("Enviar Mensaje");
// Se establecen los parámetros por defecto:
enviar->setDefault(true);
// Se conectan las señales
connect(enviar,SIGNAL(clicked()),this,SLOT(enviarComandoATPresionado()));
connect(enviarUSSD,SIGNAL(clicked()),this,SLOT(enviarUSSDPresionado()));
connect(llamar,SIGNAL(clicked()),this,SLOT(realizarLlamada()));
connect(colgar,SIGNAL(clicked()),this,SLOT(colgarLlamada()));
connect(enviarMensajeBoton,SIGNAL(clicked()),this,SLOT(enviarMensajeTexto()));
// Se crean las capas
QHBoxLayout * capaTitulo = new QHBoxLayout;
capaTitulo->addWidget(miTitulo);
QHBoxLayout * capaEnvio = new QHBoxLayout;
capaEnvio->addWidget(miLeyenda1);
capaEnvio->addWidget(mensajeAEnviar);
capaEnvio->addWidget(enviar);
QHBoxLayout * capaRecepcion = new QHBoxLayout;
capaRecepcion->addWidget(miLeyenda2);
capaRecepcion->addWidget(mensajeRecibido);
QHBoxLayout * capaLlamada = new QHBoxLayout;
capaLlamada->addWidget(leyendaNumero);
capaLlamada->addWidget(ingresoNumero);
capaLlamada->addWidget(llamar);
capaLlamada->addWidget(colgar);
capaLlamada->addWidget(enviarUSSD);
QHBoxLayout * capaMensaje = new QHBoxLayout;
capaMensaje->addWidget(leyendaMensaje);
capaMensaje->addWidget(ingresoMensaje);
capaMensaje->addWidget(enviarMensajeBoton);
QVBoxLayout * totalCapas = new QVBoxLayout;
totalCapas->addLayout(capaTitulo);
totalCapas->addLayout(capaEnvio);
totalCapas->addLayout(capaLlamada);
totalCapas->addLayout(capaMensaje);
totalCapas->addLayout(capaRecepcion);
for(int i = 0;i<MAX_LINEAS-1;i++)
totalCapas->addWidget(misMensajesAnteriores[i]);
this->setLayout(totalCapas);
setWindowTitle("Modem Comunicacion");
}
GUImodulo::~GUImodulo()
{
serial->close();
}
void GUImodulo::recepcionDatos()
{
// Este SLOT actualiza todos los campos y emite la señal recibiMensaje para hacer conocer a la central que debe analizar los mismos
ultimoMensaje = serial->readAll();
mensajeRecibido->setText(ultimoMensaje);
/*if(ultimoMensaje.length()>20){
emit recibiMensaje();
}*/
for(int i = MAX_LINEAS-1; i>0;i--)
{
ultimasLineas[i] = ultimasLineas[i-1];
misMensajesAnteriores[i-1]->setText(ultimasLineas[i]);
}
ultimasLineas[0] = ultimoMensaje;
if(readyToSend)
{// Si estamos a la respuesta de un mensaje enviamos el mismo
enviarMensajeYSUB(mensajeActual);
readyToSend = false;
this->setWindowTitle(ultimoMensaje);
}
emit recibiMensaje();
}
void GUImodulo::enviarComandoATPresionado()
{
QByteArray comandoAT = mensajeAEnviar->text().toUtf8();
serial->write(comandoAT);
serial->write("\r\n");
}
void GUImodulo::enviarUSSDPresionado()
{
// Enviando en formato: AT+CUSD=1,"*105#", para mandar opciones solo poner el número de la opcion como ser: AT+CUSD=1,"1"
QByteArray codigoUSSD = ingresoNumero->text().toUtf8();
serial->write("AT+CUSD=1,");
serial->write("\x22");
serial->write(codigoUSSD);
serial->write("\x22");
serial->write("\r\n");
}
void GUImodulo::realizarLlamada()
{
QByteArray numero = ingresoNumero->text().toUtf8();
serial->write("ATD+591");
serial->write(numero);
serial->write(";\r\n");
}
void GUImodulo::colgarLlamada()
{
serial->write("ATH");
serial->write("\r\n");
}
void GUImodulo::enviarMensajeTexto()
{
QByteArray numero = ingresoNumero->text().toUtf8();
QByteArray mensaje = ingresoMensaje->text().toUtf8();
enviarMensaje(numero,mensaje);
/*readyToSend = true;
serial->write("AT+CMGS=");
serial->write("\x22");
serial->write(numero);
serial->write("\x22");
serial->write("\r\n");*/
}
QByteArray GUImodulo::getUltimoMensaje()
{
return ultimoMensaje;
}
void GUImodulo::enviarMensaje(QByteArray number, QByteArray message)
{
readyToSend = true; // Cuidar este y el que esta en el slot
serial->write("AT+CMGS=");
serial->write("\x22");
serial->write(number);
serial->write("\x22");
serial->write("\r\n");
mensajeActual = message; // ~MensajeActual
/*QTime miTiempo = QTime::currentTime();
while((QTime::currentTime().msec() - miTiempo.msec())<2000)
{
true;
}*/
//enviarMensajeYSUB(message);
}
void GUImodulo::leerMensaje(QByteArray messageNumber)
{
serial->write("AT+CMGR=");
serial->write(messageNumber);
serial->write("\r\n");
}
void GUImodulo::escribir(QByteArray mensaje)
{
serial->write(mensaje);
}
void GUImodulo::enviarMensajeYSUB(QByteArray message)
{
serial->write(message);
serial->write("\x1A");
serial->write("AT\r\n");
}