Information Technology Reference
In-Depth Information
13.6.25 Modify the get_character() routine so that it returns an error flag if it detects an
error or if there is a time-out. Table 13.7 lists the error flags and the returned error
value. An outline of the C code is given in Program 13.6. If a character is not re-
ceived within 10 s an error message should be displayed.
Table 13.7
Error returns from get_character().
Error condition
Error flag
return
Notes
Parity error
-1
Overrun error
-2
Framing error
-3
Break detected
-4
Time out
-5
get_character() should time out if no
characters are received with 10 seconds.
Test the routine by connecting two PCs together and set the transmitter with dif-
fering RS-232 parameters.
Program 13.6
#include <stdio.h>
#include <dos.h>
#define TXDATA 0x3F8
#define LSR 0x3FD
#define LCR 0x3FB
void show_error(int ch);
int get_character(void);
enum RS232_errors {PARITY_ERROR=-1, OVERRUN_ERROR=-2,
FRAMING_ERROR=-3, BREAK_DETECTED=-4, TIME_OUT=-5};
int main(void)
{
int inchar;
do
{
inchar=get_character();
if (inchar<0) show_error(inchar);
else printf("%c",inchar);
} while (inchar!=4);
return(0);
}
void show_error(int ch)
{
switch(ch)
{
case PARITY_ERROR: printf("Error: Parity error/n"); break;
case OVERRUN_ERROR: printf("Error: Overrun error/n"); break;
case FRAMING_ERROR: printf("Error: Framing error/n"); break;
case BREAK_DETECTED: printf("Error: Break detected/n");break;
case TIME_OUT: printf("Error: Time out/n"); break;
}
}
int get_character(void)
{
int instatus;
do
{
instatus = _inp(LSR) & 0x01;
 
Search WWH ::




Custom Search