Machsupport Forum

Mach Discussion => General Mach Discussion => Topic started by: TheSuperior on March 25, 2022, 10:01:41 AM

Title: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 25, 2022, 10:01:41 AM
Hi there,

I have read the posts I could find on the forums concerning arduino and mach 3 and connecting this using modbus.

http://www.machsupport.com/forum/index.php/topic,22982.0.html

This post from Zafar has been really helpful in connecting arduino as a slave to mach3 and using it with 2 x potentiometers for FRO and Spindle override

I want to do something similar with an Automatic Tool Changer.

the only thing I need is it should send an integer value between 1 to 12 to the arduino every time it encounters the M6 Gcode and based on what tool was selected.

ex. M6 T0101 - should send "1" to the arduino.

I have no idea how to modify the code to do this. Is there anyone who has done this before??
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: zafarsalam on March 25, 2022, 10:10:10 AM
Hi,
You'll have to add new fields to the modbus configuration in setup serial modbus control menu. Perhaps use a userdro to hold the new tool number and then use this dro in the brains. This all can be handled in the M6Start macro and a brain. Also the arduino sketch needs some additions to handle the new registers.

Zafar
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 25, 2022, 10:18:46 AM
Hi Zafar,

Thank you for the quick reply.

The new fields that I add assume needs to be Output holding?

I have never made a userDRO before, I will watch a couple of youtube videos that might enlighten me.

I know how to make a brain based on your tutorial but have no idea what functions to use for this?

the m6start macro I could do. How do I change the arduino sketch to receive the inputs and can this still be done using the USB or do i need a RS232 to USB converter and use serial 1,2 or 3 on the arduino? I am completely lost.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 25, 2022, 10:29:39 AM
Also, I am doing this on Mach3 turn and not in mill. This is a 12 tool - tool changer turret I built.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: zafarsalam on March 25, 2022, 10:42:58 AM
In the M6Start macro you can use the vbcript function GetSelectedTool() and save it in a userdro.
setuserdro(2001)=getselectedtool()

In the arduino sketch 40 regs are already assigned while we are using just two of them regs[0] and regs[1] for reading analog inputs on pins A0 and A1. These regs are passed on to Mach3 brain as input-holding.

Now you'll have to define new config as output-holding and assign an address of 10 and number of registers 1. In the brain you add the new rung with userdro as the input and the modbus output register as the terminal.

The value of userdro(2001) will reflect in the arduino as regs[10].

I hope you got some hold of this confusing technique.

Zafar

Hi Zafar,

Thank you for the quick reply.

The new fields that I add assume needs to be Output holding?

I have never made a userDRO before, I will watch a couple of youtube videos that might enlighten me.

I know how to make a brain based on your tutorial but have no idea what functions to use for this?

the m6start macro I could do. How do I change the arduino sketch to receive the inputs and can this still be done using the USB or do i need a RS232 to USB converter and use serial 1,2 or 3 on the arduino? I am completely lost.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 25, 2022, 11:22:09 AM
So to clarify just check my images if this is correct?

and then in the sketch:

enum {       
        MB_REG0,
        MB_REG1,
        MB_REG10,
        // line below is for holding registers.    'vinny'
        MB_REGS   = 40    /* total number of registers on slave */
};

I added MB_REG10, ? is that correct?

I'm struggling with the brain where you say modbus output register as the terminal:
enter the modbus adres to use. what number should i enter here?
the CFG# would be number 2 then?
and macro modbus emulation?

Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 25, 2022, 01:59:39 PM
So I have watched a couple of videos. Creating the DRO was fairly easy

In the M6start macro this line: SetUserDRO(2001) = GetSelectedTool()
does not want to compile and i receive a type mismatch error.

I have no idea what to do further in the arduino Sketch?
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 25, 2022, 02:29:41 PM
So this is what I have so far and seems to compile fine:

'Variables

Dim OldTool      As Integer
Dim NewTool      As Integer


'*************************************************************
'                     Main Sub
'*************************************************************


