Communications Routes

From Mach3Wiki
Jump to: navigation, search


Overview of chapter

In this chapter we will look how the hardware of the machine tool and its user controls, the computer hardware and Mach3 communicate with each other. You will probably find it best to skim this chapter and return to it as you look at the other individual customisations.

Before we can look at customisation in detail you will have to know how the various parts of your machine communicate with Mach3 and, indeed, how you communicate with it.


The illustration shows the important communication routes and with the following description should allow you to understand how they work. The diagram looks complex and this is a fair reflection of the fact that Mach3 is complex and can be configured to work with many types of machine. Your system will, almost certainly, not have all the features on it but, before you start customising, now is a good time to understand the options. Notice the key which explains what the different type of lines represent.

Electrical connections

If you have set up your own system you will have been through the process of connecting switches like the home switches to pins of the parallel port(s) and using the dialogs to tell Mach3 which pins are used for which function. This is shown at the bottom left of the picture. Notice we have shown the limit and EStop switches directly controlling the drive electronics because this is the safest technique and recommended in this manual. Your switches might, of course, control Mach2 via the parallel port(s). This would change the arrows from the switches on the diagram to go into the port(s) instead of the motor drives box.

Mach3 makes extensive use of these signals during running. This is shown by the arrow into the "brain".

Some switches (currently 15) can be assigned to input pins as OEM Trigger signals. These can be configured to do what you wish by issuing an "OEM code" command directly to Mach3 - just like pressing a screen button does. They do not, however, need anything on a screen to do this. The OEM Button codes that are issued by each signal are defined using the Config>Set System Hotkeys dialog.

Keystroke connections


A "keystroke" produces a scancode that is sent to your computer using the PS/2 or USB connectors. They can be generated in three different ways:

  • By typing on the computer keyboard
  • By closing/opening a switch connected to a keyboard emulator. Such devices may be connected via the Universal Serial Bus (USB) and/or be daisy-chained to the main keyboard. They were originally developed by the gaming community to allow computer games to put in and be controlled by arcade-like cabinets but are very convenient for machine control applications. It is also possible to connect an encoder (typically low resolution like those used as "digital potentiometers" in hi-fi and video equipment) and to generate a given scancode for each "click" in the clockwise direction and another scancode for each counterclockwise click.
  • By pressing a button or key on a custom keyboard (perhaps like a numeric pad on a long lead, buttons on a joystick, or a "console"). These options are really just some switches and an emulator in a standard box, probably with some neat way of labelling the switches. Windows refers to them as Human Interface Devices (HIDs) Figure 2.2 shows an example with a Mach3 function overlay sheet in place.

A keystroke when Mach3 is running can, of course, do many things: be part of a command entered in the MDI line, be part of a value entered into a DRO or be a hotkey shortcut. A real keyboard will be used for all these things but the other sources of keystrokes will generally only be used for shortcuts.

Keystrokes and Shortcuts (Hotkeys)

A keystroke generates a Scancode which is processed by Windows and then Mach3. If it is not used directly as input to MDI or a DRO then it can perform three functions:

  • if the code defined as a Button Shortcut (Hotkey) for a button currently displayed on screen then it is equivalent to clicking the mouse on that button
  • if the code defined in the Config>Set Axis Hotkeys it will jog, load a file or select MDI or a DRO as appropriate
  • if the code defined in the Config>Ports and Pins Input Pins dialog it will turn On (key down) or Off (key up) the signal to which it is attached. This, used with a keyboard emulator, gives a mechanism for extending the number of input pins far beyond those available on the two parallel ports. Not all signals can be made "virtual" like this and you are advised not to use the mechanism for safety critical operations.

You may consider using having real push buttons (or perhaps a membrane switch pad) beside the screen which can be labelled for different functions on each screen (making soft keys like in a bank ATM) or be used for a common function like screen changing. This is illustrated top right of the main picture.

A rotary encoder (MPG) connected by keyboard emulator can be used to send jog axis hotkeys to jog axes or to send the codes for hotkeys for buttons like Raise/Lower spindle speed or override feedrate.

This keystroke processing mechanism is shown by the blue lines on the main picture from the "keyboard devices" to the screen buttons and the Input Pins dialog.

A USB custom keyboard


The KeyGrabber and profilers

