Page 1 of 1

UCCNC and Arduino

PostPosted: Sat Dec 31, 2022 5:27 pm
by camivic
Hi everyone.
I would like to start studying the possibility of communicating from UCCNC to an ARDUINO.
Could you point me to a simple guide to get started?
Maybe even a simple Arduino sketch that I can use as a base.
Thanks to all and happy new year.

Vincenzo

Re: UCCNC and Arduino

PostPosted: Mon Jan 02, 2023 9:57 pm
by camivic
For the moment I have solved: I found an excellent sketch for arduino, configuring some "READ INPUT" and some "READ INPUT REGISTER".
With the "READ INPUT" everything works, while with the "READ INPUT REGISTER" connected to the Arduino A0 output, I don't understand what code to insert in the UCCNC macro in order to vary the SRO (or FRO).
Can anyone suggest me a solution?
Thank you.

Re: UCCNC and Arduino

PostPosted: Tue Jan 03, 2023 10:10 am
by dezsoe
Here is a sample macroloop from my archive which handles analog and digital inputs/outputs:

Code: Select all
// ================================================================================================
// Arduino Modbus I/O sample
// ================================================================================================

ushort Inputs = 0;
ushort Pot1 = 0;
ushort Pot2 = 0;

exec.GetModbusregister(0, out Inputs);
exec.GetModbusregister(4, out Pot1);
exec.GetModbusregister(5, out Pot2);

int FRO = Convert.ToInt32(AS3.Getfield(232).Replace("%", ""));
int JRO = Convert.ToInt32(AS3.Getfield(913));

int newFRO = (int)((double)Pot1 / 1023.0 * maxFRO / 5.0) * 5;                   // 5% steps
int newJRO = (int)((double)Pot2 / 1023.0 * 100.0);

if (newFRO != FRO)
{
  AS3.Setfield(newFRO, 232);
  AS3.Validatefield(232);
}

if (newJRO != JRO)
{
  AS3jog.Setfield(newJRO, 913);
  AS3jog.Validatefield(913);
}

int newInputs = ((int)Inputs & 0x0F) ^ 0x0F;                                    // 4 inputs, change to active high

if (lastInputs == 16) lastInputs = newInputs;

if (newInputs != lastInputs)
{
  int change = newInputs ^ lastInputs;
  // NC reset switch -> force reset to switch state
  if ((change & 0x01) != 0)
  {
    if ((newInputs & 0x01) != 0)
      exec.Callbutton(513);
    else
      exec.Callbutton(512);
  }
  // Compare SPST switch state to screen button state -> press button if needed
  if ((change & 0x02) != 0)
  {
    if (((newInputs & 0x02) != 0) != AS3.Getbuttonstate(114))
      exec.Callbutton(114);
  }
  // NO pushbutton
  if ((change & 0x04) != 0)
  {
    if ((newInputs & 0x04) != 0)
      exec.Callbutton(130);                                                     // Button pressed
//  else
//    exec.Callbutton(0);                                                       // Button released
  }
  // NO pushbutton
  if ((change & 0x08) != 0)
  {
    if ((newInputs & 0x08) != 0)
      exec.Callbutton(522);
//  else
//    exec.Callbutton(0);
  }
  lastInputs = newInputs;
}

bool LED1 = exec.GetLED(19);
bool LED2 = exec.GetLED(54);
bool LED3 = false;
bool LED4 = false;

ushort digitalOut = (ushort)((LED1 ? 0x01 : 0) + (LED2 ? 0x02 : 0) + (LED3 ? 0x04 : 0) + (LED4 ? 0x08 : 0));

ushort analogOut1 = (ushort)(AS3.Getfielddouble(2451) / AS3.Getfielddouble(178) * 255); // Overridden RPM / max. RPM
ushort analogOut2 = (ushort)(0);

ushort[] data = new ushort[3];
data[0] = digitalOut;
data[1] = analogOut1;
data[2] = analogOut2;

exec.SetModbusregisters(10, data);

// ================================================================================================

#Events

// ================================================================================================

const double maxFRO = 150.0;

int lastInputs = 16;

Re: UCCNC and Arduino

PostPosted: Tue Jan 03, 2023 2:16 pm
by camivic
Thanks dezsoe, i try it.

Re: UCCNC and Arduino

PostPosted: Tue Jan 03, 2023 2:42 pm
by formantjim
Hi Camivic I would be interested to see that sketch for the Modbus Arduino if you could share please?

Re: UCCNC and Arduino

PostPosted: Wed Jan 04, 2023 1:20 am
by Battwell
I played with a couple of mega a few years ago using modbus
Made a great control panel :-)
Gives lots of extra io and analogs .
https://youtu.be/Mxr_4iRQsdQ

Re: UCCNC and Arduino

PostPosted: Wed Jan 04, 2023 9:43 am
by camivic
formantjim wrote:Hi Camivic I would be interested to see that sketch for the Modbus Arduino if you could share please?


Hi formantjim,
attached you can found the sketch and library used with Arduino in Slave Modbus function.
Ciao.

Re: UCCNC and Arduino

PostPosted: Thu Jan 05, 2023 1:22 am
by formantjim
Many thanks. Nicely commented out I will give it try at the weekend.
I'm looking to build a quick and simple Modbus tester for the various pieces of software my company use with our vendors instruments. It will be a simple battery powered device using an UNO with Analogue values coming in via 4 to 20mA or a potentiometer and be read by the UNO and displayed in other software.