oriolrius.cat

Des del 2000 compartiendo sobre…

Tag: serial

socat tip: create virtual serial port and link it to TCP

Reading time: < 1 minute Create a virtual serial port and publish it on TCP port:

socat pty,link=/dev/virtualcom0,rawer tcp-listen:2101

In another computer, for instance, another virtual port can be created and connected to the previous one:

socat pty,link=/dev/virtualcom0,rawer tcp:SERVER_IP:2101

If in any of those both sides we want to open a real serial port, for instance, in the server case we can run:

socat /dev/ttyS0,rawer tcp-listen:2101

More information on socat manpage.

Mikrotik RouterBoard 150 – configuración consola serie

Reading time: < 1 minute

Resulta que el RouterBoard 150 de Mikrotik no té la mateixa configuració que la resta. Resulta que a aquest li hem de desactivar el control de fluxe per hardware i software:

serial-port-minirouter.png

Baud: 115200, Bits: 8, Parity: None, Stop: 1, Flux Control: none.

Configuració consola serie del mikrotik/routerboard 532A

Reading time: < 1 minute

M’he fet un fart de buscar aquesta tonteria per poder accedir des de la consola serie amb el minicom així doncs, aquí va la configuració:

mikrotik-serial-configuration.png

Baud: 115200, Bits: 8, Parity: None, Stop: 1, Flux Control: hardware.

IBSN – Internet Blog Serial Number

Reading time: 1 – 2 minutes

Una tonteria com un altre? no ho sé, la qüestió és que gràcies a la web de microsiervos, he descovert aquest concepte, el IBSN.

Diferencias entre el ISSN, el ISBN y el IBSN

El ISSN (International Standard Serial Number / Número Internacional Normalizado de Publicaciones Seriadas) y el ISBN (International Standard Book Number / Número Internacional Normalizado de Libros) son códigos numéricos de identificación. El ISSN, un número de ocho cifras, es el código internacional de identificación de las publicaciones seriadas (revistas, periódicos, boletines, anuarios, series de monografías… y el ISBN, un número de diez cifras, identifica los libros.

El IBSN (Internet Blog Serial Number / Número de Serie de Blogs de Internet) consta también de diez cifras, e identifica los blogs (weblogs o cuadernos de bitácora) de Internet.

Així doncs, m’he afegit a la iniciativa i m’he creat el meu propi codi de barres IBSN. Concretament m’he auto-assignat el codi: 0-000-0000-72 el codi de barres me l’he fet a la web Barcode Label Printer.

codi-de-barres.png

Introduïnt el PIN a la VRA de forma automàtica

Reading time: 3 – 5 minutes

Aquest és un d’aquells posts que podria tenir 40 títols diferents. De fet, només vull passar-vos un parell de trossos de codi un en C i l’altre en perl, que el que fan és obrir el port serie virtual que crea el modem UMTS/GPRS PCMCIA de Vodafone després l’hi envia la comanda AT corresponent per introduir el PIN del SIM que porta la targeta i llestos.

Abans de passar als temes de codis un petit resum d’ordres AT útils per temes 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 fet, si no voleu fer un codi tan bonic com el que jo uso, també podeu fer una cosa tan senzilla com aquesta:

# echo AT+CPIN=1234 > /dev/tts/USB0

Sent 1234 el codi PIN i /dev/tts/USB0 el dispositiu serie que crea el modem PCMCIA de vodafone.

Anant a codis una miqueta més interessants aquí teniu un trosset de codi C per fer el mateix que em fet en l’ordre 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);
}

Com en l’exemple anterior el dispositiu serie és /dev/tts/USB0 i el codi PIN 1234. Aquest codi és un copy/paste de la part d’un serial howto que ja no recordo ni d’on el vaig treure. Només m’he quedat amb el codi que em calia i hi he afegit algunes otpions que hi havia al howto però no als exemples de codi.

A continuació penjo un codi equivalent fet en perl. Com podeu veure aquest codi en perl té una dependència de la llibreria CPAN: Device::Gsm. Amb aquest llibreria també és molt senzill enviar SMS i descobrir algunes informacions del telèfon, com el fabricant o l’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();