Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DOS Int14 Service 04 Serial Question

Status
Not open for further replies.

deanlwvu

Technical User
Jul 23, 2003
25
US
I'm trying to pull a character from the serial port, and I'm having some trouble. I need to have a program listen to the serial port and print a character if it sees one, or send a character out if a key is pressed. Using Turbo C 2.1 on a Windows 2k machine.

Code:
/* -- Includes -- */

#include <stdio.h>
#include <dos.h>

/* -- Function Prototypes -- */

int initialize_serial_port(void);
void serial_out(unsigned char);
unsigned char serial_in(void);

int main()
{
	unsigned char key_out = 0;
	unsigned char key_in = 0;
	int serial_status = 0;
	system("CLS");
	gotoxy(0,0);

	serial_status = initialize_serial_port();

	if (serial_status == 1)
	{
        	while(key_out != 27)	/* Looking for an ESC character */
		{

			if (kbhit())
			{
				key_out = getch();
				serial_out(key_out);
			}


			key_in = serial_in();
			if (key_in != NULL)
			{
				printf("%c", key_in);
			}

		}
	}
	return 0;
}

int initialize_serial_port()		/* Setting serial port up for COM2, 9600, 8, N, 1 */
{
	_AH = 04;
	_AL = 00;
	_DX = 00;
	_BH = 00;
	_BL = 00;
	_CH = 03;
	_CL = 07;
	geninterrupt(0x14);
	return 1;
}

void serial_out(unsigned char key)	/* Sends data out one character at a time */
{
	_AH = 01;
	_AL = key;
	_DX = 00;

	geninterrupt(0x14);
}

unsigned char serial_in(void)		/* Checks serial port for data */
{
	_AH = 02;
	_DX = 00;

	geninterrupt(0x14);
	return _AL;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top