Modbus in Mach

From Mach3Wiki
Jump to: navigation, search


Introduction to Modbus

Modbus is an internationally standardised protocol for communicating with Input/Output devices over a serial or TCP/IP datalink. The standard can be found here but you are unlikely to need to read this if you are using a Modbus device that is programmed for use with Mach3.

Mach3 uses the serial implementation and allows RS-232 or RS-485 (two wire or four wire) physical connections between the PC and Modbus device. Many users will find RS-232 (as implemented by PC COMx ports) totally satisfactory. RS-485 allows much greater distances between equipent and more reliable operation in electrically noisy environments.

TCP/IP Modbus

(Click link above to goto the subject/setup page on TCP/IP modbus)

Modbus devices and Mach3

Anyone who has customized Mach3 to use on a system with more than the most basic limit switches will be aware of the shortage of inputs and outputs via the parallel port. A second port can assist but there are still severe limitations for a large machine. It is possible to use a keyboard emulator (e.g. IPAC) to extend input functions but this is not totally reliable as it uses the keyboard routines on Windows and "keystrokes" can get lost.

A Modbus device is the preferred way of expanding input/output capability for signals which are not time-critical.

Mach IV uses the GRex interface which has its own extensive I/O capability. This chapter currently describes the Mach3 implementation of Modbus.

Many Programmable Logic Controllers (PLCs) offer Modbus interfaces as standard or options and offer very flexible and cost effective ways of interfacimng to the sensors and actuators of machine tools. Code in the PLC can be used to implement the complex sequences required for automatic tool changers, work pallet controls, safety interlocks etc. In principle Mach3 will work with any such PLC but the majority of system implementation work will lie in programming the PLC. This is outside the scope of this chapter.


The ModIO

The ModIO (tm) Homann Designs is a range of pre-programmed IO units which connect using Modbus serial connections. A ModIO provides simple interfacing to Mach3 of:

  • Switches to control Mach operation
  • One or two Manual Pulse Generators (MPGs) for axis jogging and shuttle control of running part-programs
  • Analog input from potentiometers to control functions such as feed override or, with a few fixed resistors, to interface multi-position switches to define jog step sizes or which axis to jog.


How Mach3 accesses devices interfaced by Modbus

All Modbus devices are slaves. That means that they can accept data from Mach3 in the PC or accept a command to send some data to the PC. A Modbus device cannot make a request for anything to be done.

A Modbus device appears to its controlling system (Mach3) as numbered registers which can be read, written or read and written. The addresses and the meaning of these registers depend on the Modbus device and, if it is programmable, what program has been installed. The Modbus specification does however allow for different types of register to reflect their function (e.g. single bits or numeric values) and there are different ways or reading and writing them.

With an RS-485 connection it is possible to have more than one device on the same four wires. This is, of course, where the "bus" in Modbus comes from. Even when only one device is present (or can be present in the case of RS-232), it needs a Slave Address. This will be programmed into the device or set by switches or jumpers. Mach3 uses this slave address in everything it sends to the device. A Modbus device only responds to messages containing its address.

Mach3 can send messages to the Modbus device in two different wasy:

  • By reading data into a buffer in the PC and writing data from a buffer to registers in the Modbus device chosen in the configuration process. This occurs periodically at a defined rate (typically 20 times per second) and is called Autopolling
  • By reading a register or writing a register when required by the code of a Mach3 VB Script (on button or in a macro). This is called Direct access


Ports & Pins

A Modbus device can provide a way of connecting more devices to Mach3 via the Input and Output Ports & Pins lists.

In Mach3, one uses Port number zero to refer to the Modbus device. One can use 64 input "pins" and 64 output "pins" both numbered 0 to 63. Note: Input pin 12, for example, is quite distinct from output pin 12.


The screenshot shows four OEM Trigger inputs connected to Modbus "pins".

The actual association of the 128 pins (64 in and 64 out) with electrical connections to the Modbus device depends on the Mach3 Modbus configuration and how the device itsel (e.g. PLC) is programmed. This is easiest to explain in terms of the ModIO which has "factory supplied" programming aimed at using its inputs and outputs as pins on port 0.

Using Ports & Pins to access Modbus can only be done using Autopolling.

VB Script calls

The Autopoller will also transfer a block of input registers and a block of output registers to Mach3 polling buffers. VB Routines can be used to access these buffers.

Direct access to Modbus registers

There are occasions when a VB Script writer needs direct access to Modbus device registers. This is provided by an interface which allows reading and writing of a block of one or more registers into Mach3 buffers (distinct from the autopoller buffers) and routines for accessing data in the buffers.

Comparing Autopolling and Direct access to registers

