Hello I'm doing a program about UART in Borland C. And I have a few questions. This is the code:

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

#define COM1 0x3F8

int main(void)
{
  clrscr();

//  outportb( COM1 + 1, 0 );
  outportb( COM1 + 3, 0x83);
  outportb( COM1, 0x0C);
  outportb( COM1 + 1, 0x00);
  outportb( COM1 + 3, 0x03);

  char ans;
  char readValue;

  do
  {

	readValue = inportb( COM1 + 5 );

	if ( readValue & 0x80 )
	{
	   printf("ERROR!");
	   continue;
	}

	if ( readValue & 1 )
	{
	   readValue = inportb( COM1 );
	   printf("%c", readValue);
	}

	if ( kbhit() )
	{
	   ans = getch();
	   outportb( COM1, ans );
	}


  } while ( ans != 27 );


  return 0;

}

The program reads char data from the COM1 port, and finds if there's any errors.
My question is how to simplify this program, or write it in different way. I need to present two different codes and I don't have idea for the second one.
Thanks!!!

Recommended Answers

All 3 Replies

Does the second program have to do the same thing as the first but must be written differently?
You could always do a switch on readValue instead of the if tree.

Yes the second one should do the same as the first one, only I need it written with different commands or simply in different way.

Can someone give me some example?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.