• C

Serial port IRQ

Could someone teach me how to enable and use the serial port IRQ. I need this to detect whether there a transmitting or receiving error (like parity error, etc). I'm using turbo C on a 286/386 PC. I would appreciate if you could include some examples.

Thanks ;)

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

unsigned _bios_serialcom( unsigned service, unsigned serial_port, unsigned data );
service Communications service
serial_port Serial port to use
data Port configuration bits
#include <bios.h>
The _bios_serialcom routine provides serial communications services using INT 0x14. The serial_port argument is set to 0 for COM1, to 1 for COM2, and so on. The _bios_serialcom routine may not be able to establish reliable communications at baud rates in excess of 1200 baud ( _COM_1200) due to the overhead associated with servicing computer interrupts. Faster data communication rates are possible with more direct programming of serial-port controllers. For more details on serial-communications programming in C, see C Programmer's Guide to Serial Communications. (More information on this book is found in Other Books of Interest)
The service argument can be set to one of the following manifest constants:
·      _COM_INIT Sets the port to the parameters specified in the data argument
·      _COM_SEND Transmits the data characters over the selected serial port
·      _COM_RECEIVE Accepts an input character from the selected serial port
·      _COM_STATUS Returns the current status of the selected serial port
The data argument is ignored if service is set to _COM_RECEIVE or _COM_STATUS. The data argument for _COM_INIT is created by combining (with the OR operator) one or more of the following constants:
·      _COM_CHR7  7 data bits
·      _COM_CHR8 8 data bits
·      _COM_STOP1 1 stop bit
·      _COM_STOP2 2 stop bits
·      _COM_NOPARITY No parity
·      _COM_EVENPARITY Even parity
·      _COM_ODDPARITY Odd parity
·      _COM_110 110 baud
·      _COM_150 150 baud
·      _COM_300 300 baud
·      _COM_600 600 baud
·      _COM_1200 1200 baud
·      _COM_2400 2400 baud
·      _COM_4800 4800 baud
·      _COM_9600 9600 baud
The default value of data is 1 stop bit, no parity, and 110 baud.
Return Value
The function returns a 16-bit integer whose high-order byte contains status bits. The meaning of the low-order byte varies, depending on the service value. The high-order bits, following, have these meanings:
·      15 Timed out
·      14 Transmission-shift register empty
·      13 Transmission-hold register empty
·      12 Break detected
·      11 Framing error
·      10 Parity error
·      9 Overrun error
·      8 Data ready
When service is _COM_SEND, bit 15 is set if data could not be sent. When service is _COM_RECEIVE, the byte read is returned in the low-order bits if the call is successful. If an error occurs, any of the bits 9, 10, 11, or 15 is set. When service is _COM_INIT or _COM_STATUS, the low-order bits are defined as follows:
·      7 Receive-line signal detected
·      6 Ring indicator
·      5 Data set ready
·      4 Clear to send
·      3 Change in receive-line signal detected
·      2 Trailing-edge ring indicator
·      1 Change in data-set-ready status
·      0 Change in clear-to-send status
Note that this function works only with IBM personal computers and true compatibles.
ANSI    MS-DOS    QWIN    UNIX    WIN    WIN DLL        
/* BSERIALC.C: This program checks the status of serial port COM1. */

#include <bios.h>
#include <stdio.h>

void main( void )
   unsigned com1_status;

   com1_status =  _bios_serialcom( _COM_STATUS, 0, 0 );
   printf ( "COM1 status: 0x%.4x\n", com1_status );

Another possiblilty

/* Compile options needed: none
 - The following program is a simple example which sends and receives
   one character to/from COM1:
#include <stdio.h>
#include <bios.h>
void main(void)
    unsigned com1_status;
    unsigned com1_send;
    unsigned com1_rec;
    unsigned com1_init;
    int result, mask;
    /* open serial port at 1200 baud, 8 data bits,
    ** No parity, 1 stop bit */
    com1_init = _bios_serialcom(_COM_INIT, 0,
        _COM_CHR8 | _COM_NOPARITY | _COM_STOP1 | _COM_1200);
    printf("Init status: 0x%4.4X\n", com1_init);
    /* send an '*' to com1 */
    com1_send = _bios_serialcom(_COM_SEND, 0, '*');
    printf("Send status: 0x%4.4X\n", com1_send);
    mask = 0x6100;
    /* value used to mask:
    *    bits 0-7 are related to modems,
    *    bits 8-15 are for port status,
    *      check to see that the following bits are set:
    *         8 (data ready)
    *        13 (Transmission-hold  register empty)
    *        14 (Transmission-shift register empty)
    /* check the status */
    com1_status = _bios_serialcom(_COM_STATUS, 0, 0);
    printf("COM1 status: 0x%4.4X\n", com1_status);
    /* wait until a character is ready */
    do {
        /* check the status */
        com1_status = _bios_serialcom(_COM_STATUS, 0, 0);
        /* mask off the low order byte of com1_status */
        com1_status = com1_status & 0xFF00;
    } while( (mask & com1_status) == 0);
    /* get a character */
    com1_rec =  _bios_serialcom(_COM_RECEIVE, 0, 0);
    printf("Read status: 0x%4.4X\n", com1_rec);
    /* print the character we just received */
    result = com1_rec & 0x00FF;
    printf("Character: 0x%2.2X  =  %c\n", result, (char)result);
These compiled and ran fine with my Borland 5.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.