Heidenhain TNC 640: Klartext & Q-Parameters

The TNC 640’s answer to Fanuc Macro B is Q-parameter programming: variables named Q/QL/QR/QS plus a set of numbered FN functions for assignment, math, jumps, and I/O. Combined with labels, subprograms, and external program calls, this is everything you need to write parametric part families and probing/utility macros in HEIDENHAIN Klartext (conversational) format. This reference is drawn from the TNC 640 Klartext Programming manual (NC software 34059x-11) — cycle and parameter numbers differ between Heidenhain series, so verify against your control’s manual if you run a TNC 620, iTNC 530, or TNC7.

Klartext Program Structure

A Klartext program is a numbered series of NC blocks. The first block is always BEGIN PGM with the program name and unit of measure; the last is END PGM with the same name and unit. Right after BEGIN PGM you normally define the workpiece blank with BLK FORM (needed only for graphic simulation) — rectangular (MIN/MAX corner points), cylindrical, rotationally symmetric, or an STL file. Klartext programs use the extension .H; DIN/ISO programs use .I.

0 BEGIN PGM NEW MM                 ; Program beginning, name, unit of measure
1 BLK FORM 0.1 Z X+0 Y+0 Z-40      ; Spindle axis, MIN point coordinates
2 BLK FORM 0.2 X+100 Y+100 Z+0     ; MAX point coordinates
3 END PGM NEW MM                   ; Program end, name, unit of measure

Labels, Subprograms & Program-Section Repeats

Subprograms and repeats both hang off labels (LBL), set with the LBL SET key. A label carries a number from 1 to 65535 or a name you define; each may be used only once per program. LBL 0 is reserved: it marks the end of a subprogram and may appear as often as needed (CALL LBL 0 is not permitted).

TechniqueSyntaxRules from the manual
SubprogramCALL LBL "UP1"LBL "UP1"LBL 0Write subprograms after the block with M2/M30; a subprogram cannot call itself; call as often as desired; jump target can also be a QS string parameter
Program-section repeatLBL 1CALL LBL 1 REP 2Up to 65 534 repeats; the section always runs one more time than the REP count (first pass + repeats)
External program callCALL PGM TNC:\ZW35\HERE\PGM1.HCalled program must not contain M2/M30 and must not call the caller back; relative paths (..\PGM1.H, DOWN\PGM1.H) allowed; Q parameters remain globally effective across PGM CALL
Deferred callSEL PGMCALL SELECTED PGMSEL PGM accepts string parameters, so program calls can be built dynamically; verify paths first with FN 18 ID10 NR110/NR111

Maximum nesting depth is 19 for subprograms and 19 for external NC programs (a CYCL CALL counts as an external call); program-section repeats can be nested without limit. The PGM CALL key also holds the softkeys SEL TABLE (datum table), SEL PATTERN (point table), SEL CONTOUR, and SEL CYCLE.

Manual example — groups of holes via subprogram

0 BEGIN PGM UP1 MM
3 TOOL CALL 1 Z S5000              ; Tool call
5 CYCL DEF 200 DRILLING ...        ; Cycle definition: drilling
6 L X+15 Y+10 R0 FMAX M3           ; Move to starting point for group 1
7 CALL LBL 1                       ; Call the subprogram for the group
8 L X+45 Y+60 R0 FMAX
9 CALL LBL 1
12 L Z+250 R0 FMAX M2              ; End of main program
13 LBL 1                           ; Beginning of subprogram 1: group of holes
14 CYCL CALL                       ; Hole 1
15 L IX+20 R0 FMAX M99             ; Move to 2nd hole, call cycle
16 L IY+20 R0 FMAX M99             ; Move to 3rd hole, call cycle
17 L IX-20 R0 FMAX M99             ; Move to 4th hole, call cycle
18 LBL 0                           ; End of subprogram 1
19 END PGM UP1 MM

Q-Parameter Types & Ranges

The letter decides the type, the number decides who owns it. Stick to the recommended ranges — HEIDENHAIN cycles, OEM cycles, and your program all share the same Q space, and overlaps cause “reciprocal effects” the manual flags as a collision hazard.