Sub Main()   


   
  'Define variables

  OldTool = GetCurrentTool()
  NewTool = GetSelectedTool()

   
  Message("Tool Change Initiated Old Tool #" & OldTool &" New Tool #" & NewTool)


  'If tools are the same then exit
 
  If NewTool = OldTool Then       
    Message("New Tool and Old Tool are Same - No Action Requested")
    Exit Sub
  End If
 
  SetUserDRO(2001) = GetSelectedTool()

  Message("**********  Tool Change Start ********************")

   'Move The Toolchanger away from the Spindle - I stiill need to save the old positions and continue machining operation from there
   
   Code "G0 Z200"

End Sub
= tool
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 25, 2022, 03:44:07 PM
So this is what I have now:


  'Define some constants
 
    MyDRO = 2001   
   
  'Set MyDRO to GetSelectedTool()

   SetUserDRO(MyDRO, GetSelectedTool())
   
   'SetUserDRO(MyDRO, "2") 'this was just to test if it works correctly when I run the script
   
   'Get current value of MyDRO
   MyDROVal = GetUserDRO(MyDRO)
   
  'Define variables

  Message("Tool Change Started. Tool: " & MyDROVal & " selected")

   'Move The Toolchanger away from the Spindle - I still need to pause then save the old positions and continue machining operation from there
   
   Code "G0 Z200"
   
It compiles fine and it sets my UserDRO to the number I select when testing it through the VB script editor. But If I run G-codes with toolchanges it does not change the userDRO value, neither does it change if I just type M6 T0202 in the command line? What did I do wrong?
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 25, 2022, 03:58:33 PM
I had to uncheck the box "ignore tool change" in the general config and check the box "Auto toolchanger"
Now the macro is working fine along with userDRO of 2001. How will I test the brain I made to see if it is working the way it should?
Now it seems it is just the arduino sketch left to modify? So how do I do this and how do I add my toolchanger code to it? without either one interfering with the other?
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: zafarsalam on March 26, 2022, 12:33:05 AM
Good to see that you have progressed so far. You can view the brain using the menu Operator/Brain Control ... and then click on the Brain View button.

In the Arduino sketch you should handle the inputs and outputs through the DIO pins. What I prefer to do is use two of the pins of Arduino to communicate with Mach3. One pin receives signal from Mach3 to initiate the toolchange sequence and the other signals back to Mach3 when the tool change sequence has completed. You can connect all your tool change actuators and sensors to the input and output pins of the Arduino. Be careful when using the delay command in Arduino. During the delay loop the communication with Mach3 through modbus will stop. Better use the timer function in Arduino for the delay. Also be careful of the infinite loops in case of any actuator or sensor malfunction. Add checks in your code to break out of infinite loops based on a time limit.

Also you can program some error codes which you can feed back to Mach3 through the modbus registers.

Zafar

I had to uncheck the box "ignore tool change" in the general config and check the box "Auto toolchanger"
Now the macro is working fine along with userDRO of 2001. How will I test the brain I made to see if it is working the way it should?
Now it seems it is just the arduino sketch left to modify? So how do I do this and how do I add my toolchanger code to it? without either one interfering with the other?
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: zafarsalam on March 26, 2022, 12:52:24 AM
The terminator in the new brain should be like this.



So to clarify just check my images if this is correct?

and then in the sketch:

enum {       
        MB_REG0,
        MB_REG1,
        MB_REG10,
        // line below is for holding registers.    'vinny'
        MB_REGS   = 40    /* total number of registers on slave */
};

I added MB_REG10, ? is that correct?

I'm struggling with the brain where you say modbus output register as the terminal:
enter the modbus adres to use. what number should i enter here?
the CFG# would be number 2 then?
and macro modbus emulation?


Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: zafarsalam on March 26, 2022, 01:04:22 AM
It is not needed there.

I added MB_REG10, ? is that correct?


Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 26, 2022, 02:32:37 AM
Thank you Zafar, this is really good information and much appreciated!

The delays in my sketch might be a bit of a problem. As I use delays to turn the stepper motors a step each time.

I will try to implement a timer method, I will study this as I have not done it yet I have only used delays and millis before.

