Ol� Gabriel,
Embora n�o seja do escopo da lista, aqui vai um fonte que ilustra bem o
uso da serial no linux.
Ele � bem simples. Tudo o que se digita no teclado vai pela serial,e o
quem vem eh exibido na tela.
Qualquer d�vida, estamos ai
-Rafael
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/time.h>
int main(int argc, char **argv)
{
int fd, kbd;
char letra, *portname;
char msg[80];
struct termios mytty, kbdios;
fd_set descritores; /* Set of i/o handles */
struct timeval timeout;
/* Open serial port */
portname="/dev/ttyS0"; /* Use /dev/ttyS0 by default */
/* or get the name of the serial port device from the command line */
if(argc>1 && *argv[1]=='/') portname=argv[1];
/* Open the serial port */
fd=open(portname, O_RDWR | O_NDELAY);
if(fd==-1)
{
sprintf(msg, "\nUnable to open \"%s\"", portname);
perror(msg);
return -1;
}
/* Set attributes of the serial interface to raw mode */
if(tcgetattr(fd, &mytty)==-1)
{
perror(NULL);
close(fd);
return -1;
}
mytty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
|INLCR|IGNCR|ICRNL|IXON);
mytty.c_oflag &= ~OPOST;
mytty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
mytty.c_cflag &= ~(CSIZE|PARENB);
mytty.c_cflag |= CS8; /* 8 bit data bytes */
/* Set Baud Rate to 9600 baud */
cfsetispeed(&mytty, B9600);
cfsetospeed(&mytty, B9600);
if(tcsetattr(fd, TCSANOW, &mytty)==-1)
{
perror(NULL);
close(fd);
return -1;
}
/* Get keyboard handle (assuming that stdin is the keyboard and video) */
kbd=fileno(stdin);
/* Set keyboard to raw mode */
tcgetattr(kbd, &kbdios);
memcpy(&mytty, &kbdios, sizeof(struct termios));
mytty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
tcsetattr(kbd, TCSANOW, &mytty);
/* These calls make read to return imedately even when there is
no data in the input stream */
fcntl(fd, F_SETFL, FNDELAY);
fcntl(kbd, F_SETFL, FNDELAY);
/* Now we may use the serial interface and keyboard for sending
just one character at a time */
printf("\nSerial tty terminal on port \"%s\"\n", portname);
printf("Type ctrl-X (meta-X) to exit.\n");
do
{
/* Wait for any activitiy in the serial port (fd) or keyboard (kbd) */
FD_ZERO(&descritores); /* Inicializa a lista de handles */
FD_SET(fd, &descritores);
FD_SET(kbd, &descritores);
timeout.tv_sec=5;
timeout.tv_usec=0;
select(FD_SETSIZE, &descritores, NULL, NULL, &timeout);
while(read(fd, &letra, 1) >= 0) /* Get a char from serial port "fd"
*/
write(kbd, &letra, 1); /* Send it to the console "kbd"
*/
while(read(kbd, &letra,1) >= 0) /* Get a char from "kbd" (keyboard)
*/
write(fd, &letra, 1); /* send it to the serial port "fd"
*/
} while(letra!='X'-64); /* if the key was Ctrl-X exit
*/
close(fd);
/* Set console back to normal */
fcntl(kbd, 0, FNDELAY);
tcsetattr(kbd, TCSANOW, &kbdios);
putchar('\n');
return 0;
}
-----Mensagem Original-----
De: <[EMAIL PROTECTED]>
Para: <[EMAIL PROTECTED]>
Enviada em: Segunda-feira, 24 de Setembro de 2001 18:31
Assunto: (linux-br) comunicacao serial
Ola pessoal,
Eu estive trabalhando com comunicacao serial
usando programas em C, e tenho tido alguns
probleminhas.
Quando eu seto a velocidade (por exemplo, 9600),
a taxa de transmiss�o acaba sendo superior em
ordens de magnitude (nesse caso, 19200).
Se eu coloco 19200, obtenho 36k.
A detec��o de que a velocidade est� incorreta
foi atrav�s de um oscilosc�pio mesmo.
Outro problema que eu tive, talvez relacionado,
� que em determinadas situa��es, parece que os
dados n�o s�o transmitidos (se bem que isso pode
ser problema do receptor).
O que eu estou usando para configurar �, basicamente, o seguinte:
long int ARGN, data;
struct termios newtio;
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
cfsetospeed(&newtio, BAUDRATE);
cfsetispeed(&newtio, BAUDRATE);
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
Alguem pode me ajudar?
Valeu,
Gabriel
Assinantes em 25/09/2001: 2368
Mensagens recebidas desde 07/01/1999: 133943
Historico e [des]cadastramento: http://linux-br.conectiva.com.br
Assuntos administrativos e problemas com a lista:
mailto:[EMAIL PROTECTED]