[comp.sys.mac.programmer] Help needed with Serial Port C code

pro@trwrb.UUCP (Peter R. Olpe) (07/07/88)

 
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!/

leonardr@uxe.cso.uiuc.edu (07/11/88)

pro@trwrb.UUCP(Pete Olpe) writes in comp.sys.mac.programmer
 
>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?
> 
	Check out the June issue of MacTutor for an article on Serial Comm.


+---------------------------------+-----------------------------------+
+                                 +  Any thing I say may be taken as  +
+   Leonard Rosenthol             +  fact, then again you might decide+
+   President, LazerWare, inc.    +  that it really isn't, so you     +
+                                 +  never know, do you??             +
+   leonardr@uxe.cso.uiuc.edu     +                                   +
+   GEnie:  MACgician             +  MacNET: MACgician                +
+   Delphi: MACgician             +                                   +
+                                 +                                   +
+---------------------------------+-----------------------------------+

beard@ux1.lbl.gov (Patrick C Beard) (07/28/88)

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 <SerialDvr.h>

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)