What else do I need to add to the sketch? As I'm using an Adruino Mega I can use Pins 18 and 19 which are TX1 and RX1 respectively for the receiving and sending of the signals? But then I'd need to use a RS232 to USB adapter? Or did I perhaps not understand well? So then I do not need the USB 2.0 Cable Type A/B ? Can I attach my code for you to have a look at?
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: zafarsalam on March 26, 2022, 08:14:24 AM
I normally use the MAX3232 module to connect the arduino to serial port of the computer. Using pins 0 and 1 on arduino for RX and TX.

https://www.amazon.com/MAX3232-Converter-breadboard-Electronic-Connector/dp/B082B97DYQ (https://www.amazon.com/MAX3232-Converter-breadboard-Electronic-Connector/dp/B082B97DYQ)

Why are you not using mach3 to drive the stepper motor for the tool changer. You can share the code if you wish.

Zafar
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 26, 2022, 09:04:27 AM
Ah excellent, I'll purchase one of those, thank you.

The plan is:

The tool changer should be a standalone accesory for normal manual lathe operation as well. In other words. You can add it to any Lathe that is not CNC and use either the app, the buttons or the digital to control it. so I want to use an automatic tool changer on our lathes that are not CNC. I have been working on these Lathes for about 12 years now and even with a quick change it can really sometimes be a pain... It is just to make life a bit easier, and expand my knowledge in doing so. Oh and also make the wife angry... Also I want to be able to incorporate it on other systems as well such as GRBL. I'm a mechanical Engineer, so designing everything is pretty easy but when it comes to the programming side I really lack the skills and I consider myself to be a "noob" as they call it.

I have attached my arduino sketch.

I have read some posts about the timers and see now that the Delay() function is a really bad way to code. I'd say one of my biggest problems are where I move the stepper motor each step at a time and checking when my limit switch was activated. How do I replace those delays with a non-blocking timer? this is the segment of code I'm referring to:

while (!digitalRead(home_switch)==LOW) { // Do this until the switch is activated

    digitalWrite(dirPin, HIGH);
    digitalWrite(stepPin, LOW);
    delay(delayTime);                       
    digitalWrite(stepPin, HIGH);
    delay(delayTime);                       
   
  }

Also I have attached the sketch in txt format if you would like to give some pointers of where to start and incorporate this with your modbus sketch.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 26, 2022, 09:05:25 AM
Also I'm using a Mega with a CNC shield. It will not be possible for me to use pins 0 and 1
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 26, 2022, 09:32:22 AM
So i received this feedback from a user on the arduino forums:

A while loop is blocking by its very nature and a delay() in a while loop magnifies the length of the blocking

The solution is to use an if instead of a while and millis() for timing. No doubt you have seen the BlinkWithoutDelay example which uses this method

So, your code would in essence look like this

if the switch is activated
  if the step period has elapsed as measured using millis
    take a step
    save the time of the step
  end if
end if

A Timer is non-blocking and will also allow other code to run while this one is running which could be catastrophic as the ATC uses a solenoid pin, therefore is there any way to replace the Delay() and make it non-blocking code but not run the rest of the loop? any other method or function?
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: zafarsalam on March 26, 2022, 12:21:22 PM
You can try the stepper library to drive the stepper motor. With it you can put acceleration, deceleration, Max speed etc on your pulse train.

So i received this feedback from a user on the arduino forums:

A while loop is blocking by its very nature and a delay() in a while loop magnifies the length of the blocking

The solution is to use an if instead of a while and millis() for timing. No doubt you have seen the BlinkWithoutDelay example which uses this method

So, your code would in essence look like this

if the switch is activated
  if the step period has elapsed as measured using millis
    take a step
    save the time of the step
  end if
end if

A Timer is non-blocking and will also allow other code to run while this one is running which could be catastrophic as the ATC uses a solenoid pin, therefore is there any way to replace the Delay() and make it non-blocking code but not run the rest of the loop? any other method or function?
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 26, 2022, 01:21:27 PM
I did try that, after a lot of trial and error I managed to workout a  timer but the code does not work well in the loop as it becomes extremely slow.

The stepper library can not move the motor in the setup where the homing takes place either, so that is not going to work?

I wrote this sketch to test it before I add it to the main sketch and it works well as is:

//Include the Stepper library
#include <AccelStepper.h>

// Time periods between steps in milliseconds
const unsigned long Stepinterval =1;

