#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#define SERIAL_PORT "/dev/ttyS0" // Cambia esto según tu sistema
#define BUFFER_SIZE 256
int openSerialPort(const char *port) {
int fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("No se puede abrir el puerto serie");
return -1;
}
return fd;
}
void configureSerialPort(int fd) {
struct termios options;
tcgetattr(fd, &options);
options.c_cflag = B9600 | CS8 | CLOCAL | CREAD; // Configura la velocidad y el formato
options.c_iflag = IGNPAR; // Ignora caracteres de paridad
options.c_oflag = 0; // Sin control de flujo
options.c_lflag = ICANON; // Modo canónico
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);
}
void sendATCommand(int fd, const char *command) {
write(fd, command, strlen(command));
write(fd, "\r", 1); // Enviar retorno de carro
}
void readResponse(int fd) {
char buffer[BUFFER_SIZE];
int n = read(fd, buffer, sizeof(buffer) - 1);
if (n > 0) {
buffer[n] = '\0'; // Null-terminate the string
printf("Respuesta del módem: %s\n", buffer);
}
}
int main() {
int fd = openSerialPort(SERIAL_PORT);
if (fd == -1) return 1;
configureSerialPort(fd);
// Enviar comandos AT para configurar el módem
sendATCommand(fd, "AT"); // Verificar conexión
readResponse(fd);
sendATCommand(fd, "ATA"); // Contestar la llamada
readResponse(fd);
// Esperar y leer los dígitos marcados
printf("Esperando dígitos marcados...\n");
while (1) {
readResponse(fd);
// Aquí puedes agregar lógica para procesar los dígitos recibidos
}
close(fd);
return 0;
}