When you are customizing Mach3 you will need to decide which method is most appropriate for accessing Modbus registers. You will often have a choice and which is best and which is easiest depends on exactly what you want to do and the features of your Modbus device.

The following paragraphs offer some guidance. You may wish to return to read them after you have configured an actual Modbus device. The Customization case studies should form a practical starting point for you work.

  1. If you want to interface circuits so they behave as Mach3 output pins (e.g. a spindle on off control) then you must do so using the autopoller.
  2. If you want push switches to act exactly as specified screen buttons (e.g. Cycle Start) then autopoller and defining OEM Trigger pins is probably easiest. Notice that all the inputs need to be in one contiguous block of registers. See details of configuration to understand this limitation.
  3. If your VB Script is likely to read or write a value very frequently (e.g. in a loop) then it is better to read/write the value in the autopoller buffer rather than do a actual Modbus transfer each time.
  4. If you need to get an error report from the Modbus device if a read or write has failed then you must use the direct access routines.
  5. If you need to access input registers that are not in a contiguous block or output registers that are not in a contiguous block then you must use the direct access routines. This is very likely to occur if you have a register which you need to read and write.
  6. If you are accessing registers in a macropump macro then this is, in effect, your own autopoller.
  7. Autopoller access requires the user to configure the registers using the Config>Modbus Setup dialog but VB Script code can be generic to a variety of situations.
  8. Direct access does not require any user configuration but might require changes to the VB Script code.
  9. Systems that require both forms of access can be hard to maintain because register addresses are in both code and the configuration and a given Modbus register might be referred to in two different ways depending if it is being read or written.

Configuring Modbus in Mach3

This section describes the steps in configuring a Modbus device with Mach3.

Modbus Input/Output Support must be checked on the Port Setup & Axis Selection tab of Config>Ports & Pins before any detailed configuration can be done. Mach3 should be reloaded after doing this.

Link titleM3PPModbusOn.JPG

It is generally necessary to have the Modbus device connected when performing the detailed configuration.

General configuration



The first section of the Config>Modbus Status & Control dialog defines the communications parameters.

In Port Num: enter 1 for Com1, 2 for Com2 etc.

Enter the Baud Rate to which the Modbus device is set and choose the data/stop/parity bits

Use RTS for Transmit (RS485) only applies to 2-wire connections and, if you use RS-485, you are advised to use 4-wire whenever possible as Windows is not reliable in its timing of the link direction with some interfaces and 2-wire.

The default timeout of 50mS is generally satisfactory.

Even with a connected device the Status will probably read Receive Timeout as you have not yet defined the Autopoller.

NOTE: For Automation Direct PLC's set the com to: 8-N-1 in the modbus config and the same in the second port setup on your PLC. Mach can only communicate with the 2cd port, since on DL05's and DL06's the 1st port is 8-O-1 and can't be changed. Also USB to serial adapters WILL work between Mach and the PLC, remember to look in your device manager then plug into the port you will use to see what COM Windows assigns it. You will have to use that specific USB/serial port from then out since to change physical ports will change the port #.


This has entries for input and output. The Input section loads a 128 word (16 bit) buffer from the Modbus device registers and the Output section unloads, to the Modbus device registers, a 128 word output buffer at the defined Update Frequency.

The first line of each section defines the registers to map to words 0 to 63. Each word corresponds to a pin. If an output pin is active then its word will be 1 otherwise it will be zero. Similarly a word set to 1 will makes its input be active.