const int solenoidPin = 53; //This is the solenoid output pin

const int home_switch = 52; // Pin 52 connected to Homing Switch (Sensor)

const byte stepPin = 2;
const byte dirPin = 5;
const byte enablePin = 8;

int StopHoming = 0;

// Variable holding the timer value so far.
unsigned long Steptimer;

AccelStepper x_stepper(AccelStepper::DRIVER, stepPin, dirPin);

void setup ()
  {

    Serial.begin(9600);
    pinMode(enablePin, OUTPUT);
    digitalWrite(enablePin, LOW);
    pinMode(solenoidPin, OUTPUT);
   
  Steptimer = millis ();
 
           

  }  // end of setup

void MoveStepper ()
  {
    digitalWrite(dirPin, HIGH);
   if (digitalRead (stepPin) == HIGH)
      digitalWrite (stepPin, LOW);
   else
      digitalWrite (stepPin, HIGH);

  Steptimer = millis (); 
 
  }  // end of MoveStepper



void loop ()
  {
   
                  if (!digitalRead(home_switch)==LOW){
                      if ( (millis () - Steptimer) >= Stepinterval){
                          MoveStepper ();         
                      }
}

}  // end of loop

But if I add it to my main sketch it's just extremely slow and has to run through the loop the whole time. It seems with controlling the motor and trying to home it there just is no alternative then delay() ?
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 27, 2022, 03:14:10 AM
Do you think the code inside of the setup will matter? the Delay() ? As those will only run with the initial startup? Also what is the reason for the program not to have the Delay() functions? how does this affect Mach3 exactly?

Furthermore if I figured out how to rewrite everything using timers? What else should I do in order to integrate this with your code?
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: zafarsalam on March 27, 2022, 03:27:40 AM
During a delay the modbus communication will stop. Since you don’t need to send any signals to mach3 during the stepper movement so it is safe to use it.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 27, 2022, 07:19:54 AM
So it only stops during the Delay() ? In that case it would not matter would it? As the arduino would wait for the serial Input? after that the Arduino executes the move completely independent of Mach3 and then continues again? I have added code in the m6tstart macro to move the tool changer away from the job, so the Arduino won't need to signal mach3 when it is done either as this will take some time to execute? after the change is done it can just run through the loop again and wait for the next command or tool change? Or am I completely wrong here? I feel I'm getting just so close yet so far. You have really been a great help, Thank you. I'm not sure what my next step is?
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: zafarsalam on March 27, 2022, 10:46:16 AM
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 or the state of a pin which can be connected to an output pin from Mach.

In the tool change macro you can either turn an output on and off to signal the arduino to start the tool change sequence. Or change the value of a user DRO which can be read in brains and then to modbus. Then you can do a while statement in the tool change macro which waits for the input from Arduino to finish the tool change macro.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 27, 2022, 02:27:47 PM
I finally managed to rewrite the entire code without a single delay in it. Oky, let me see if I can follow, although I'd like to read the value of a register from Mach3 modbus? From value 1 to 12?  So then change the value in the user DRO which can be read in brains and then to modbus? Still I have no idea how to start writing that or incorporating it?
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TPS on March 28, 2022, 12:05:15 PM
might be interesting:

https://www.machsupport.com/forum/index.php?topic=1658.0

but the question is why using a arduino to control the ATC. IHMO there can be nothing on a ATC
what Mach3 can not control itselve via a VB Script.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 28, 2022, 12:45:06 PM
Thank you TPS. This seems as though it could be useful.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 28, 2022, 01:49:45 PM
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 or the state of a pin which can be connected to an output pin from Mach.

In the tool change macro you can either turn an output on and off to signal the arduino to start the tool change sequence. Or change the value of a user DRO which can be read in brains and then to modbus. Then you can do a while statement in the tool change macro which waits for the input from Arduino to finish the tool change macro.

So I'm trying to figure out how to do this,
in the m6start macro should i use sendserial() ?

Please help with what the if statement needs to contain?
In the sketch:

if (txenpin > 1) { // pin 0 & pin 1 are reserved for RX/TX
                Txenpin = txenpin; /* set global variable */
                pinMode(Txenpin, OUTPUT);
                digitalWrite(Txenpin, LOW);
        }

