[comp.os.msdos.programmer] Help! serial comm problems

kurtenba@parc.xerox.com (Gord Kurtenbach) (07/26/90)

I'm not a DOS programmer and I`m trying to write a program to read and 
write over the serial port. The basic structure is:

while (1) {

	<if there is something to send out then stuff it down the serial port >

	<if something has been sent to the serial port then read it>
}


I've tried opening "com2" using fopen and I can successsfully stuff
data down the stream. But when I try to read from the stream with say
a call to fgetc, I get a "General failure reading com2:" errormessage.
I've fopened com2 for reading and writing and the baud rates etc are
correct and I can read data from the hardware port using bioscom so
communication is working.  What is wrong here? Will this approach work
or am I on the wrong track?

My second question is, if I do get the read working how can I avoid 
blocking on it?

If some kind soul out there has a stitch of code (C would be great)
that does what I want I would be most thankful for their help.

Thanks.

Gord Kurtenbach

PS: I've notice that the DOS command "copy com2: junk" also gives the
same error. Could my system be configured wrong?

coy@ssc-vax.UUCP (Stephen B Coy) (07/28/90)

In article <487@roo.UUCP>, kurtenba@parc.xerox.com (Gord Kurtenbach) writes:
> 
> I'm not a DOS programmer and I`m trying to write a program to read and 
> write over the serial port. The basic structure is:
> 
> while (1) {
> 
> 	<if there is something to send out then stuff it down the serial port >
> 
> 	<if something has been sent to the serial port then read it>
> }
> 
> If some kind soul out there has a stitch of code (C would be great)
> that does what I want I would be most thankful for their help.
> 
> Thanks.
> 
> Gord Kurtenbach

This is for Microsoft C although I think the only thing you'll need
to change for Turbo C or others is the _bios_serialcom() function
calls.  Since its not a standard function everyone has created their
own standards.  Hope this helps.

Stephen Coy
uw-beaver!ssc-vax!coy


/*
	Test serial port interaction.  Keyboard hits are sent across
	the serial line, characters received are displayed on the
	screen.  Hit ESC to exit.
*/

#include <stdio.h>
#include <stdlib.h>
#include <bios.h>

#define COM1	(0)
#define COM2    (1)

#define COM	COM1
#define PARITY	_COM_NOPARITY
#define BITS	_COM_CHR8
#define STOP	_COM_STOP1
#define BAUD	_COM_9600

main(int ac, char **av)
{
	int     c, status, error;

	/* init serial port, 8bits, 1s stop bit, no parity, 9600 baud */

	_bios_serialcom(_COM_INIT, COM, BITS | STOP | PARITY | BAUD);

	for(;;) {
		/* Is DATA_READY set? If so, grab the character. */
		status = 0x0100 & _bios_serialcom(_COM_STATUS, COM, 0);
		if(status) {
			c = 0x00ff & _bios_serialcom(_COM_RECEIVE, COM, 0);
			printf("%c", c);
		}

		/* Do we have data to send? */

		if(kbhit()) {
			c = getch();

			/* bail if esc as hit */
			if(c == 27) {
				printf("\n\nbye...\n");
				exit(0);
			}

			/* wait until transmit hold reg empty */

			status = 0x2000 &_bios_serialcom(_COM_STATUS, COM, 0);
			while(status != 0x2000) {
				status = 0x2000 &_bios_serialcom(_COM_STATUS, COM, 0);
			}

			status = _bios_serialcom(_COM_SEND, COM, c);
			if(status & 0x8000) {
				fprintf(stderr, "\nError send %c\n", c);
			}
		}       /* end of if kbrd hit loop */
	}       /* end of forever loop */

}       /* end of main */