You must specify the Slave address of your Modbus device, the address of the first register (i.e. to be buffer word 0 and the number of registers to be transferred at each poll. This may be less than 64 if there are fewer than 64 inputs or outputs.

Normally each input/output will correspond to a register. However automatic packing/unpacking as appropriate can be performed by checking Use each input words as xx bits of Bitmapped... in Special Control. The value nn is the number of bits in the Modbus Device register.

NOTE: When Using Bit packing special control, it only applies to the 1st Input and Output channels (0-63) respectivly. Input/Output channels #2, is Full Words only (entire 16 bit registars). With Bit packing remember that after 16 INPUTS or OUTPUTS, you will need to make sure you have the next higher registar available and turned on. I.e. 20 INPUTS would require a 2 to be put in the number of registars.

The second line of each section defines the registers to map to words 64 to 127. Each word corresponds to a 16 bit value. These registers will typically be counts from an MPG, derived from analog values input to the Modbus device etc.

Use the Radio buttons to select the type of Modbus register that the autopoller is to access, if your device is fussy about such things.

When you Apply the settings with a running Modbus device connected you should see the Status turn to No Error

Finally the OEM Control code... when checked allows a value in a given input buffer location to be treated as an OEM trigger. With suitable code in the Modbus device, this can be used to implement multiple pushbuttons buttons without using OEM Trigger pins.

If you continue to get Receive Timeout you should check the validity of the Slave Addresses, Register numbers, count and type of registers. The autopoller will flag an error if it cannot access these registers.

ModIO configuration


If ModIO Modbus card is checked then suitable default registers for it will be selected, bit packing will be used (as if selected in Special Control) and it is permitted to enable the MPGs

NB you may still have to configure the Slave Address if your ModIO is set to other than address = 1.

When ModIO Modbus is checked a special mapping is performed for inputs to load input buffer words 64 to 70. These are as follows:

Buffer word ModIO register
64 Analog 1
65 Analog 2
66 Analog 3
67 Encoder 1
68 Encoder 2
69 Tick counter
70 Frequency

If the second line of the inputs section is checked then any buffer locations loaded by it will overwrite the special assignments from this table.

When ModIO is enabled you can use Config to select a dialog to help set it's non-volatile configuration.


For details of the configuration registers you need to refer to the ModIO User's Guide for your device. The following notes may assist Mach3 configuration:

  1. Changes only take place in the ModIO when it is reset by hand or on power-up
  2. The Config can be set when the "Default Configuration" jumper is set in the ModIO but is, of course, never used while that jumper position is selected.
  3. Ensure you run with Disable Debug checked. Outputs are disabled in Debug mode
  4. If you have problems with outputs then they might be being disabled by the Charge Pump. If they ane non-critical, for example indicators, you might consider diabling the charge pump protection.

After using Send Config to save the configuration, do not forget to reset the ModIO.

Low Level testing Modbus devices

It is possible to use Mach3 to read and write individual Modbus device registers or blocks of registers.

This feature is avccessed by the Test Modbus button of the Modbus Status & Control dialog.

Opening the test dialog will switch off the Autopoller so you have total control of Modbus registers, even ones normally updated by the Autopoller. This may cause Modbus device watchdog or charge pump timers to time out. You may need to disable them for low level testing.


Any test must start with opening the port. The number and baud rate default to the values in the main configuration. The Bits and Parity settings cannot be altered in this dialog.

To inspect the contents of a register or block of registers define the Slave Address and register address of the start of the block. Enter the number of registers to transfer. Note: it is an error to attempt to read registers which are not implemented by the device so you must ensure the block exists as a contiguous set of registers.

Select the type of register and click Read. The values should be displayed in the edit box. The sample shows 6 registers read.

You can edit the values and click Write to send them to the Modbus device. In the example Register 103 is being changed to 0x00FF.

The Status message will reflect the state after a read or write request.

??? The dialog has a slider and Report Slave ID and Read Excp Stat buttone. If anyone can explain these please do so here. They do nothing for me :=)

The slider can be used to continously read the selected range of coils, inputs or registers. As the slider is moved towards the right the software starts polling the device with the selected command. Move it back to its left most position to stop the polling.

The 'Report Slave ID button' issues a function code 17 (Report Slave ID) to the adressed slave device. The slaves response will be device specific and is up to the manufacturer of the specific device. If the function code is not supported by the device it should respond with an Illegal Function error code but devices not fully complying to the MODBUS specification may simply not respond at all which will result in a timeout.

Currently (Mach3 v3.042.040) the 'Read Excp Stat' button does not seem to send anything to the device. One could guess it's supposed to send a Read Exception Status command (function code 07).

Original draft by John Prentice

airline cheapest diazepam tramadol buy online online generic allstate insurance affordable life insurance ambien buy cash advance cash loan credit card debt credit card online debt consolidation debt solutions eliminate debt home equity loan diet hoodia hoodia loss weight hoodia pure hoodia effects hoodia side insurance policy buy meridia meridia online cheap meridia mortgage rate mortgage refinance personal loan acyclovir zyban buy zyban online zyban side effect of zyban zovirax buy zovirax online zovirax side effect of zovirax zoloft zoloft buy online zoloft side effect zocor buy zocor online zocor zithromax zithromax side effect zithromax price zithromax online xenical buy xenical cheap xenical xenical online discount xenical xanax xanax online buy xanax wellbutrin wellbutrin sr wellbutrin side effect wellbutrin sr side effect vaniqa valtrex valium ultram tramadol soma soma online buy soma soma price soma buy online renova prozac propecia prevacid paxil meridia meridia online meridia 10 discount meridia meridia generic fioricet fioricet online fioricet no prescription buy fioricet online didrex buy didrex on line cheap didrex discount didrex didrex free shipping didrex price cheapest didrex diazepam darvocet darvocet information about darvocet darvocet and side effect celebrex carisoprodol carisoprodol information buy carisoprodol carisoprodol online soma carisoprodol online soma ambien acyclovir