⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rs232.doc

📁 RS232.C was written to provide all of the basic functionality needed to employ serial I/O in any a
💻 DOC
📖 第 1 页 / 共 4 页
字号:
              occurred as follows:
                bit 0 (RS_RBER): 1 = receive buffer overflow
                bit 1 (RS_ROER): 1 = receive data overrun (UART received data
                                 faster than it could be read by the interrupt
                                 service routine)
                bit 2 (RS_PERR): 1 = parity error
                bit 3 (RS_FERR): 1 = framing error
                bit 4 (RS_BKDT): 1 = break detected
              Returns 0 if no error has been detected since last call to
              rs_error or since port was opened.

        >   Examples:

              if(rs_error())
                printf("Serial port error\n");

              if(rs_error() & RS_PERR)
                printf("Parity error!\n");

        >   Notes:
              A call to rs_error returns a code for any errors which have
              occurred and clears the error flags.  Subsequent calls will not
              indicate the same error unless it has reoccurred.

              Most UARTs will indicate a framing error when a break is
              detected.

      >   rs_modctrl

        >   Purpose:
              Controls or returns status of modem control lines.

        >   Prototype:
              int rs_modctrl(int rs_cmd,...);

        >   Arguments:
              rs_cmd = 2 (RS_GETMCR): Return status of modem control output
                                      lines.
              rs_cmd = 1 (RS_WRTMCR): Change status of modem control output
                         lines.  2 additional arguments are required with this
                         command. The first is an integer with a bit pattern
                         indicating which line(s) to control:
                            1 (RS_MCRDTR): Data Terminal Ready line.
                            2 (RS_MCRRTS): Ready To Send line.
                         The second is an integer determining whether the
                         line(s) should be turned on or off:
                            0 (RS_LINOFF): Turn line off.
                            1 (RS_LINON): Turn line on.
              rs_cmd = 0 (RS_GETMSR): Return status of modem control input
                         lines.

        >   Return Value:
              If rs_cmd = 0 (RS_GETMSR), an integer with a bit pattern
              indicating the requested status is returned:
                0x01 (RS_CTSCHG) CTS line changed states
                0x02 (RS_DSRCHG) DSR line changed states
                0x04 (RS_RICHG)  RI line changed states
                0x08 (RS_DCDCHG) DCD line changed states
                0x10 (RS_CTSSTE) state of CTS line
                0x20 (RS_DSRSTE) state of DSR line
                0x40 (RS_RICHG)  state of RI line
                0x80 (RS_DCDSTE) state of DCD line
              If rs_cmd = 1 (RS_WRTMCR), zero is returned.
              If rs_cmd = 2 (RS_GETMCR), an integer with a bit pattern
              indicating the current state of the modem control output lines
              is returned:
                0x01 (RS_MCRDTR): 1 = DTR on
                0x02 (RS_MCRRTS): 1 = RTS on
              If an operation fails (no port open), -1 is returned.

        >   Examples:

              if(! (rs_modctrl(RS_GETMSR) & RS_MSRDSR))
                printf("Remote equipment is not ready\n");

              /* set Data Terminal Ready output line on */
              rs_modctrl(RS_WRTMCR,RS_MCRDTR,RS_LINON);

              if(rs_modctrl(RS_GETMCR) & RS_MCRRTS)
                printf("Ready To Send output line is currently off\n");

        >   Notes:
              When using rs_modctrl to return the status of the modem status
              input lines (rs_cmd = RS_GETMSR), the lower 4 bits of the value
              returned indicate that the corresponding modem status lines have
              changed states since the last time the status was read.
              The next time the status is read, these bits will be cleared
              unless a line has again changed states.

              When using rs_modctrl to set modem control output lines, more
              than 1 line can be turned on or off with the same call by or'ing
              the bit pattern for the desired lines (i.e.
              rs_modctrl(WRTMCR,RS_MCRDTR | RS_MCRRTS,RS_LINON) ).

      >   rs_break

        >   Purpose:
              Sends break to remote equipment.

        >   Prototype:
              int rs_break(void);

        >   Return Value:
              Returns 0 on success, -1 if no port open.

        >   Examples:

              if(rs_error())
                rs_break();

        >   Notes:
              An RS232 line is normally in the "marking" state when idle.
              rs_break places the line in a spacing state for approx. 1
              character period.  It is used by some communications equipment
              to signal some event.

              If a buffer transmit is currently in progress, rs_break will
              wait until the buffer is empty before sending the break unless
              output is currently disabled via flow control.  In this case,
              rs_break will send the break immediately.

      >   rs_clrout

        >   Purpose:
              Clears output buffer (and the UART output FIFO if used).  If
              RS_POLLED_XMIT is defined, clears the output FIFO only.

        >   Prototype:
              void rs_clrout(void);

        >   Return Value:
              none

        >   Examples:

              if(rs_error() & RS_BKDT)  /* break detected */
                rs_clrout();            /*  stop transmission, clear output */

        >   Notes:
              rs_clrout will immediately stop transmission and clear the
              output buffer.  It has no effect if a port is not open.

      >   rs_clrin

        >   Purpose:
              Clears input buffer.

        >   Prototype:
              void rs_clrin(void);

        >   Return Value:
              none

        >   Examples:

              if(rs_error()){ /* if an error occurred */
                rs_clrin();   /*    clear input buffer */
                rs_sndstr(0,"resend last message");
                }

        >   Notes:
              rs_clrin will immediately clear the input buffer of any
              characters not yet read.  If a UART FIFO is in use, it will also
              be cleared.

      >   rs_keyhit

        >   Purpose:
              Determines if a key has been pressed.

        >   Prototype:
              int rs_keyhit(void);

        >   Return Value:
              Returns 0 if no keys have been pressed, 1 if one or more keys
              have been pressed but not yet read.

        >   Examples:

              if(rs_keyhit())
                rs_sndbyt(getch());

        >   Notes:
              When constant keyboard monitoring is required, rs_keyhit should
              be used in place of library functions like kbhit().  rs_keyhit
              reads the BIOS keyboard data area directly to determine if any
              keys have been pressed.  Library functions like kbhit() call a
              BIOS interrupt routine to accomplish the same thing.  This can
              cause loss of incoming serial data when using high transmission
              speeds.  A port need not be open to use this function.

      >   rs_timer

        >   Purpose:
              Times events or operations.

        >   Prototype:
              unsigned rs_timer(int rs_cmd);

        >   Arguments:
              One integer argument determines the action which will be taken:
                0 (RS_CLRTIM): The current timer value is returned and the
                               timer is set to 0.
                1 (RS_GETTIM): The current timer value is returned and timing
                               continues.

        >   Return Value:
              rs_timer returns an unsigned integer indicating the number of
              "ticks" (18.2 per second) which have elapsed.

        >   Examples:

              rs_timer(RS_CLRTIM);
              rs_sndstr(msg_len,msg);
              while(! rs_inrcvd()){
                if(rs_timer(RS_GETTIM) > 18){ /* wait approx. 1 second */
                  printf("Message response timed out\n");
                  break;
                  }
                }

        >   Notes:
              rs_timer reads the BIOS time data area directly and avoids use
              of BIOS interrupt calls to do this.  It should be used instead
              of library routines such as biostime() to avoid loss of incoming
              serial data when performing high speed communication.  The value
              returned may be in error by as much as 1 "tick" so should be
              considered a fairly low resolution timer.  A port need not be
              open to use this function.

      >   rs_setflow

        >   Purpose:
              Establishes or returns status of flow control.

        >   Prototype:
              int rs_setflow(int rs_cmd,...);

        >   Arguments:
              rs_cmd can be one of the following:
                0 (RS_FLWOFF) -  Disables flow control.
                1 (RS_FLWHDW) -  Enables hardware flow control.  Requires an
                                 additional integer argument to define hardware
                                 line(s) to use for flow control:
                                   1 (RS_FLWCTS) - Use Clear To Send
                                   2 (RS_FLWDSR) - Use Data Set Ready
                                   4 (RS_FLWRI) - Use Ring Indicator
                                   8 (RS_FLWDCD) - Use Data Carrier Detect
                2 (RS_FLWXON) -  Enables XON/XOFF flow control.  Requires two
                                 additional parameters, the first of which
                                 defines the character to use for XON, the
                                 second defining the character to use as XOFF.
                                 The following two pre-defined constants are
                                 the standard XON and XOFF characters:
                                   0x11 (RS_XON)
                                   0x13 (RS_XOFF)
                3 (RS_FLWSTAT) - Returns the status of flow control.  1
                                 indicates that output is currently disabled
                                 by flow control.  0 indicates output is
                                 currently enabled.
                4 (RS_FLWINS)  - Inserts a control character in the output
                                 stream.  The control character to be sent is
                                 defined by one additional argument.


        >   Return Value:
              Returns -1 if no port open.  Except when rs_cmd is 3
              (RS_FLWSTAT), returns 0 on success.  When rs_cmd is RS_FLWSTAT,
              value returned is status of flow control.

        >   Examples:

              /* set flow control to use Data Set Ready hardware line */
              rs_setflow(RS_FLWHDW,RS_FLWDSR);

              /* set flow control to XON/XOFF using standard XON and XOFF
                 characters */
              rs_setflow(RS_FLWXON,RS_XON,RS_XOFF);

              if(rs_setflow(RS_FLWSTAT))
                printf("Output currently disabled via flow control\n");

              /* send XOFF character to remote */
              rs_setflow(RS_FLWINS,RS_XOFF);

              /* disable flow control */
              rs_setflow(RS_FLWOFF);

        >   Notes:
              Enabling flow control, whether with hardware line monitoring or
              with XON/XOFF characters, will cause output to stop immediately
              when the defined condition is met (the selected hardware line
              goes to logic 0 or the character used as XOFF is received).  Any
              characters buffered for output will remain buffered and
              transmission will resume when the hardware line being used for
              flow control goes to logic 1 or the character used for XON is

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -