Foros del Web » Administración de Sistemas » Unix / Linux »

Ayuda (comunicacion por el serial)

Estas en el tema de Ayuda (comunicacion por el serial) en el foro de Unix / Linux en Foros del Web. Se trata de lo siguiente, el programa debe estar mandando y recibiendo info del serial, en caso de q la linea se corte debe tratar ...
  #1 (permalink)  
Antiguo 27/01/2003, 12:43
Avatar de marcos25  
Fecha de Ingreso: noviembre-2002
Ubicación: España
Mensajes: 164
Antigüedad: 21 años, 5 meses
Puntos: 0
Ayuda (comunicacion por el serial)

Se trata de lo siguiente, el programa debe estar mandando y recibiendo info del serial, en caso de q la linea se corte debe tratar de comunicarse (estar ciclado esperando respuesta del modem), en caso de q se restablezca la comunicacion volverá a enviar y recibir info.

Aqui les anexo el código para q le hechen una mirada:

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>

#define BAUDRATE B9600
#define MODEMDEVICE "/dev/ttyS1"
#define _POSIX_SOURCE 1 /* fuentes cumple POSIX */
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;
char recibido[1024];

void signal_handler_IO (int status); /* definicion del manejador de segnal */
int wait_flag=TRUE; /* TRUE mientras no segnal recibida */

main()
{
int fd,c, res,jj;
struct termios oldtio,newtio;
struct sigaction saio; /* definicion de accion de segnal */
fd_set rfds;
struct timeval tv;
char buf[255];

/* abre el dispositivo en modo no bloqueo (read volvera inmediatamente) */


//escribe()

fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd <0) { perror(MODEMDEVICE); exit(-1); }
//write(fd,"ATa\r",4);

/* instala el manejador de segnal antes de hacer asincrono el dispositivo */

saio.sa_handler = signal_handler_IO;
// saio.sa_mask[0] = 0;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);

/* permite al proceso recibir SIGIO */

fcntl(fd, F_SETOWN, getpid());

/* Hace el descriptor de archivo asincrono (la pagina del manual dice solo
O_APPEND y O_NONBLOCK, funcionara con F_SETFL...) */

fcntl(fd, F_SETFL, FASYNC);
tcgetattr(fd,&oldtio); /* salvamos conf. actual del puerto */

/*
fija la nueva configuracion del puerto para procesos de entrada canonica
*/

//newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CREAD;
//newtio.c_iflag = ICRNL;
newtio.c_oflag = 0;
//newtio.c_lflag = ICANON;
//newtio.c_lflag &= (ICANON);
newtio.c_cc[VMIN]=1;//1
newtio.c_cc[VTIME]=4; // 0//control de cano
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);





// write (fd,"ATa\r",4);
//sleep(10);
//printf("\nFin\n");
write(fd,"AT s7=45 s0=3 l1 v1 x4 &c1 e0 q0\r",33);
//sleep(12);

/* bucle de espera para entrada. Normalmente se haria algo util aqui */


//recibido=malloc(500*sizeof(char));


/*FD_ZERO(&rfds);
FD_SET(fd, &rfds);*/
tv.tv_sec=60;
tv.tv_usec=0;



//while (select(fd+1,&rfds,NULL,NULL,&tv) > 0) {
while (select(0,0,NULL,NULL,&tv) == -1)
{
printf("estoy aca");
while (STOP==FALSE)
{
recibido[0]='\0';
if (wait_flag==FALSE)
{
res = read(fd,buf,1024);
buf[res]='\0';
if (buf[res-1]==10)
{

tv.tv_sec=0;;
STOP=TRUE;
}
strcat(recibido,buf);
/* para el bucle si solo entra un CR */
wait_flag = TRUE; /* espera una nueva entrada */
}

if (recibido[0]!='\0')
{
printf("%s",recibido);
buf[0]='\0';
}

//if (buf[res]==10) STOP=TRUE;
}
/*if (recibido[0]!='\0')
printf("%s",recibido);*/


STOP=FALSE;

}
printf("fin\n");
tcsetattr(fd,TCSANOW,&oldtio);

}


