![]() |
Suburban Machinery Software Division 37777 Harlow Dr., Willoughby, OH 44094 (440) 951-8974 FAX (440) 951-1412 |
Use Your Personal Computer For Direct Numerical Control
(as published in Modern Machine Shop magazine, Sept 86)
By Dan Fritz
President, Suburban Machinery, Software Div.
Two simple software programs in the BASIC language and easy-to-follow instructions make it possible to link a personal computer to a computer numerical control unit for sending and receiving NC programs from a disk.
If you have CNC (Computer Numerical Control) machine tools in your shop, and also own a personal computer, you've probably thought about linking the two to create a kind of direct numerical control (DNC) system. Most personal computers and most CNC units have standard communications ports, so the possibility obviously seems tempting and feasible. Linking the personal computer with a CNC would allow NC programs to be sent and received as disk files. For one thing, this arrangement would eliminate the need for punched tape. For another, it makes file management easier because many short programs or a few very long ones can be stored on a single disk, regardless of the size of memory in the CNC. Even if you don't have a personal computer or CNC machine tools, you can learn a lot about this technology by looking at these step-by-step procedures. Becoming familiar with the principles and the terminology will also be helpful in a world saturated with computer words and concepts. Linking a personal computer with a CNC machine tool happens very much like linking a personal computer to a computer printer.
The printer receives a file through a cable from the personal computer and executes the commands to print letters and symbols in a certain order on the paper. Of course, the user must have word processing software to create the document that the printer types out, and likewise, in the machine shop, the user must have the software to create the NC program for the machine tool.
Most modern CNC controls and Personal Computers are available with RS-232 serial communications ports. This type of port transmits one "bit" at a time in a serial fashion (sort of an electronic Morse Code). To transmit a single character, such as the letter "X", the port must send 8 bits of data, each bit corresponding to a single hole position on an 8 channel wide paper tape. Each bit is sent by placing a positive or negative voltage on a single wire. An extra bit called a "start" bit marks the beginning of each 8 bit character, and another "stop" bit markes the end of each character. Consequently, 10 bits are required for each character in the data stream. The bits are transmitted at a designated frequency, or baudrate, which must be the same for both the sending and receiving devices. A baudrate of 300 means that the ports are communicating at 300 bits persecond, or 30 characters per second (10 bits = 1 character). Some devices use 2 stop bits instead of 1, so it takes 11 bits to send a single character. If the sending device uses 2 stop bits, the receiving device must also use 2 bits. NC programs can be created in a variety of ways. Most programming assist software for the IBM PC and PC compatibles will store finished NC files on disk in ASCII (American Standard Code for Information Interchange). These files are usually compatible with the BASIC programs in this article. Programs created at the CNC controller using the MDI (Manual Data Input) mode can be stored on the PC if the CNC is able to send the program through an RS-232 port in ASCII or ISO (a subset of ASCII). Virtually all CAD/CAM systems are able to send finished (post processed) NC programs to another device with an RS-232 port. If your CAD/CAM system is able to punch tapes with an RS-232 tape puncher, it will also be able to send files to a PC.
Some CNCs use a special protocol for data transmission. The Mazatrol CNC used by Mazak is a good example. Programs created on the Mazatrol with the CNCs conversational programming feature cannot be sent to the PC as a text file since the CNC's software is designed to communicate with a specially made storage device. Special software is commercially available to to communicate with these CNCs, but describing such a protocol goes beyond the scope of this article.
Paper tapes punched in ASCII or ISO can be read into the PC using an RS-232 tape punch/reader. Tapes punched in EIA code (such as those made on a Flexowriter) cannot be read by the software provided in this article. EIA code is alien to the PC, and requires an additional processing step in the software to convert each character into ASCII before it can be displayed or edited on the PC. Perhaps we can discuss this problem in a future article.
Most word processing software includes the routines for sending files to a printer. The user must usually specify the kind of printer that will be used so that the routines tailored for that printer are provided. Unfortunately, software for transferring files to and from personal computers and CNC units is not so readily available. The user has to create the programs himself, but that is not as difficult as it may sound.
Programming
Programming computers is regarded by many to be something of a black art. Many programming languages such as Assembler, Pascal, and "C" are cryptic, requiring time and patience to learn. BASIC, on the other hand, is powerful and quite easy to use. The real advantage to the BASIC language is that most anyone can write useful programs to solve problems for which no "off-the-shelf" software exists.
BASIC is often the language provided by the builder of the personal computer. Detailed explanations and directions for its use are often included with the documentation and user's manual that come with the personal computer.
Included in this article are a pair of BASIC programs for the IBM PC and compatible models. These programs are designed to send and receive disk files using the personal computer's RS-232 serial port. Also included are some cable wiring diagrams and parameter settings for the popular Fanuc and General Numeric CNC controls. These CNC units are not only widely used, but are also good examples of the differences in settings that may be encountered among different control models. Similar RS-232 equipped controls such as those made by GE, Allen-Bradley and Yasnac can be connected to the personal computer with little or no modification to the programs.
Caution: The PC's RS-232 port, which is usually a 25 pin MALE connector located on the rear of the computer housing, can easily be confused with the computer's parallel (printer) port, which is usually a FEMALE plug. If you are in doubt about your PC's output ports, contact your computer dealer for confirmation before you connect any devices to your PC.
To complete an RS-232 data link between a CNC unit and a personal computer, three key elements must be in place. First, the software in the computer must be able to access disk memory and control the computer's RS-232 port. Second, the CNC's RS-232 port must be configured to match the transmission speed (baudrate) of the computer. (If you are not sure that your CNC has an RS-232 port or do not know where it is located, refer to the unit's user manual or contact the manufacturer.) And third, the cable connecting the CNC with the computer must be properly wired. Each of these items will be addressed in order.
The Software
These programs are written for the IBM PC using IBM's BASICA (Advanced BASIC) language. They will also run on most IBM-compatible personal computers using Microsoft's GWBASIC, which is also widely used. They can also be used in conjunction with Microsoft's QuickBasic compilers
First, load BASICA (or GWBASIC), then type in the following code. Be very careful to copy these lines exactly, observing every letter, number, symbol, space, and punctuation mark.
10 SCREEN 0:CLS:KEY OFF
20 DEFINT A-Z
30 PORT$="COM1"
40 BAUD$="300"
50 MCR=&H3FC
60 IF RIGHT$(PORT$,1)="2" THEN MCR=&H2FC
70 FILES:INPUT "Enter file name ",F$
80 IF F$="" THEN 480
90 ON ERROR GOTO 160
100 OPEN F$ FOR INPUT AS #1
110 CLOSE #1
120 PRINT "Caution! File already exists"
130 INPUT "Continue anyway (Y/N) ";A$
140 IF A$="y" OR A$="Y" THEN 170
150 GOTO 10
160 IF ERR=53 THEN RESUME 170
170 ON ERROR GOTO 0
180 OPEN F$ FOR OUTPUT AS #1
190 OPEN PORT$+":"+BAUD$+",E,7,1,CS,DS" AS #2
200 PRINT "Ready to receive data ..."
210 B$=INKEY$:IF B$<>"" THEN 210
220 ON ERROR GOTO 230:GOTO 240
230 RESUME 240
240 B$=INKEY$:IF B$=CHR$(27) THEN 470
250 IF B$<>"" THEN PRINT #2,B$;
260 IF EOF(2) THEN 240
270 C$=INPUT$(1,#2):C=ASC(C$)
280 IF C=18 THEN PRINT #2,CHR$(17);
290 IF C<>10 AND C<>13 THEN 240
300 ON ERROR GOTO 0
310 B$=INKEY$
320 IF B$=CHR$(27) OR C=37 THEN 470
330 IF EOF(2) THEN 310
340 IF LOC(2)>128 THEN OUT MCR,INP(MCR) AND &HFC
350 IF LOC(2)<16 THEN OUT MCR,INP(MCR) OR &H3
360 C$=INPUT$(1,#2):C=ASC(C$)
370 IF C=10 OR C=13 THEN 400
380 L$=L$+C$
390 PRINT C$;:GOTO 310
400 IF L$="" THEN 310
410 OUT MCR,INP(MCR) AND &HFC
420 PRINT #1,L$
430 PRINT
440 OUT MCR,INP(MCR) OR &H3
450 L$=""
460 GOTO 310
470 PRINT #1,"%"
480 CLOSE:END
When finished, save this program on disk by typing: SAVE "TODISK" and then hit the `enter' key.The next program will send disk files back to your CNC unit or tape/punch reader. After the f first program has been safely stored on disk, type in the command: NEW and press the `enter' key. This will clear the personal computer's program memory so you can begin entering the second program.
10 SCREEN 0:CLS:KEY OFF
20 DEFINT A-Z
30 PORT$="COM1"
40 BAUD$="300"
50 MSR=&H3FE
60 IF RIGHT$(PORT$,1)="2" THEN MSR=&H2FE
70 FILES:INPUT "Enter file name ",F$
80 IF F$="" THEN 420
90 ON ERROR GOTO 120
100 OPEN F$ FOR INPUT AS #1
110 GOTO 160
120 IF ERR=53 THEN RESUME 130
130 ON ERROR GOTO 0
140 PRINT "File not found"
150 GOTO 70
160 OPEN PORT$+":"+BAUD$+",E,7,1,CS,DS" AS #2
170 PRINT "Ready to send data ..."
180 PRINT "Press ENTER key to begin"
190 B$=INKEY$:IF B$<>"" THEN 190
200 B$=INKEY$
210 IF EOF(2) THEN 250
220 D$=INPUT$(1,#2):D=ASC(D$) AND &H7F
230 IF D=18 THEN PRINT #2,CHR$(17);
240 IF D=17 THEN 270
250 IF B$=CHR$(27) THEN 410
260 IF B$<>CHR$(13) THEN PRINT #2,B$;:GOTO 200
270 H=INP(MSR) AND &HCF
280 PRINT #2,CHR$(18)CHR$(13)CHR$(10)
290 IF EOF(1) THEN 410
300 B$=INKEY$:IF B$=CHR$(27) THEN 410
310 IF EOF(2) THEN 350
320 D$=INPUT$(1,#2):D=ASC(D$) AND &H7F
330 IF D=19 THEN P1=1
340 IF D=17 THEN P1=0
350 P2=INP(MSR) AND &HCF
360 IF P2<H OR P1=1 THEN 300
370 C$=INPUT$(1,#1):C=ASC(C$) AND &H7F
380 PRINT #2,CHR$(C);
390 IF C<>10 THEN PRINT CHR$(C);
400 GOTO 290
410 PRINT #2,STRING$(5,0)CHR$(4)CHR$(20);
420 CLOSE:END
When finished, save this program by typing: SAVE "FROMDISK" and then hit the `enter' key.
As you can see, the port number (COM1 or COM2) and the transmission speed (baudrate) are defined near the beginning of each program. If you prefer to use your personal computer's COM2 port, or a baudrate other than 300 bits per second, you can edit these lines without affecting the rest of either program.
Programs running in interpreter BASIC are easy to create and edit, but they do not run very fast. When sending to another device, the FROMDISK program can transmit about 30 characters per second. This speed will result in a steady stream of data at 300 baud, or intermittent "bursts" of data at a higher baudrate. Baud rates higher than 300 will not result in faster program transfers.
The TODISK program can only receive characters at about 30 characters per second. If the sending device sends characters at a baudrate greater than 300, the TODISK program will have to force the sending device to regulate its transmission speed with the "handshaking" signal CTS (pin 5) or DTR (pin 20).
These programs can run up to ten times faster if they are compiled into machine code with a BASIC compiler program. Compilers turn BASIC code into an "executable" program that can be run directly from the DOS "A>" prompt. A compiled program runs much faster, but can no longer be edited. Compiled versions of FROMDISK and TODISK set to operate at 2400 baud can be obtained by sending $5.00 or $1.00 and a formatted 5 1/4 inch diskette to Suburban Machinery, Software Division, 37777 Harlow Drive, Willoughby, OH 44094.
Setting the CNC
Most CNC units use several "parameter" registers to configure the CNC's RS-232 port. Changing the parameters is usually done by entering new data at the CNC keyboard in the manual data input mode. For more information about how to change the parameter settings, refer to your CNC's user manual. It is very important that the baudrate setting of the CNC unit matches the baudrate used in line 40 of the FROMDISK and TODISK programs.
The four most common types of Fanuc CNCs are:
1) System 3 series (3T, 3M, 3TF, etc.)
2) System 6 series (6T, 6M)
3) System 6-B series (6TB, 6TF, 6MB, 6MF, etc.)
4) System 10/11/12/15 series (all variations)
5) System 0 (0TA, 0TB, 0TC, 0MA, 0MB, and 0MC)
Each series has a different set of parameters for the RS-232 port.
System 3 series:
a) Set baudrate with parameter #68 and #69 (set both to 300)
b) Set parameters 5 and 14 to: 1xxxx0x0 (bits marked "x" are
used for something else - do not change).
c) Other SETTING values: Punch Code = ISO, I/O device = 0
System 6A series (6T,6M - older models):
a) Set baudrate with parameter #25 (10001100 = 300 baud)
b) Set parameter #24 to: xxxxxxx0 (don't change bits with "x")
c) Set parameter #26 to: 0xxxxxxx " " " "
d) Other SETTING values: Punch code = ISO, INPUT DEVICE 2 = 1
System 6B series (6TB, 6MB, and so on):
a) Set baudrate with parameter #311 (11000101 = 300 baud)
b) Set parameters 340 and 341 to: 2
c) Other SETTING values: Punch code = ISO, INPUT DEVICE 2 = 1
System 10/11/12/15 series:
a) Set parameters 5110, 5120 and 5130 to : 3 (I/O type)
b) Set parameters 5111, 5121 and 5131 to : 1 (stop-bits)
c) Set parameters 5112, 5122 and 5132 to : 6 (300 baud)
d) Set parameter 0000 to: xxx01010 (disregard bits marked "x")
e) Other SETTING values: PUNCH CODE = ISO, INPUT DEVICE =1,
OUTPUT DEVICE = 1
System 0 series
a) Set parameters 0002 and 0012 to: 1xxxx0x0
b) Set parameters 553 and 554 to: 6 (300 baud)
c) Other SETTING values: PUNCH CODE = ISO, I/O = 1
Making the cable
Most CNC units, tape punch/readers and computers use the standard DTE (Data Terminal Equipment) pin assignments on the RS-232 plug. When connecting two DTE devices together (such as hooking the personal computer to a Fanuc control unit) you must "cross-connect" some wires as shown in Figure 1. When connecting the personal computer to a DSI tape punch/reader, use the MODEM plug on the back of the tape punch/reader. Use only high quality shielded cable with 22 or 24 gage stranded wires.
Some personal computers will have a 9-pin male plug intead of a standard 25-pin plug for the RS-232 serial port. If your computer has a 9-pin plug, use use a 9 to 25 pin adapter, sometimes called an "AT serial port adapter". These adapters are readily available for about $5 at Radio Shack, and are used with the cable described below.
male for Fanuc CNC side --------------------------------- female for PC side
2 *--------------------------* 3
3 *--------------------------* 2
4 *--------------------------* 5
5 *--------------------------* 4
7 *--------------------------* 7
6*------ Tie pins 6, 8, and 20 together on CNC side
8*------
20*----
Operating the system
After all the preliminary work has been done, the system is finally ready to be tested. Keep in mind that software only works if it is entered EXACTLY as printed here. Even a small typographical mistake can cause an error to occur while running the program. If you discover an error, edit the BASIC code and re-store the newly edited version of the program on the disk.
To store files on the personal computer, load BASICA (or GWBASIC), and type: RUN "TODISK" (enter).The TODISK program will prompt you to enter a file name for the NC program. When TODISK is ready to receive characters, select a program on the CNC and begin transmitting. The actual keystrokes required to transmit will depend on the model CNC. Characters will be displayed on the personal computer's screen as they are received. If characters come in too fast for the personal computer to process them, TODISK will turn off the signals RTS (pin #4) and DTR (pin #20) until it is ready to proceed.
To send a file to the CNC, load BASICA (or GWBASIC) and type: RUN "FROMDISK" (enter), then enter the name of the disk file that you want to send to the CNC. Next, prepare the CNC to receive data (again, this procedure will vary depending on the CNC). Some CNCs will transmit a special DC1 (Xon) control character when it is ready to receive data. If this is the case, the FROMDISK program will begin sending your file immediately. If not, /you will have to press the `enter' key on the PC to begin the file transfer.
If FROMDISK sends characters too fast for the receiving device to process them, it can be made to stop transmission by turning off the signal at pin 5 (CTS) or pin 6 (DSR), or by sending in a special "Xoff" control character. FROMDISK will resume transmission when pins 5 and 6 are both back on, or if the receiving device sends an "Xon" control character. These "handshaking" techniques will work with most of the CNC units and tape punch/readers on the market.
Problems
If your personal computer and CNC machine tool are compatible with those mentioned in this article, and if you have followed the instructions carefully, your DNC link should be successful. If not, double check the procedure to be sure you have not missed a step. If this review does not reveal the problem, contact the supplier of the CNC or of the personal computer. They may be able to help. Here is a list of a few typical problems and their possible solutions.
1) If no data is transferred in either direction and a "Device I/O error occurs on the PC when using TODISK, you probably have a baudrate or "stop" bits mis-match between devices. Confirm that your CNC or punch reader is using the baudrate you expect.
2) If no data is transferred in either direction and no errors occur, you may have an improperly wired cable. If your CNC or puncher uses a DCE port instead of the usual DTE port, you must use a "straight through" cable instead of the cross-wired type described above.
3) If a "Communications Buffer Overflow" error occurs on the PC, your CNC is probably ignoring the handshaking signals CTS and DSR (pins 5 and 6) and is sending data when TODISK is not ready. When loading BASICA use the command : BASICA /C:1024 (enter) This will increase the COM buffer size to 1 k-byte.
4) Some tape punch/readers (Blue Chip, for example) must use 2 "stop" bits for all communications. Also, most devices that operate at 110 baud use 2 "stop" bits. This can easily be changed by editing line 190 in TODISK and 170 in FROMDISK to read:
OPEN PORT$+":"+BAUD$+",E,7,2,CS,DS" AS #2
5) Some controls (early model Bridgeports) use ASCII code without a "parity" bit (channel #8 on a paper tape). To use ASCII without parity, edit line 190 in TODISK and 170 in FROMDISK to :
OPEN PORT$+":"+BAUD$+",N,1,CS,DS" AS #2
6) A very few Fanuc CNCs were not able to send programs in ISO code even though the setting parameter labeled "PUNCH CODE" is set for ISO (EIA/ISO switching is an optional feature). This option can be field-installed by your machine tool builder or a Fanuc engineer at a reasonable cost.
If you have specific questions about the TODISK and FROMDISK programs, send a brief letter describing your difficulty to the address given above. Every effort to provide assistance will be made as time permits.