Technical information
Cterm Serial Solutions
Figure 7-1. Function open_com()._____________________________
FILE *com_inp, *com_out;
/* Files for: com port input */
/* com port output */
char comname[] = "COM1\0 "; /* Name */
/* Open com port. */
void open_com(void)
{
char bf[80]; /* Temporary text buffer. */
com_inp = fopen(comname,"rb"); /* Open port input */
if( com_inp == NULL )
{
sprintf(bf, "Failed to open %s\nTry different params.\n"
,comname);
screen(bf, fromctrl);
}
else
uncook(com_inp); /* Set to raw data mode. */
rewind( com_inp );
com_out = fopen(comname,"wb"); /* Open port output */
if( com_out == NULL )
{
sprintf(bf, "Failed to open %s\nTry different params.\n"
,comname);
screen(bf, fromctrl);
fclose(com_inp);
}
else
uncook(com_out); /* Set to raw data mode. */
rewind( com_inp );
}
Open_com() shows the two file variables (com_inp for
input and com_out for output) that refer to the serial port. A
single file could be used but this introduces the risk of losing
characters as the file buffer maintained by C is flushed between
reads and writes. The fopen() function creates a file structure for
the com port that these variables refer to. It takes two operands,
the name of the file and a string showing how the file will be
used. In this latter string the character ’r’ means ’read’, ’w’
means ’write’, and ’b’ means that the file is a binary file. The
last parameter prevents C from translating end of file and end of
line characters. We set this so that we can accurately control the
Chapter 7 Page 119