Microsoft KB Archive/36897

Calling C to Implement COBOL 3.0 Serial Port Communications

PSS ID Number: Q36897 Article last modified on 04-20-1993

3.00 | 3.00 MS-DOS | OS/2

Summary: Accessing the serial ports in COBOL Version 3.0 is not directly supported. One has the choice of calling interrupts from COBOL or calling Microsoft PASCAL or C. The following article provides an example for using Microsoft C to access the communication ports.

More Information: The Microsoft C function used is included in the BIOS library. The function is called _bios_serialcom and allows simple polled I/O from a serial port at multiple baud rates. More information about this function can be found in the Microsoft C Version 5.1 manuals. Another excellent resource on serial communications in C is “C Programmer’s Guide to Serial Communications” by Joe Campbell, published by Howard W. Sams & Company. The following is a code example: The programs further below can be compiled and linked as follows: Compile the C program as follows: cl /c /Awlf com.c Compile the COBOL Version 3.0 program as follows: cobol callc.cob vsc2 rtncode-size(4) litlink; Link them together as follows: link callc+minitc+com,,,lcobol/m/noe; The following is the COBOL Version 3.0 routine: identification division. program-id. com-ports. environment division. configuration section. source-computer. ibm-pc. object-computer. ibm-pc. data division. working-storage section. procedure division. CALL “c_com”. The following is the Microsoft C Version 5.1 routine: #include  #include  #define COM1 0 com { int ch_hit; unsigned service, data, status; data = (_COM_CHR8 |_COM_STOP1 | _COM_NOPARITY | _COM_9600); _bios_serialcom(_COM_INIT,COM1,data); printf(“Connecting to serial port 1. Type ‘q’ to exit ”); while(1) { /* First, see if “DATA READY” flag is set. If yes, read * character from serial port. */ status = 0x100 & _bios_serialcom(_COM_STATUS, COM1, 0); if (status == 0x100) { /* If there is a character, get it and display it */ ch_hit = 0xff & _bios_serialcom(_COM_RECEIVE, COM1, 0); printf(“%c”,ch_hit); } /* Now check if any key has been pressed */ if (_bios_keybrd(_KEYBRD_READY)) { /* If yes, read the keyboard buffer */ ch_hit = _bios_keybrd(_KEYBRD_READ) & 0xff; if((ch_hit == ‘q’) || (ch_hit == ‘Q’)) { /* Exit if it’s a “q” or a “Q” / printf(“Exiting…”); exit(0); } / Or else, wait until “transmit holding register empty” * flag is set. Once it’s set, send out character to * serial port. */ status = 0x2000 & _bios_serialcom(_COM_STATUS, COM1, 0); while (status != 0x2000) { status = 0x2000 & _bios_serialcom(_COM_STATUS, COM1, 0); } printf(“%c”,ch_hit); _bios_serialcom(_COM_SEND, COM1, ch_hit); if ((status & 0x8000) == 0x8000) { printf(“Error sending: %c”, ch_hit); } } } }

Additional reference words: 3.00 Copyright Microsoft Corporation 1993.