+mArkO+
Guest
Sat Sep 01, 2007 6:39 pm
necesito orientacion por parte de alguien que haya programado transmision
por puerto serie en linux
en lo posible algun codigo de ejemplo o sugerencias
gracias
Miguel Gimenez
Guest
Sat Sep 01, 2007 7:10 pm
+mArkO+ escribió:
Quote:
necesito orientacion por parte de alguien que haya programado transmision
por puerto serie en linux
en lo posible algun codigo de ejemplo o sugerencias
gracias
Yo he hecho alguna cosa, pero hasta el lunes no tengo acceso al fuente.
Si para entonces no te han contestado pondré lo que tengo.
--
Saludos
Miguel Giménez
Miguel Gimenez
Guest
Mon Sep 03, 2007 10:59 am
+mArkO+ escribió:
Quote:
necesito orientacion por parte de alguien que haya programado transmision
por puerto serie en linux
en lo posible algun codigo de ejemplo o sugerencias
gracias
Aqui tienes un trozo que lee una cadena del puerto serie y la muestra en
pantalla. Está sacado de un programa más grande y no he probado a
compilarlo. La parte de transmisión es bastante más fácil, pero no la
tengo a mano. Si quieres hacer polling puedes quitar la parte de
señales, pero el programa consumirá más recursos.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
void signal_handler_IO (int status);
volatile int data_ready = 0;
int main(int argc, char **argv)
{
int fd;
char Port[] = "/dev/ttyS0", Buffer[256];
struct sigaction saio;
/* Open the device to be non-blocking (read will return immediatly) */
fd = open(Port, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0)
return(1);
/* Install the signal handler before making the device asynchronous */
saio.sa_handler = signal_handler_IO;
sigemptyset(&saio.sa_mask);
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO, &saio, NULL);
/* Allow the process to receive SIGIO */
fcntl(fd, F_SETOWN, getpid());
/* Make the file descriptor asynchronous */
fcntl(fd, F_SETFL, FASYNC);
/* Save current port settings */
tcgetattr(fd, &oldtio);
/* Set new port settings for canonical input processing */
newtio.c_cflag = CRTSCTS | CS8 | CLOCAL | CREAD | PARENB;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio);
/* Wait for any signal */
pause();
if (data_ready)
{
read(fd, Buffer, sizeof(Buffer));
printf(Buffer);
}
/* Restore old port settings */
tcsetattr(fd, TCSANOW, &oldtio);
/* Close the port */
close(fd);
return(0);
}
void signal_handler_IO (int status)
{
data_ready = 1;
}
--
Saludos
Miguel Giménez