As mentioned above, keystokes from the actual keyboard and keyboard emulators have their scancodes processed by Windows before they are given to Mach3. In particular, of course, Windows has to route the keycodes to the application whose window has the focus. Custom keyboards, game pads etc. (HIDs) generally need a piece of software to define which keycodes each button is to generate. This software is often called a profiler and is supplied by the custom keyboard manufacturer. Profilers usually have a picture of the device and its buttons and when you click on these you are prompted to input the keycode and any <Shift>, <Ctrl> or <Alt> modifiers to be used with it.

A utility, KeyGrabber, has been developed, by Les Newell, for use in conjunction with Mach3 (and its Screen Designer) to make it easy to configure Keyboard Emulators and USB connected HIDs including custom buttons, joy sticks, throttle controls etc. It has the additional advantage that it will pass selected keycodes to Mach3 whatever program has the focus. This Mach3 can continue to be controlled even if you need to briefly run another application like Windows Explorer, the Calculator or you accidentally swap to a program by clicking in the task bar. Although at first sight translation of a few key codes sounds rather trivial, if you look into it we think you will discover how powerful a technique it is for making Mach3 easy to use with a variety of standard and therefore reasonably priced input devices. Details of KeyGrabber and its installation and configuration are given at Keygrabber info.

VB Script connections

A part program is written using G and M codes. It is, however, also possible to program Mach3 in another language called VB Script. This is a subset of the Microsoft Visual Basic language which is available in many applications besides Mach2, including Microsoft Office. While it would be possible to totally control a machine tool using Mach3 with a VB Script program, the facility is really intended to help customize the environment in which a G-code part program runs.

VB Script programs

A VB Script program can be used in three ways:

  • By being "attached" to a screen button and so be run when the button is clicked or its hotkey is pressed
  • By being put in a macro and called from within a part program, the MDI line of the G-code attached to a button. The last of these methods being suitable if the program is too big to attach directly to the button or is required to be used as a macro from a part program as well as a button.
  • By being a macro attached to an OEM input trigger by OEM code 301.

The VB Script program can:

  • declare and use VB variables,
  • execute conditional statements (If-Then-Else),
  • perform loops, and
  • call VB the functions and subroutines defined by the macro writer or provided by Mach3 as its interface to scripts.

The predefined interface functions and subroutines are defined in detail in Mach specific Subroutines/Functions grouped by purpose. The main ones allow the script to issue G-code commands, to read and write the values of DROs, to inspect LEDs (and for User LEDs, to set them), and to send Function and OEM codes to Mach3 thus simulating what a user does when s/he clicks a screen button. Additionally a macro can test the values of input signals, set output signals, access internal data such as motor tuning parameters and use parallel and serial ports not used by Mach3 itself.

Mach3 macros

As already mentioned, a macro is a piece of VB Script. Each macro has a name like M134. The M is used at the start of every macro name and the number can be any integral value up to 99999 that is not used to define a built-in M-code. These built-in numbers are listed in chapter 11. Thus for example, M12, M50, M16543 are all valid macro names; while M3, M-56, M0234, M567.4 are not valid names.

Standard macros will use the number range up to M999, Original Equipment Manufacturers (OEMs) are advised to use M1000 to M89999 and end-users can avoid naming conflicts by using M90000 to M99999.

Each macro is stored as text in its own file whose name is the macro name with file extension ".m1s". Thus one would find files: M12.m1s, M50.m1s and M16543.m1s on a system with the above macros in it. The macro files are collected in a folder corresponding to the profile name with which they are to be used within a folder called "Macros" within the Mach3 folder.

A macro is called just like an ordinary M-code command by:

  • Execution of a line (block) of a part program containing its name
  • Including its name on commands entered on the MDI line
  • Including its name in the code to be executed on clicking a screen button.

All these features are shown by the arrows into and out of the Macro box in the control flow picture

Windows' control

The final elements shown in the control flow picture are the standard devices that control Windows. Common examples are the mouse and trackball or a touch-screen. Windows also supports a gaming joystick and throttle. This can be used for jogging and overriding jog and feed rates.

Other customisation

Global hotkeys

Config>Set System Hotkeys allows you to set up the keys which will jog the axes when the Jog Mode button is displayed on the current screen. Click the button for the axis and direction you want to set and then type the keystroke to be used. It is your responsibility to make sure they are unique and do not clash with other hotkeys you might want to use. You can use ScreenTweak to prepare a list of standard hotkeys.

These hotkeys can very usefully be generated by a profiler such as KeyGrabber to provide jogging support from Human Interface Devices or buttons and encoders attached to a keyboard emulator.

You can also define hotkeys which will select the MDI window (default is Enter), will select a DRO and will display the File>Load G-code dialog. These are useful if you are controlling Mach3 without a mouse or similar pointing device.