[comp.unix.programmer] Read and write /dev/ttya?

montnaro@spyder.crd.ge.com (Skip Montanaro) (01/19/91)

I'm trying to write some code to control a Lyon-Lamb MiniVAS animation
sequencer and am having a bit of a problem getting status info back from the
device.

The MiniVAS is an RS-232 device. I have it plugged into /dev/ttya on a Sun-3
through a null modem. Some of the commands evoke a one or more character
response from the box which have to be read to determine the transaction
status. For instance, the initialization sequence "VSG" evokes an "I"
response when initialization is complete.

All communication with the device is printable ASCII, so for simplicity I'm
just using stdio. I'm successful writing to the device. (This I know because
the MiniVAS and the VTR do the anticipated things.) However, I can't seem to
read the response stream. Here's what I'm doing in effect (sans declarations
and error checking):

	f = fopen("/dev/ttya", "r+");
	fprintf(f, "VSG");
	fflush(f);

	ch = getc(f);

	if (ch != 'I') ...

The getc() just hangs. What am I doing wrong?

Thx,

--
Skip (montanaro@crdgw1.ge.com)

barmar@think.com (Barry Margolin) (01/19/91)

In article <MONTNARO.91Jan18213947@spyder.crd.ge.com> montanaro@crdgw1.ge.com (Skip Montanaro) writes:
>The getc() just hangs. What am I doing wrong?

Does the device send carriage return after its response?  If not, you have
to put the terminal device in the mode where it returns characters
immediately.  You don't say what flavor of Unix you're using, so I can't
tell you the exact ioctl to use.
--
Barry Margolin, Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar

veit@du9ds3.uni-duisburg.de (Holger Veit) (01/22/91)

montnaro@spyder.crd.ge.com (Skip Montanaro) writes:


[...]
>All communication with the device is printable ASCII, so for simplicity I'm
>just using stdio. I'm successful writing to the device. (This I know because
      ^^^^^^^^^^^^
>the MiniVAS and the VTR do the anticipated things.) However, I can't seem to
>read the response stream. Here's what I'm doing in effect (sans declarations
>and error checking):

>	f = fopen("/dev/ttya", "r+");
>	fprintf(f, "VSG");
>	fflush(f);

>	ch = getc(f);

>	if (ch != 'I') ...

>The getc() just hangs. What am I doing wrong?

stdio does input and output buffering. If your answering device does not
deliver a Carriage return as a block limiter, your getc() call will wait
forever. Try including 
	setbuf(f,NULL);	
as described in setbuf(3V) after your fopen statement, this should switch
to unbuffered I/O.

Holger Veit


--
|  |   / Holger Veit             | INTERNET: veit@du9ds3.uni-duisburg.de
|__|  /  University of Duisburg  | BITNET: veit%du9ds3.uni-duisburg.de@UNIDO
|  | /   Fac. of Electr. Eng.    | UUCP:   ...!uunet!unido!unidui!hl351ge
|  |/    Dept. f. Dataprocessing | 

agm@stl.stc.co.uk (Andrew G. Minter) (01/23/91)

In the referenced article veit@du9ds3.uni-duisburg.de (Holger Veit) writes:
>montnaro@spyder.crd.ge.com (Skip Montanaro) writes:
>>The getc() just hangs. What am I doing wrong?
>stdio does input and output buffering. If your answering device does not
>deliver a Carriage return as a block limiter, your getc() call will wait
>forever. Try including 
>	setbuf(f,NULL);	

This surely depends on where the buffering is done.  Most UNIX systems will,
by default, do line buffering on tty input in the kernel.  You may find it
necessary to put the tty into CBREAK or RAW mode using an ioctl code.  I
have a deep mistrust of stdio.  It seems that the "tricky bits" work
*slightly* different on every UNIX box I come to and it's very hard to write
portable code.  It may be a better idea to keep to low level I/O when
talking to a device.

Andrew

klaus@cnix.uucp (klaus u schallhorn) (01/24/91)

In article <3971@stl.stc.co.uk> "Andrew G. Minter" <agm@stl.stc.co.uk> writes:
>In the referenced article veit@du9ds3.uni-duisburg.de (Holger Veit) writes:
>>montnaro@spyder.crd.ge.com (Skip Montanaro) writes:
>>>The getc() just hangs. What am I doing wrong?
>>stdio does input and output buffering. If your answering device does not
>>deliver a Carriage return as a block limiter, your getc() call will wait
>>forever. Try including 
>>	setbuf(f,NULL);	
>
Unless you set ICANON off it's going to wait until it receives
a linefeed. Have a look in termio(4).

klaus
-- 
George Orwell was an Optimist