DEFINITION MODULE RS232;

(* (C) Copyright 1987 Fitted Software Tools. All rights reserved.

    This module is part of the example multitasking communications program
    provided with the Fitted Software Tools' Modula-2 development system.

    Registered users may use this program as is, or they may modify it to
    suit their needs or as an exercise.

    If you develop interesting derivatives of this program and would like
    to share it with others, we encourage you to upload a copy to our BBS.
*)

(*
    This module provides buffered, interrupt driven, communications I/O
*)

FROM Kernel     IMPORT SignalHeader, LockHeader;

VAR RS232Input  :SignalHeader;  (* everytime a character is received *)
                                (* a Signal is sent to this SignalHeader *)


(*
    When Init is invoked, a COM port is initialized for communications
    I/O and a process is started to service the interrupts from the COM
    port.

    The COM interrupt service routine places the characters received
    in a circular buffer, the size of which is specified when Init
    is called.
*)

(*========================================================================*)
(*  Initializing the port.
    Init must be called before issuing I/O to the COM port.
*)

PROCEDURE Init( portNumber      :CARDINAL;      (* 1 or 2       *)
                baudRate        :CARDINAL;      (* 300..38400   *)
                nStopBits       :CARDINAL;      (* 1 or 2       *)
                parityEnable    :BOOLEAN;
                evenParity      :BOOLEAN;
                charSize        :CARDINAL;      (* 5..8         *)
                inBufferSize    :CARDINAL;      (* input buffer size *)
                VAR done        :BOOLEAN        (* success      *)
              );
    (*  Initialize the COM port *)


PROCEDURE ResetPars( baudRate        :CARDINAL;      (* 300..38400   *)
                     nStopBits       :CARDINAL;      (* 1 or 2       *)
                     parityEnable    :BOOLEAN;
                     evenParity      :BOOLEAN;
                     charSize        :CARDINAL;      (* 5..8         *)
                     VAR done        :BOOLEAN        (* success      *)
                     );
    (* reset the parameters of the active COM port *)


(*========================================================================*)
(*  Reading and Writing to the COM port:
*)

PROCEDURE GetCom( VAR ch :CHAR; VAR received :BOOLEAN );
    (*  IF a character is available for reading THEN
            ch gets the next character,
            received is set to TRUE
        ELSE
            received is set to FALSE
    *)

PROCEDURE PutCom( ch :CHAR );
    (*  sends ch to the remote system *)


(*======================================================================*)
(*  XON / XOFF processing:
    If enabled, the input interrupt service routime will send an XOFF
    to the remote system whenever the input buffer is over 75% full.
    An XON will be sent (if an XOFF was sent) when the input buffer
    is 50% empty.

    Unless XON is called, XON / XOFF is not enabled.
*)

PROCEDURE XON;
    (* enable xon/xoff processing *)

PROCEDURE XOFF;
    (* disable xon/xoff processing *)

END RS232.