Type / RangeWho it belongs to
Q parameters (numeric, effective in all NC programs in memory)
Q0Q99Free for the user, provided there is no overlap with HEIDENHAIN SL cycles; inside macros/OEM cycles these act locally
Q100Q199Special control functions — read-only for you (preassigned values, probe results)
Q200Q1199Primarily HEIDENHAIN cycles
Q1200Q1399Manufacturer (OEM) cycles that return values to your program
Q1400Q1599Input parameters for manufacturer cycles
Q1600Q1999Free for the user
QL parameters (numeric, local)
QL0QL499Effective only locally within one NC program — the scratch variables for macros
QR parameters (numeric, permanent — survive power-off, included in backups)
QR0QR99Free for the user
QR100QR199HEIDENHAIN functions (e.g. cycles)
QR200QR499Machine tool builder
QS parameters (strings, up to 255 characters, 2000 available)
QS0QS1999Same range ownership pattern as Q: 0–99 user, 100–199 control, 200–1199 HEIDENHAIN cycles, 1200–1599 OEM, 1600–1999 user

Numeric values may range from −999 999 999 to +999 999 999, with 16 digits total of which 9 may precede the decimal point (internally the control calculates up to 1010, IEEE 754 binary — expect round-off on some decimals when comparing for jumps). A parameter can also be reset to the state Undefined with FN 0: Q5 SET UNDEFINED; a positioning block using an undefined Q parameter is simply ignored. Check values anytime with the Q INFO soft key, or pin a watch list in the QPARA status tab.

Assignments & Arithmetic — FN 0 to FN 5, FORMULA

FunctionExampleMeaning
Basic arithmetic (BASIC ARITHM. soft key)
FN 0 ASSIGNFN 0: Q5 = +60Assign a value (or SET UNDEFINED)
FN 1 ADDITIONFN 1: Q1 = -Q2 + -5Sum of two values
FN 2 SUBTRACTIONFN 2: Q1 = +10 - +5Difference
FN 3 MULTIPLICATIONFN 3: Q2 = +3 * +3Product
FN 4 DIVISIONFN 4: Q4 = +8 DIV +Q2Quotient (division by 0 not permitted)
FN 5 SQUARE ROOTFN 5: Q20 = SQRT 4Root (negative operand not permitted)
Trigonometry & geometry (TRIGONOMETRY soft key)
FN 6 SINEFN 6: Q20 = SIN-Q5Sine of an angle in degrees
FN 7 COSINEFN 7: Q21 = COS-Q5Cosine of an angle in degrees
FN 8 ROOT SUM OF SQUARESFN 8: Q10 = +5 LEN +4Hypotenuse length from two values
FN 13 ANGLEFN 13: Q20 = +25 ANG-Q1Angle from arctangent of opposite/adjacent (0–360°)
Circle calculations
FN 23 CIRCLE from 3 pointsFN 23: Q20 = CDATA Q30Point pairs in Q30–Q35 → center in Q20/Q21, radius in Q22
FN 24 CIRCLE from 4 pointsFN 24: Q20 = CDATA Q30Point pairs in Q30–Q37 → same results, higher accuracy

The FORMULA soft key lets you skip the FN numbers entirely and write infix math in one block: operators + - * / ( ) ^ and functions SQ, SQRT, SIN, COS, TAN, ASIN, ACOS, ATAN, LN, LOG, EXP, NEG, INT, ABS, FRAC, SGN, % (modulo), and the constant PI. Normal precedence applies (parentheses, then functions, powers, multiply/divide, add/subtract); chained powers evaluate right-to-left. Beware: INT truncates — it does not round. To round, add 0.5 before truncating.

12 Q1 = 5 * 3 + 2 * 10             ; = 35
13 Q2 = SQ 10 - 3^3                ; = 73
37 Q25 = ATAN (Q12/Q13)            ; angle from opposite/adjacent sides
14 Q4 = SIN 30 ^ 2                 ; = 0.25 (function before power)

Jumps & If/Then Decisions — FN 9 to FN 12

A condition compares a Q parameter with another parameter or a value; if true, execution continues at the given label (by number, name, or QS parameter). If false, the next block runs. Unlike subprograms, jumps need no LBL 0 terminator — and they ignore return-jump labels entirely.

FunctionExampleJumps when
FN 9 IF EQUALFN 9: IF +Q1 EQU +Q3 GOTO LBL "UPCAN25"Values are equal
FN 9 IF UNDEFINEDFN 9: IF +Q1 IS UNDEFINED GOTO LBL "UPCAN25"Parameter has no value
FN 9 IF DEFINEDFN 9: IF +Q1 IS DEFINED GOTO LBL "UPCAN25"Parameter has a value
FN 10 IF UNEQUALFN 10: IF +10 NE -Q5 GOTO LBL 10Values differ
FN 11 IF GREATERFN 11: IF +Q1 GT +10 GOTO LBL QS5First > second
FN 12 IF LESSFN 12: IF +Q5 LT +0 GOTO LBL "ANYNAME"First < second

An unconditional jump is just a condition that is always true: FN 9: IF+10 EQU+10 GOTO LBL1. The manual’s counter idiom — loop a machining operation N times with a Q parameter as counter:

