tony@joshua.math.ucla.edu (03/10/89)
Hello Netland, I'm trying to read from the com1: port
on my AT clone and I can't seem to get it right. I hope someone
out there can tell me what it is that I'm doing wrong.
I first do a DOS mode com1:1200,n,8,1. Then from within a C program
(I'm using MSC5.1) I do a fopen ("COM1","r+"). I can write to the serial
port fine (I tested this by hooking a serial printer up to it) but when I
try to read from it I just get garbage (I have a data collection
box hooked up to COM1 and it should give me the ascii date string when I
press return).
BTW when I use kermit everything works fine.
My code looks something like this:
#include <stdio.h>
main() {
FILE *fp,*fopen() ;
char buffer[513] ;
if ((fp = fopen ("COM1","r+))==NULL) {
fprintf (stderr,"\nCould not open COM1\n\n") ;
exit (1) ;
}
fprintf (fp,"%c",13) ; fflush (fp) ; /* Not \n, just ^M */
fgets (buffer,512,fp) ;
printf ("%s\n",buffer) ;
exit (0) ;
}
The result from this code is that sometimes it hangs at the fgets line,
other times it returns garbage in buffer.
I've checked the cabling and its not that.
I hope someone one out there can give me a clue!
Thanks in advance.
Tony Lei
Internet: tony@math.ucla.edu
UUCP: ...!ucbvax!ucla-cs!math.ucla.edu!tony