Do I have change these pins to use 18 and 19?

unsigned int Txenpin = 0;        /* Enable transmission pin, used on RS485 networks */

Then here is the code I use to capture the serial input. How do I modify this to capture what modbus sends or will it work as is?

void ReceiveData() {

  while (Serial.available()) {
    BlinkPattern(); 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

  if (readString.length() >0) {
    //Serial.println(readString);  //so you can see the captured string
    //int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    Input = atoi(carray);
   
    readString="";
  }
 
}
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 28, 2022, 02:12:37 PM
another thing, as I'm using Arduino mega with a CNC shield mounted at the top. Won't this interfere ?
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TPS on March 29, 2022, 02:18:02 AM
the code of the posted example:

https://www.machsupport.com/forum/index.php?action=dlattach;topic=1658.0;attach=1174

will send a small string via SendFifo() witch is an undocumented Mach3 VB script function.
this string will contain: 

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

witch "gives" the Adrduino a minimum chance to check the received data for validity.
than it will wait for a response (Hex B6) to know that Arduino has "understand" the order.

sorry but can not "assist" on Arduino side.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior 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
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior 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"
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior 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] ?
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TPS 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.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior 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.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TPS 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.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior 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?
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TPS 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.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior 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
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior 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)
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TPS on March 31, 2022, 04:56:39 AM
0x1E  0x?? 0x0D where ?? is the toolnumber

based on this code for M6Start:

https://www.machsupport.com/forum/index.php?action=dlattach;topic=1658.0;attach=1174

and again that has nothing to do with Modbus Communication you mentoined to use.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 31, 2022, 05:41:00 AM
It will still use modbus to communicate to the microcontroller.

What I mean is what would an actual message of this Startsign (Hex 1E) + toolnumber + CR (Hex 0D) look like

I see that The value of HEX 1E in decimal is 30 and in binary is 00011110

Hex 0D = 0D

So would it be: 30 01 0D for tool number 1
30 02 0D - tool number 2
30 03 0D - tool number 3
30 04 0D - tool number 4
etc
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TPS on March 31, 2022, 05:45:59 AM
looks like i am writing Swahili ??

IF YOU WILL USE MODBUS THE DATA WILL BE COMPLETLY DIFFERENT THAN IN MY EXAMPLE
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 31, 2022, 11:54:15 AM
I know this, but it seems you do not fully understand.

I need to write code in arduino to understand what is send to it.

This macro basically sends

 30 toolnumber 13

but in Hex format. This is what I'm asking, so I figured it out myself.

ToolCommand = &H1E     'Tells microcontroller the job is tool change
JobDone = &HB6         'Micro sends this value when the job is done
CReturn = &H3E         'Value of carriage return in Hexadecimal

This is in the macro and gets appended to be send as a complete message.

I need to know how this macro is send over RS232 (Not rocket science I assume)

which I did figure out without using Swahili.

&H1E this is the same as 0X1E which is Hex format for a Record Seperator
&H0D this is the same as 0X0D ehich is HEX format for Carriage Return

Now I can not just randomly try to Parse this in arduino as it will not work. but what I can do is use both of these as Start and end markers. That is what they were intended for but I can replace them with my own start and end markers

ex. "<" and ">"

That way I can use only the integer value that is returned between my markers. That is exactly how this macro was written when it was made for the microcontroller.

So when everything is done I can use this: JobDone = &HB6         'Micro sends this value when the job is done
but instead of &HB6 I can use my own HEX value to send back to Mach3 and tell it the tool change has been completed.

Visual Basics and C++ are worlds apart therefore for them to communicate takes a different approach

The easy answer would have been for you to say '30 toolnumber 13' in HEX format but it seemed you did not understand this either.
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TPS on March 31, 2022, 12:20:55 PM
OK if you think i did not understand the real problem, it is ok for me. i am out.

good luck anyway
Title: Re: Arduino - Mach 3 - Gcode M6 T0101
Post by: TheSuperior on March 31, 2022, 03:30:39 PM
I have finally managed to solve it. Thank you Zafar and TPS for all the help it is really appreciated.