Date: Mon, 2 Apr 2001 21:41:15 +0000 From: David <david@angra.uac.pt> To: questions@FreeBSD.org Subject: Working with the serial port (cuaa0) Message-ID: <01040221502102.04098@david>
next in thread | raw e-mail | index | archive | help
[-- Attachment #1 --]
Hello!
I was working with Mandrake but after finding some bugs in the operating
system, I decided to move to freeBSD.
I'm programming communications via RS-232 serial port.
I'm using the same source code (except the change ttyS0 <-> cuaa0) that I was
using with Mandrake but now I can't read from the serial port . I can write,
though...
Is ther any other change I have to the in my source in order to be able to read
from the port?
I'm sending my source code for the case someone can help ...
Cheers
David
------------------------------------------------------------
David Sousa Mendes
LAMTec- Laboratório de Ambiente Marinho e Tecnologias
Telefone : 96.4470312
Residência : R. da Graça, nº 90
9760 - Praia da Vitória
Ilha Terceira
[-- Attachment #2 --]
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/time.h>
#include <math.h>
#include <time.h>
/**********************Def. external variables************************************/
int fd;
struct termios oldtio;
/********************************************************************************/
/*****************************signal function************************************/
void sig_handler(int signum)
{
extern int fd;
extern struct termios oldtio;
printf("received SIGINT\n");fflush(stdout);
if( tcsetattr(fd,TCSANOW,&oldtio)==-1 )
{
perror(" erro em tcsetattr");
exit(0);
}
close(fd);
exit(1);
}
/********************************************************************************/
int main(void){
/********************Def. automatic functions*************************************/
extern int fd;
extern struct termios oldtio;
FILE * RS;
int res, n,i;
char buf[255];
struct termios newtio;
struct sigaction sa;
/********************************************************************************/
/********************** install signal handler for SIGINT, CTRL+C ***************/
sa.sa_handler = sig_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGINT, &sa, NULL))
{
printf("sigint error\n"); /* Could not set signal */
exit(-1);
}
/********************************************************************************/
if( (fd=open("/dev/cuaa1",O_RDWR))==-1 ){ //abrindo rs232
perror("erro na abertura da RS232");
exit(0);
}
if( tcgetattr(fd,&oldtio)==-1 ){ //guardando old attribs
perror(" erro em tcgetattr");
exit(0);
}
newtio=oldtio;
//execv("/home/david/mobiles/2400",NULL);
if( cfsetospeed(&newtio,B9600)==-1 ){ //nova out baud rate
perror(" erro em cfsetospeed");
exit(0);
}
if( cfsetispeed(&newtio,B9600)==-1 ){ //nova in baud rate
perror(" erro em cfsetospeed\n");
exit(0);
}
//Control Modes
newtio.c_cflag = B9600;
newtio.c_cflag &= ~CSTOPB; //negando, temos 1 stop bit
newtio.c_cflag &= ~PARENB; //negando, temos "no parity"
newtio.c_cflag = CS8; //8 data bits
//Local Modes
newtio.c_lflag &= ~ECHO; // echo on (if set)
newtio.c_lflag &= ~ICANON; // canonical mode(if set)
//Input Modes
newtio.c_iflag=0; //raw input, no input processing
//Output Modes
newtio.c_oflag &= ~OPOST; //negando, the char are transmitted as-is
newtio.c_cc[VMIN]=0;
newtio.c_cc[VTIME]=50;
if((RS=fdopen(fd,"w+"))==NULL ){ //transf.fd into file* STREAM
perror(" erro na transf. fd <-> STREAM");
}
if( tcsetattr(fd,TCSANOW,&newtio)==-1 ){ //new attribs
perror(" erro em tcsetattr");
exit(0);
}
fprintf(RS,"ATZ\r"); fflush(RS); tcdrain(fd);
sleep(3);
//HERE'S WHERE I'M TRYING TO READ!!
i=getc(RS);printf("%d",i);fflush(stdout); //EITHER WITH THIS COMMAND LINE OR
res=read(fd,buf,100);buf[res]=0;printf("%s",buf);fflush(stdout); //WITH THIS ONE
if( tcsetattr(fd,TCSADRAIN,&oldtio)==-1 )
{
perror(" erro em tcsetattr");
exit(0);
}
exit(0);
}
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?01040221502102.04098>
