My experiments with Mach3 ModBus and Arduino

Spread the love

My experiments with Mach3 ModBus and Arduino

My project is add some external Inputs and Outputs to Mach3 using Arduino. For that last several days I read many posts about this.
I test Arduino ModBus library. Is very well documented, work but is too complicated for me in a moment. Zafarsalam too have good tutorial who I read carefully.
So I try share here step by step all my experiments when learn Mach3 ModBus and because don’t know well Mach3 will thankful all who join and test.

PLEASE DON’T TEST USING MACH3 CONNECTED TO WORKING MACHINE!

Equipment:
1. Arduino board (I using Arduino Duemilanove but you can use any other Arduino board)
2. PC with Windows XP and Mach3 because software who I using for monitoring ModBus communication no support new versions Windows.
3. Software’s
– Arduino IDE
– Mach3
– Free Serial Port Monitor from https://info.serial-port-monitor.com

Part I: “Hello Word” or “Running Cycle Start button from Arduino Input pin”.

(1) Mach3 ModBus settings

1. Connect Arduino to PC and upload any blank sketch or Blink Led example. My Arduino using COM5 so recommend change your to using the same COM Port.

2. Run Serial Port Monitor, go to New Session >> Serial Port Monitor >> Choose COM port(COM5), mark Request View and click “OK”
Don’t close Serial Port Monitor. Here you will see ModBus communication!

3. Run Mach3, go to Config, Ports&Pins and mark “ModBus InputOutput support”.
– Click OK and restart Mach3.
– Go to Setup Serial ModBus control and set parameters exactly thereby:

Mach3 ModBus and Arduino
Mach3 ModBus and Arduino

Click Apply.

Now time for first checking break you to sure that all until now is correct.
– Mach3 in ModBus status show ‘Receive timeout’. In a moment this is normal. Arduino no programmed to receive command and reply.
– Minimize Mach3 and go to Serial Port Monitor. Because default timeout settings is 500ms you will see that any half second Mach3 send to Arduino this request “01 04 00 00 00 01 31 CA”. Don’t try now understand what this mean in a moment. Will expand in next parts.
– RX LED Arduino must blink too any half second.

Mach3 ModBus and Arduino
Mach3 ModBus and Arduino

If you don’t see exactly this go back and try to find problem.
And don’t continue until this step no work correctly! Any error bring no working project.

Now close Mach3 and Serial Port Monitor.

(2) Arduino programming

1. Upload this simple code who emulate ModBus slave to Arduino.
Please use this code only for test because no have any safeguard!

Quote
int pin = 2;  //Cycle Start button
int in_buffer[8]; // receiving buffer
int reply_1[] ={0x01, 0x04, 0x02, 0x00, 0x01, 0x78, 0xF0};
int reply_0[] ={0x01, 0x04, 0x02, 0x00, 0x00, 0xB9, 0x30};

void setup()
{
  Serial.begin(9600);            // Init serial communication
  pinMode(pin, INPUT);           // Set pin to input
  digitalWrite(pin, HIGH);       // Turn on pullup resistor
}

void loop()
{
   if ( Serial.available() == 8 ) {
     
     // read ModBus command from Mach3
     // in this example we only read, don't check anything 
     for (int i=0; i < 8; i++) in_buffer[ i ] = Serial.read();
     
     // Read Input PIN and reply with 0 or 1
     // Reply is inverted because PIN default state is HIGH and we connect to GND
     if ( digitalRead(2) == 0 ) 
         for (int i=0; i < 7; i++) Serial.write(reply_1[ i ]);
        else 
         for (int i=0; i < 7; i++) Serial.write(reply_0[ i] );
  
   }   
}

Don’t hurry to run Mach3. First try to understand what Arduino reply to Mach3 request.

Please download this ModBus test software:
https://info.kmtronic.com/software/ModBus/KMtronic_ModBus_Tester.zip

Run, set COM5, 9600, click Open, put this “01 04 00 00 00 01” command in text box and send to Arduino.
Next connect PIN2 Arduino to GND and click send again.

Mach3 ModBus and Arduino
Mach3 ModBus and Arduino
Mach3 ModBus and Arduino
Mach3 ModBus and Arduino

Now time to expand…
Please ignore last two bytes commands and replies. This is Check Sum and software calculate before send command for easy usage.

Command “01 04 00 00 00 01” is ModBus command who send Mach3 (check previous part).
You see in log that first reply when PIN2 is unconnected is:
1 – Rx: 01 04 02 00 00
and
3 – Rx: 01 04 02 00 01
when PIN2 is connected to GND.

So Arduino now will reply 0 or 1 when Mach3 ask for status Input.

Again time for break if all is not the same.
Mach3 and Arduino must ‘talk’ together so if one of this two no work correctly no sense you to continue…

(3) Mach3 & Arduino

1. Close all programs
2. First run Serial Port Monitor >> Request View
3. Next run Mach3
If all is OK:
– Mach3 in “ModBus InputOutput support” will show ‘No error’
– Arduino TX and RX LEDs will blink very fast.
– In Serial Port Monitor you will see a lot of Mach3 requests and Arduino replies.

Mach3 ModBus and Arduino
Mach3 ModBus and Arduino

Again break if all is not the same!

Time for last step – using brain tell Mach3 what command to execute when button is push down (PIN2 connected to GND).

(4) Create a brain who read ModBus and control Cycle Start button

1. Close again all programs.
2. Run Mach3 only and open Brain editor. Click on ‘+’, go to ModBus and mark this:

Mach3 ModBus and Arduino
Mach3 ModBus and Arduino

Save file to Mach3\Brains in example ‘Arduino_Test_01.brn’.
Now go to Brian control, click ‘ReloadAll’, enable it and next ‘ViewBrain’.

Test that all until now work.
When you leave free PIN2 you will see Input blue. When connect to GND – green.

Mach3 ModBus and Arduino
Mach3 ModBus and Arduino
Mach3 ModBus and Arduino
Mach3 ModBus and Arduino

Brain now read Input!!!

This Input you can connect now to any function in Mach3. This example is how connect to Cycle Start button.

Go back to Brain editor, mark Input and using ‘+’ add to Input:

Mach3 ModBus and Arduino
Mach3 ModBus and Arduino

Next mark again and go to Mach3 function ‘1000- CycleStart’

Mach3 ModBus and Arduino
Mach3 ModBus and Arduino
Mach3 ModBus and Arduino
Mach3 ModBus and Arduino

Save brain, re-load from Brian control, open any G-Code and try!

Hope this will be usefully Mach3 users understand how ModBus works .

Happy fun!

 

 

KMtronic Modbus RS485 RTU Serial to Modbus LAN TCP/IP Module Converter

 

KMtronic RS485 8 Channel ModBus RTU Relay Controller

 

 

Ethernet Modbus IP 8 channels Modbus/TCP LAN Relay board

 

 

 

916total visits,5visits today