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;
}