[comp.lang.c] Serial stuff

thielen@uwovax.uwo.ca (02/20/91)

I have been having a bunch of trouble reading the output
from a very simple voice activator box attached to a
serial line on my Sun 3/50. Now, I think I am just completely
off base, but can't really fathom what else I should do.
The code I am using is the following... and the program
just sits in the fread command and never gets out of it.

Any Ideas???

The code is here.. /* It's pretty simple stuff */

#include <stdio.h> 
#include <fcntl.h> 
#include <sys/termios.h> 
#define TRUE 1
#define FALSE 0 
int size = 1; 
int nitems = 1; 
FILE *stream; 
char    *buf; 
char   input[10]; 
main() 
{ 
if ((stream = fopen("/dev/ttya", "r")) == NULL){        
    printf("cannot open serial port \n");         
    exit(0); 
}      
setbuffer(stream,buf, size); 
fread( input, size, nitems, stream);
fprintf("\nread character input %s", input); 
fclose(stream); 
} 

Any, suggestions, flames, whining would be greatly appreciated.


Susan KJ Thielen
Centre for Cognitive Science
UWO

vongease@hplred.HP.COM (Terry Von Gease) (02/27/91)

>I have been having a bunch of trouble reading the output
>from a very simple voice activator box attached to a
>serial line on my Sun 3/50. Now, I think I am just completely
>off base, but can't really fathom what else I should do.
>The code I am using is the following... and the program
>just sits in the fread command and never gets out of it.
>
>Any Ideas???
>
>The code is here.. /* It's pretty simple stuff */
>
>#include <stdio.h> 
>#include <fcntl.h> 
>#include <sys/termios.h> 
>#define TRUE 1
>#define FALSE 0 
>int size = 1; 
>int nitems = 1; 
>FILE *stream; 
>char    *buf; 
>char   input[10]; 
>main() 
>{ 
>if ((stream = fopen("/dev/ttya", "r")) == NULL){        
>    printf("cannot open serial port \n");         
>    exit(0); 
>}      
>setbuffer(stream,buf, size); 
>fread( input, size, nitems, stream);
>fprintf("\nread character input %s", input); 
>fclose(stream); 
>} 
>
>Any, suggestions, flames, whining would be greatly appreciated.
>
>
>Susan KJ Thielen
>Centre for Cognitive Science
>UWO
>----------
 
The fread is waiting for for a newline. Obviously one is not forthcoming
from whatever you are trying to read from. You gotta get down to the 
bare metal to read serial ports. Use open(), then ioctl() to turn off 
canonical mode, then read one character at a time.

#include <stdio.h>
#include <termio.h>

...

int innum;
struct termio tbuf;

innum=fileno(stream);

...

ioctl(innum,TCGETA,&tbuf);     /*turn off canonical & echo mode*/
tbuf.c_lflag&=~ICANON;
tbuf.c_lflag&=~ECHO;
ioctl(innum,TCSETA,&tbuf);

...

read(innum,input,1);           /*read stuff*/

...

ioctl(innum,TCGETA,&tbuf);    /*turn on canonical & echo mode*/
tbuf.c_lflag|=ICANON;
tbuf.c_lflag|=ECHO;
ioctl(innum,TCSETA,&tbuf);

...

This is the idea anyway...

Terry