Information Technology Reference
In-Depth Information
int get_buffer(void)
{
int ch;
if (startbuf == endbuf) return (-1);
ch = (int) buffer[startbuf];
startbuf++;
if (startbuf == RSBUFSIZE) startbuf = 0;
return (ch);
}
The
get_character()
and
send_character()
functions are similar to those developed in
Chapter 13. For completeness, these are listed next.
void send_character(int ch)
{
char status;
do
{
status = inportb(LSR) & 0x40;
} while (status!=0x40);
/*repeat until Tx buffer empty ie bit 6 set*/
outportb(TXDATA,(char) ch);
}
int get_character(void)
{
int status;
do
{
status = inportb(LSR) & 0x01;
} while (status!=0x01);
/* Repeat until bit 1 in LSR is set */
return( (int)inportb(TXDATA));
}
The
main()
function calls the initialization and the de-initialization functions. It also contains
a loop, which continues until the Esc key is pressed. Within this loop, the keyboard is tested
to determine if a key has been pressed. If it has then the
getche()
function is called. This
function returns a key from the keyboard and displays it to the screen. Once read into the
variable
ch
it is tested to determine if it is the Esc key. If it is then the program exits the loop,
else it transmits the entered character using the
send_character()
function. Next the
get_buffer()
function is called. If there are no characters in the buffer then a -1 value is
returned, else the character at the start of the buffer is returned and displayed to the screen
using
putch()
.
int main(void)
{
int ch, done = FALSE;
setup_serial();
/* set new interrupt vectors and store old ones*/
set_vectors();
enable_interrupts();
printf("Terminal emulator, press [ESC] to quit\n");
do