0 BEGIN PGM COUNTER MM
2 Q1 = 0                                 ; Initialize counter
3 Q2 = 3                                 ; Number of passes
5 LBL 99
6 Q1 = Q1 + 1                            ; New Q1 = old Q1 + 1
7 FN 12: IF +Q1 LT +Q2 GOTO LBL 99       ; Runs passes 1 and 2
8 FN 9: IF +Q1 EQU +Q2 GOTO LBL 99       ; Runs pass 3
10 END PGM COUNTER MM

String Processing — QS Parameters

QS parameters hold up to 255 characters and feed variable text into logs (FN 16), dynamic program calls (SEL PGM), and label targets. Assign with DECLARE STRING; process with the STRING FORMULA and FORMULA function groups:

FunctionExampleResult
AssignDECLARE STRING QS10 = "Workpiece"QS10 holds the text
ConcatenateQS10 = QS12 || QS13 || QS14Chained string
TOCHARQS11 = TOCHAR ( DAT+Q50 DECIMALS3 )Number → string with 3 decimals
SUBSTRQS13 = SUBSTR ( SRC_QS10 BEG2 LEN4 )4 chars from position 2 (first char = position 0)
SYSSTRQS5 = SYSSTR ( ID10010 NR1 )System data as string (here: path of current main program)
TONUMBQ82 = TONUMB ( SRC_QS11 )String → number (must be purely numeric)
INSTRQ50 = INSTR ( SRC_QS10 SEA_QS13 BEG2 )Position of first match; full length+1 if not found
STRLENQ52 = STRLEN ( SRC_QS15 )Text length (−1 if undefined)
STRCOMPQ52 = STRCOMP ( SRC_QS12 SEA_QS14 )0 identical, −1/+1 alphabetic order
CFGREADQ50 = CFGREAD( KEY_QS11 TAG_QS12 ATR_QS13 )Read a machine parameter (key/entity/attribute defined in QS parameters first)

FN 14: ERROR — and the Other Additional Functions

FN 14: ERROR raises an error message under program control and interrupts the program — the Klartext equivalent of Fanuc’s #3000 alarm. Numbers 0–999 call machine-builder dialogs; 1000–1199 are HEIDENHAIN-predefined messages (1000 = “Spindle?”, 1001 = “Tool axis is missing”, 1010 = “Feed rate is missing”, … up to 1191). Example from the manual — complain if the spindle is not on:

180 FN 14: ERROR = 1000            ; Displays "Spindle?" and stops the program

The rest of the DIVERSE FUNCTION menu, for completeness: FN 16: F-PRINT (formatted output), FN 18: SYSREAD (read system data), FN 19/FN 29: PLC (transfer two / eight values to the PLC — OEM territory), FN 20: WAIT FOR (NC/PLC sync; FN 20: WAIT FOR SYNC pauses look-ahead before real-time reads), FN 26/27/28 (freely definable tables), FN 37: EXPORT (return local QL/QS values from your own cycles), and FN 38: SEND (push text and up to seven variable values to the log or to StateMonitor over TCP/IP). FN 16, FN 18, and FN 26–28 are covered in depth in the Tables & System Data article.

Preassigned Q Parameters (Q100–Q199)

The control fills Q100–Q199 automatically — never write to them. Q108 and Q114–Q117 follow the unit of measure of the active program.

ParameterContents
Q100Q107Values handed over from the PLC
Q108Active tool radius (R + DR from table + DR from TOOL CALL / compensation table)
Q109Tool axis: −1 none, 0 X, 1 Y, 2 Z, 6 U, 7 V, 8 W
Q110Spindle state: −1 undefined, 0 M3, 1 M4, 2 M5 after M3, 3 M5 after M4
Q111Coolant: 1 = M8 on, 0 = M9 off
Q112Overlap factor for pocket milling
Q113Unit of the main program in nesting: 0 = mm, 1 = inch
Q114Current tool length
Q115Q119Coordinates of the spindle position at the probing trigger (X, Y, Z, 4th, 5th axis) — stylus length/radius not compensated
Q115/Q116Alternate meaning after automatic tool measurement (e.g. TT 160): length / radius deviation actual-nominal
Q120Q122Rotary-axis coordinates calculated by the control when tilting with workpiece angles (A, B, C)
Q150Q199Touch-probe cycle results — see the probing cycles article for the full table

Worked Example from the Manual: Ellipse Macro

The classic Q-parameter demonstration — approximate an ellipse with short line segments, all geometry in parameters, machining inside a subprogram, loop closed with FN 12. Set the twelve header parameters and the same program cuts any ellipse:

