Este es uno de esos posts que podría tener 40 títulos diferentes. De hecho, solo quiero pasaros un par de trozos de código uno en C y el otro en perl, que lo que hacen es abrir el puerto serie virtual que crea el módem UMTS/GPRS PCMCIA de Vodafone y después le envía el comando AT correspondiente para introducir el PIN del SIM que lleva la tarjeta y listos.
Antes de pasar a los temas de códigos un pequeño resumen de comandos AT útiles para temas de PIN:
AT+CPIN=1234 -> Introduim el PIN a la SIM del mòdem.
AT+CPIN? -> preguntem al mòdem si ja hem introduit la SIM.
AT+CPWD="SC",old_pin,new_pin -> canviem el PIN de la SIM.
De hecho, si no queréis hacer un código tan bonito como el que yo uso, también podéis hacer una cosa tan sencilla como esta:
# echo AT+CPIN=1234 > /dev/tts/USB0
Siendo 1234 el código PIN y /dev/tts/USB0 el dispositivo serie que crea el módem PCMCIA de vodafone.
Yendo a códigos un poco más interesantes aquí tenéis un trocito de código C para hacer lo mismo que hemos hecho en el comando anterior:
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
main() {
int fd;
fd = open_port();
init_modem(fd);
close(fd);
}
int open_port(void) {
int fd; /* File descriptor for the port */
struct termios options;
fd = open("/dev/tts/USB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
/*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/tts/USB0 - ");
}
else fcntl(fd, F_SETFL, 0);
/* get the current options */
tcgetattr(fd, &options);
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);
/* set raw input, 1 second timeout */
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~CNEW_RTSCTS;
/* set the options */
tcsetattr(fd, TCSANOW, &options);
return (fd);
}
int /* O - 0 = MODEM ok, -1 = MODEM bad */
init_modem(int fd) /* I - Serial port file */
{
char buffer[255]; /* Input buffer */
char *bufptr; /* Current char in buffer */
int nbytes; /* Number of bytes read */
int tries; /* Number of tries so far */
for (tries = 0; tries < 3; tries ++) {
/* send an AT command followed by a CR */
if (write(fd, "AT+CPIN=1234\r", 13) < 13) continue;
/* read characters into our string buffer until we get a CR or NL */
bufptr = buffer;
while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0) {
bufptr += nbytes;
if (bufptr[-1] == '\n' || bufptr[-1] == '\r') break;
}
/* nul terminate the string and see if we got an OK response */
*bufptr = '\0';
if (strncmp(buffer, "OK", 2) == 0) return (0);
}
return (-1);
}
Como en el ejemplo anterior el dispositivo serie es /dev/tts/USB0 y el código PIN 1234. Este código es un copy/paste de la parte de un serial howto que ya no recuerdo ni de dónde lo saqué. Solo me he quedado con el código que me hacía falta y le he añadido algunas opciones que había en el howto pero no en los ejemplos de código.
A continuación cuelgo un código equivalente hecho en perl. Como podéis ver este código en perl tiene una dependencia de la librería CPAN: Device::Gsm. Con esta librería también es muy sencillo enviar SMS y descubrir algunas informaciones del teléfono, como el fabricante o el imei.
use Device::Gsm;
my $gsm = new Device::Gsm( port => '/dev/tts/USB0', pin => '4466' );
if( $gsm->connect() ) {
print "connected!\n";
} else {
print "sorry, no connection with gsm phone on serial port!\n";
}
# Register to GSM network (you must supply PIN number in above new() call)
print $gsm->register();