Hello Guest it is April 18, 2024, 03:46:46 PM

Author Topic: Arduino - Mach 3 - Gcode M6 T0101  (Read 4560 times)

0 Members and 1 Guest are viewing this topic.

Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #30 on: March 29, 2022, 11:13:47 AM »
Thank you TPS this is really super useful, but still a bit above my understanding. It only means I have some more research to do. I'm going to test this code and see what I can come up with. thank you
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #31 on: March 29, 2022, 11:15:36 AM »
Zafar, if you can just help me with this if statement please:

"In the Arduino sketch you can do an if statement in the main loop. You can read the value of a register from Mach3 modbus"
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #32 on: March 29, 2022, 11:28:12 AM »
I did check if the code still works properly with the mega and CNC shield with my current setup. I just changed this

regs[1]=analogRead(A8);

to connect the pot on analog 8, it worked fine except for a little bit of interference.

I somehow need to copy all of my code into the modbus sketch, I did try this with no luck as my sketch and toolchanger still worked fine but the pot did not work anymore. No idea why this might be.

I just need to know how to make write the if statement and what it should include. I assume it will be regs[10] ?

Offline TPS

*
  •  2,505 2,505
    • View Profile
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #33 on: March 29, 2022, 12:01:09 PM »
ok, i see you will use modbus for communication with your Arduino, than you can forget about the code sample
i posted. this will only work as a simple serial interface without modbus framing.

for modbus you have to set up modbus in Mach3 and the used In/Out registers.
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #34 on: March 30, 2022, 03:43:14 PM »
The code might work if I modify it a little to send only Chr(ToolN) which is only the tool number.

Offline TPS

*
  •  2,505 2,505
    • View Profile
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #35 on: March 31, 2022, 02:20:27 AM »
yes sure, but you should keep in mind, that there is no chance to check the received data for validity.
i think there must be a minimum of safety check. otherwise , if you only have one single character (1-12)
the ATC will start a toolchange even if the data was caused by a electrical noise ore so.
that is one of the most important reasons why dataframes (at least modbus does the same) have been
created/established.

just a thinking.
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #36 on: March 31, 2022, 04:02:47 AM »
oky I understand,

What if I use something like this arduino sketch that receives a request from mach3 and responds with a 1 or 0:

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();
           Serial.println(in_buffer[ i ]);
     }


     
     // 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] );
 
   }   
}

What I actually want to know is when mach3 sends the request to the slave what would it look like? Something like this?

01 04 00 00 00 01

and which of these data bits will be the tool number?

it would actually look something like this then? 01 04 00 00 00 01 31 CA

It would be easy if I can just figure out what this lne of data looks like when mach3 sends it as a request to arduino. I would be able to convert it then.

If I make any sense at all?

Offline TPS

*
  •  2,505 2,505
    • View Profile
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #37 on: March 31, 2022, 04:12:14 AM »
i am still not realy sure wether you what to use Modbus communication or the serial communication of the
sample i posted.

in case of the sample i posted the reveive data will look like this:

Startsign (Hex 1E) + toolnumber + CR (Hex 0D)

or

0x1E  0x?? 0x0D where ?? is the toolnumber

in case of you are using modbus, i have no experience what data would look like.
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #38 on: March 31, 2022, 04:38:30 AM »
At this point I'm just trying to figure it out.

All I want is this:

Mach3 G-code: M6 T0101

This gets stored into the userDRO that I created, from there a macro should save the value in the userDRO into a variable and send that number to my arduino (The only numbers that will ever matter is 1 to 12. So even if I have to write a If statement for every single number it is fine. I just want to send a number based on the tool selected to my arduino. From there the arduino can what for the number and perform the toolchange according to my program and maybe signal mach3 when it is done or it does not even need to.

So basically:

Mach3 Turn encounters M6 T0101 G-code
Stores Tool number 1 in the userDRO(2001)
Runs the m6Start Macro
Saves the tool number and sends it using the RS232 hardware connected to arduino to the arduino
arduino is already configured to receive it and do what it needs to do.

But obviously this gets done through the modbus protocol in Mach3 and a brain
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #39 on: March 31, 2022, 04:53:02 AM »
i am still not realy sure wether you what to use Modbus communication or the serial communication of the
sample i posted.

in case of the sample i posted the reveive data will look like this:

Startsign (Hex 1E) + toolnumber + CR (Hex 0D)

or

0x1E  0x?? 0x0D where ?? is the toolnumber

in case of you are using modbus, i have no experience what data would look like.

Could you please give me an example of what this will look like Startsign (Hex 1E) + toolnumber + CR (Hex 0D)