/************************************************** *************************
* manipulacion de segnales. pone wait_flag a FALSE, para indicar al bucle *
* anterior que los caracteres han sido recibidos *
************************************************** *************************/

void signal_handler_IO (int status)
{
//printf("\nrecibida segnal SIGIO.\n");
wait_flag = FALSE;
}

Como verán esta en un ciclo infinito mandando y recibiendo datos del serial.

Los problemas q se me presentan son:
- Entra una vez, me responde OK y luego me devuelve -1.

- Si apago el modem, trata de comunicarse, pero en cuanto lo enciendo deberia mandar la cadena y responder OK nuevamente, pero no lo hace.

Espero q alguien me heche una mano con esto.

Muchas gracias.

_Marcos_
  #2 (permalink)  
Antiguo 27/01/2003, 12:46
Avatar de marcos25  
Fecha de Ingreso: noviembre-2002
Ubicación: España
Mensajes: 164
Antigüedad: 21 años, 5 meses
Puntos: 0
correccion

Lo siento pero copie otra version del código, aqui les va el bueno.

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>

#define BAUDRATE B9600
#define MODEMDEVICE "/dev/ttyS1"
#define _POSIX_SOURCE 1 /* fuentes cumple POSIX */
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;
char recibido[1024];

void signal_handler_IO (int status); /* definicion del manejador de segnal */
int wait_flag=TRUE; /* TRUE mientras no segnal recibida */

main()
{
int fd,c, res,jj, x=0, cont=0;
struct termios oldtio,newtio;
struct sigaction saio; /* definicion de accion de segnal */
fd_set rfds;
struct timeval tv;
char buf[255];

fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd <0) { perror(MODEMDEVICE); exit(-1); }

/* instala el manejador de segnal antes de hacer asincrono el dispositivo */
saio.sa_handler = signal_handler_IO;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);


/* permite al proceso recibir SIGIO */
fcntl(fd, F_SETOWN, getpid());

/* Hace el descriptor de archivo asincrono (la pagina del manual dice solo
O_APPEND y O_NONBLOCK, funcionara con F_SETFL...) */
fcntl(fd, F_SETFL, FASYNC);
tcgetattr(fd,&oldtio); /* salvamos conf. actual del puerto */

newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CREAD;
newtio.c_oflag = 0;
newtio.c_cc[VMIN]=1;//1
newtio.c_cc[VTIME]=4; // 0//control de cano
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);

FD_ZERO(&rfds);
FD_SET(fd, &rfds);

while (1==1)
{//1
printf("Loading %d \n", cont);
cont++;
write(fd,"AT s7=45 s0=3 l1 v1 x4 &c1 e0 q0\r",33);//mando cadena de inicializacion
tv.tv_sec=5;
tv.tv_usec=0;

while ((x=select(fd+1,&rfds,NULL,NULL,&tv)) > 0)
{//2
printf("estoy aca");
while (STOP==FALSE)
{//3
recibido[0]='\0';
if (wait_flag==FALSE)
{//4
res = read(fd,buf,1024);
buf[res]='\0';
printf("----------------------- -%s-",buf); //leyó correctamente

if (buf[res-1]==10)
{//5
tv.tv_sec=0;;
STOP=TRUE;
}//5
strcat(recibido,buf);
/* para el bucle si solo entra un CR */
wait_flag = TRUE; /* espera una nueva entrada */
}//4
if (recibido[0]!='\0')
{
buf[0]='\0';
}
}//3

STOP=FALSE;
tcsetattr(fd,TCSANOW,&oldtio);
}//2
printf("fin %d \n", x);

if (x==0)
printf("ES CERO \n");

tcsetattr(fd,TCSANOW,&oldtio);
}//1
}//0


/************************************************** *************************
* manipulacion de segnales. pone wait_flag a FALSE, para indicar al bucle *
* anterior que los caracteres han sido recibidos *
************************************************** *************************/
void signal_handler_IO (int status)
{
wait_flag = FALSE;
}
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:03.