Path: utzoo!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!ux1.lbl.gov!beard From: beard@ux1.lbl.gov (Patrick C Beard) Newsgroups: comp.sys.mac.programmer Subject: Re: Help needed with Serial Port C code Keywords: Serial,C,Need example Message-ID: <546@helios.ee.lbl.gov> Date: 28 Jul 88 02:00:07 GMT References: <7044@trwrb.UUCP> Sender: usenet@helios.ee.lbl.gov Reply-To: beard@ux1.lbl.gov (Patrick C Beard) Organization: Lawrence Berkeley Laboratory, Berkeley Lines: 86 In article <7044@trwrb.UUCP> pro@trwrb.UUCP (Peter R. Olpe) writes: > > >I have been racking my brains trying to solve this problem: >Everytime I call the serial driver and ask it to read N bytes >(N>0) it returns immediately with NO bytes read. > >Could somebody please send me some sample C code which uses the >Serial Port? > >Many thanks in advance. >-- >----------------------------------------------------------------------- > ...ucbvax!\ -Pete Olpe- > UUCP Path: ...decwrl!decvax!trwrb!pro > ...ihnp4!/ It sounds like you aren't opening, and or setting aside a buffer for, the .AIN (or .BIN) serial driver. Here is a little example to get you started. /* serial_example.c */ #include Modem_Init(buf,len,OutRef,InRef) char *buf; long len; int *OutRef, *InRef; { int error; /* should check error */ error=OpenDriver("\p.AIn",&InRef); error=OpenDriver("\p.Aout",&OutRef); /* set the baud rate, etc... */ SerReset(*OutRef,rates[settings->baud_rate]+stop10+data8+noParity); SerReset(*InRef,rates[settings->baud_rate]+stop10+data8+noParity); /* now, set aside a buffer for the serial driver to use */ SerSetBuf(*InRef, buf, len); return(error); } mputs(s, out) char *s; int out; { SerStaRec bufStatus; long count; count=strlen(s); do /* make sure no other writes are pending */ { SerStatus(out, &bufStatus); } while(bufStatus.wrPend); FSWrite(out,&count,s); return((int)count); /* let caller know how many characters were sent */ } mgets(s,in,max) char *s; int in; int max; { long count; /* see if any characters are there */ SerGetBuf(in,&count); if(count) { if(count>max) count=max; FSRead(InRef,&count,s); s[count]='\0'; /* make a C string out of it */ } return((int)count); } I put this code together from a program I'm writing. Most of it is tested. I hope this helps. Patrick Beard PCBeard@lbl.gov (arpa)