0 BEGIN PGM ELLIPSE MM
1 FN 0: Q1 = +50                   ; Center in X axis
2 FN 0: Q2 = +50                   ; Center in Y axis
3 FN 0: Q3 = +50                   ; Semiaxis in X
4 FN 0: Q4 = +30                   ; Semiaxis in Y
5 FN 0: Q5 = +0                    ; Starting angle in the plane
6 FN 0: Q6 = +360                  ; End angle in the plane
7 FN 0: Q7 = +40                   ; Number of calculation steps
8 FN 0: Q8 = +0                    ; Rotational position of the ellipse
9 FN 0: Q9 = +5                    ; Milling depth
10 FN 0: Q10 = +100                ; Feed rate for plunging
11 FN 0: Q11 = +350                ; Feed rate for milling
12 FN 0: Q12 = +2                  ; Set-up clearance for pre-positioning
13 BLK FORM 0.1 Z X+0 Y+0 Z-20
14 BLK FORM 0.2 X+100 Y+100 Z+0
15 TOOL CALL 1 Z S4000
16 L Z+250 R0 FMAX
17 CALL LBL 10                     ; Call machining operation
18 L Z+100 R0 FMAX M2
19 LBL 10                          ; Subprogram 10: machining operation
20 CYCL DEF 7.0 DATUM SHIFT        ; Shift datum to center of ellipse
21 CYCL DEF 7.1 X+Q1
22 CYCL DEF 7.2 Y+Q2
23 CYCL DEF 10.0 ROTATION          ; Account for rotational position
24 CYCL DEF 10.1 ROT+Q8
25 Q35 = (Q6 - Q5) / Q7            ; Calculate angle increment
26 Q36 = Q5                        ; Copy starting angle
27 Q37 = 0                         ; Set counter
28 Q21 = Q3 * COS Q36              ; X coordinate of starting point
29 Q22 = Q4 * SIN Q36              ; Y coordinate of starting point
30 L X+Q21 Y+Q22 R0 FMAX M3        ; Move to starting point in the plane
31 L Z+Q12 R0 FMAX                 ; Pre-position to set-up clearance
32 L Z-Q9 R0 FQ10                  ; Move to working depth
33 LBL 1
34 Q36 = Q36 + Q35                 ; Update the angle
35 Q37 = Q37 + 1                   ; Update the counter
36 Q21 = Q3 * COS Q36              ; Current X coordinate
37 Q22 = Q4 * SIN Q36              ; Current Y coordinate
38 L X+Q21 Y+Q22 R0 FQ11           ; Move to next point
39 FN 12: IF +Q37 LT +Q7 GOTO LBL 1   ; Not finished? Loop
40 CYCL DEF 10.0 ROTATION          ; Reset the rotation
41 CYCL DEF 10.1 ROT+0
42 CYCL DEF 7.0 DATUM SHIFT        ; Reset the datum shift
43 CYCL DEF 7.1 X+0
44 CYCL DEF 7.2 Y+0
45 L Z+Q12 R0 FMAX                 ; Move to set-up clearance
46 LBL 0
47 END PGM ELLIPSE MM

DIN/ISO Equivalents: the D Functions

In the TNC 640’s ISO dialect the same machinery exists with D numbers instead of FN, and positional operands P01/P02/P03 instead of dialog prompts. Programs open with %NAME G71 (mm) and close with N99999999 %NAME G71; labels are set with G98 L1 and a subprogram is called with L1,0. The mapping is one-to-one: D00–D05 = FN 0–5, D09–D12 = FN 9–12, D14 = FN 14, D16 = FN 16, D18 = FN 18, D19/D29 = FN 19/29, D20 = FN 20, D26/D27/D28 = FN 26/27/28, D37 = FN 37, D38 = FN 38.

; Klartext                            ; DIN/ISO equivalent
16 FN 0: Q5 = +10                     N16 D00 Q5 P01 +10*
17 FN 3: Q12 = +Q5 * +7               N17 D03 Q12 P01 +Q5 P02 +7*
FN 12: IF +Q5 LT +0 GOTO LBL "ANYNAME"   D12 P01 +Q5 P02 +0 P03 "ANYNAME"*

References

  • HEIDENHAIN, TNC 640 Klartext Programming User’s Manual, NC software 340590-11/340591-11/340595-11, 01/2021, ID 892903-29.
  • HEIDENHAIN, TNC 640 ISO Programming User’s Manual, NC software 34059x-11, 01/2021, ID 892909-29.

Have a question or want to contribute?

Contact us with corrections, additions, or topics you'd like covered.

Get in Touch