C Language Development Kit/Functions/GetCom

From OptiWiki

Jump to: navigation, search
Description This function reads a character from the receive buffer from

the serial communications port that was opened last by comopen().

Syntax
int getcom(int timeout);
Arguments
int timeout
timeout is the time (in steps of 20ms) that the routine tries to get a character from the communications

port before returning. For example timeout = 10 specifies 200 milliseconds. When timeout = 0, the routine returns directly.

Returns On success, when a character is available, getcom() returns the received character. On error -1 is returned.
Remarks When the COM port was not opened previously, the error value -1 will be returned.
Example
#include "lib.h"
#include <stdio.h>
 
void main( void )
{
    int ch;
 
    comopen( COM0 );    // Open the COM port for the direct cable
 
    while( 1 )
    {
        if( (ch = getcom( 0 )) != -1)
        {
            putchar( ch );
            putcom( ch );
        }
 
        if( (ch = getchar()) != EOF)
        {
            putchar( ch );
            putcom( ch );
        }
        idle();    // Very important to lower the power consumption
    }
}


Views
Personal tools