Skip to content
systemcoding.dev
Mainframe

Write Your First CICS COBOL Program With SEND TEXT

Write, translate, compile and run a first CICS COBOL program that shows a message on a 3270 screen with EXEC CICS SEND TEXT.

6 min read
Summarize in AI
Flat illustration of a 3270 terminal showing JCL beside a mainframe.

The shortest useful CICS program sends a line of text to a terminal and ends. It teaches the whole development path: write COBOL with EXEC CICS commands, translate the commands into calls, compile, link with the CICS stub, define the program and transaction, and run it from a screen. This post walks that path with Enterprise COBOL for z/OS 6.4 and CICS Transaction Server for z/OS 6.1 on z/OS 3.1. The definition step is covered in more depth in the CEDA post that follows.

The program

HELLOCIC.cblcobol
       IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLOCIC.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-MESSAGE      PIC X(30)
           VALUE 'HELLO FROM CICS COBOL'.
       01  WS-LENGTH       PIC S9(4) COMP VALUE 21.
       PROCEDURE DIVISION.
       MAIN-PARA.
           EXEC CICS SEND TEXT
                FROM(WS-MESSAGE)
                LENGTH(WS-LENGTH)
                ERASE
           END-EXEC.
           EXEC CICS RETURN
           END-EXEC.

Read it in the order CICS runs it. The WORKING-STORAGE SECTION holds the message and its length. Unlike a batch program, there is no STOP RUN and no OPEN for the terminal: CICS owns the terminal. EXEC CICS SEND TEXT writes the data in WS-MESSAGE to the user's screen. FROM names the data area, LENGTH gives the number of bytes to send, and ERASE clears the screen first so the message appears on a clean display. EXEC CICS RETURN ends the task and hands control back to CICS. The length 21 matches the visible text HELLO FROM CICS COBOL, which is 21 characters. The remaining nine bytes of the field are blanks that we do not send.

The run result on the terminal is one line at the top left of a cleared screen.

Text
HELLO FROM CICS COBOL

The cursor sits after the text, and the keyboard is unlocked, so the user can type a new transaction ID.

Why there is no GOBACK

A batch COBOL program ends with GOBACK or STOP RUN. In CICS, STOP RUN is wrong, because it would try to end the run unit that CICS owns. GOBACK works but returns control to CICS without the options RETURN offers, such as naming the next transaction. The convention is EXEC CICS RETURN, and every program in this cluster ends with it.

Translating and compiling

CICS commands are not COBOL, so something has to turn them into statements the compiler understands. Two ways exist. The older way runs a separate translator step, DFHECP1$ for COBOL, which reads your source and writes new COBOL where each EXEC CICS is replaced with a CALL to a CICS stub, plus the generated working storage. The newer way uses the integrated CICS translator inside the COBOL compiler: you give the compiler the CICS option and it handles the commands in one pass. The second is simpler and gives better listings, so we use it.

jcl
//COMPILE EXEC PGM=IGYCRCTL,
//        PARM='LIB,CICS,NODYNAM,RENT,OBJECT'
//STEPLIB  DD DISP=SHR,DSN=IGY.SIGYCOMP
//SYSLIB   DD DISP=SHR,DSN=CICSTS61.CICS.SDFHCOB
//SYSIN    DD DISP=SHR,DSN=USER01.COBOL.SOURCE(HELLOCIC)
//SYSLIN   DD DSN=&&LOADSET,DISP=(MOD,PASS),UNIT=SYSDA,
//         SPACE=(TRK,(3,3))
//SYSPRINT DD SYSOUT=*
//SYSUT1   DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT2   DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT3   DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT4   DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT5   DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT6   DD UNIT=SYSDA,SPACE=(CYL,(1,1))
//SYSUT7   DD UNIT=SYSDA,SPACE=(CYL,(1,1))

IGYCRCTL is the Enterprise COBOL compiler. The CICS option turns on the integrated translator. NODYNAM makes calls static, and RENT makes the program reentrant, which CICS requires. The data set names above are common defaults, and your site may use others, so copy them from a working compile job. The SDFHCOB library holds COBOL copybooks for the CICS interface block. The work files SYSUT1 through SYSUT7 are the compiler's scratch space.

After the compile the object module needs the CICS interface stub, DFHELII, which connects the translated calls to the CICS nucleus.

jcl
//LKED    EXEC PGM=IEWL,PARM='LIST,XREF,RENT'
//SYSLIB  DD DISP=SHR,DSN=CICSTS61.CICS.SDFHLOAD
//        DD DISP=SHR,DSN=CEE.SCEELKED
//SYSLIN  DD DSN=&&LOADSET,DISP=(OLD,DELETE)
//        DD *
  INCLUDE SYSLIB(DFHELII)
  NAME HELLOCIC(R)
/*
//SYSLMOD DD DISP=SHR,DSN=CICSTS61.USER.LOADLIB
//SYSPRINT DD SYSOUT=*

The output goes to a load library that the CICS region has in its DFHRPL concatenation. If the library is not part of DFHRPL, CICS cannot find the program, and the transaction ends with abend APCT, which we cover below.

Defining and running it

CICS needs two definitions. The program definition names HELLOCIC, and the transaction definition ties a four-character ID such as HELO to the program. You create them with CEDA as the next post shows, install the group, and then type the transaction ID on a clear terminal.

Text
CEDA DEFINE PROGRAM(HELLOCIC) GROUP(MYGRP) LANGUAGE(COBOL)
CEDA DEFINE TRANSACTION(HELO) GROUP(MYGRP) PROGRAM(HELLOCIC)
CEDA INSTALL GROUP(MYGRP)

Clear the screen, type HELO, press Enter, and the message appears. Before the first run you may need to make the new load module visible, because CICS keeps a copy of a program in storage after first use. A changed program needs CEMT SET PROGRAM(HELLOCIC) NEWCOPY, covered in its own post.

When it does not work

Three failures are common on a first program. A transaction that answers TRANSACTION 'HELO' NOT FOUND means the install step did not run or the definition is in a group that was not installed. An abend APCT means CICS could not find or load the program, usually because the load library is not in DFHRPL or the program is disabled. A blank screen with a locked keyboard often means the program ended without sending anything, for example when SEND TEXT is missing FROM. Use CEMT INQUIRE PROGRAM(HELLOCIC) to see whether CICS knows the program and how many times it was used, which tells you whether the task ran at all.

Reading the compile listing

Open the SYSPRINT of the compile step first. With the CICS option the listing shows each EXEC CICS statement expanded, and the messages start with DFH for the translator and IGY for the compiler. A return code of 0 or 4 allows the link step. Look for message DFHCO... entries about unknown options, which usually mean a typo in a command such as LENGHT for LENGTH. The translator is strict about spelling and syntax, and its messages name the line, so these errors are quick to fix.

Also check the working storage the translator adds. It inserts the DFHEIBLK block, which is the execute interface block, and the DFHCOMMAREA when you declare one. You never write the EIB yourself. After the first command executes, fields such as EIBTRNID hold the transaction ID and EIBTRMID the terminal ID, and you may read them freely.

Making the program a little more useful

Once the hello program runs, extend it in two small steps. First, show the terminal ID by building a message from EIBTRMID, using a STRING statement into WS-MESSAGE and sending it with the length of the result. Second, move the message text out of the code into a constant at the top, so a later change is a data edit. These steps teach the interface block and the habit of sending exactly the bytes you mean, using a length field that matches the content rather than the whole field.

A length that is too long sends trailing blanks to the screen, which is harmless but untidy. A length that is too short cuts the message. A length of zero is an error and raises the LENGERR condition, which a later post on RESP shows how to test.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.