Hello Guest it is December 06, 2019, 10:30:46 PM

Author Topic: LUA Script error in PLC, PoKeys  (Read 1882 times)

0 Members and 1 Guest are viewing this topic.

Offline thosj

*
  •  368 368
    • View Profile
LUA Script error in PLC, PoKeys
« on: March 07, 2017, 05:05:11 PM »
I am trying to get my PoKeys 57E working with my analog FRO pot. I downloaded a LUA script from PoKeys, followed their instructions, and get an error exiting the screen editor after editing the PLC.

Saved downloaded script in C:\Mach4hobby, edited it with script editor only for Pokeys device name and pin number, saved.

Added line to PLC to run the file.

Get error: Syntax error during pre-compliation [string ""]:511: unexpected symbol near char(226)

My first attempt at customizing and trying to get my PoKeys 57 working my buttons. Not a good first attempt, so far!!

I tried to upload screenshots but can't seem to do it.

Ideas?

Tom
« Last Edit: March 07, 2017, 05:10:36 PM by thosj »
Re: LUA Script error in PLC, PoKeys
« Reply #1 on: March 07, 2017, 05:49:42 PM »
When I run it I get a debug message:

attempt to perform arithmetic on local 'analog' (a string value)

change it to a number and try it then

Offline thosj

*
  •  368 368
    • View Profile
Re: LUA Script error in PLC, PoKeys
« Reply #2 on: March 07, 2017, 06:05:05 PM »
You lost me, I know almost nothing about LUA. This is my first attempt and I downloaded the lua file from PoKeys.

Change analog to a number? What number? 1, 1000?

And how do you "run" it? In my case, the lua file is being run by the PLC with the line I added as shown in one of the screenshots. I get the error shown, something about a symbol near character 226!!

Sorry!!
Re: LUA Script error in PLC, PoKeys
« Reply #3 on: March 07, 2017, 06:16:34 PM »
Not the expert here but you passed the Analog variable which contained a string of two letters a '4' and a '5' representing "45".  When passed to the function where you do the calculations you are trying to divide a a string by some numbers.  You need to convert "45" to 45.

alog=toNumber(Analog)

alog will now contain the number 45 and you can do calculations with it.

HTH

RT

Offline thosj

*
  •  368 368
    • View Profile
Re: LUA Script error in PLC, PoKeys
« Reply #4 on: March 07, 2017, 06:34:18 PM »
OK, I change the "45" to 45, done?

Or do I leave "45" as "45" and use your code to create a new variable, alog, as in alog=toNumber(Analog)

Maybe you could just edit my script like it should be, post it back up here, and maybe I can learn something.

OH, and to the unwashed, it looks to me like analogPIN is what's set to "45", not analog, but perhaps I don't get that either

What I don't get is why PoKeys posted this code on their blog for dopes like me to try to use. I guess to force a learning experience on us, those clever bastards!!!

Thanks for the help!

Tom
« Last Edit: March 07, 2017, 06:36:55 PM by thosj »
Re: LUA Script error in PLC, PoKeys
« Reply #5 on: March 07, 2017, 07:04:28 PM »
I feel your pain.  Understanding/following code for the uninitiated is really confusing and Lua lets you do anything you want and assumes you know what you are doing so it is very hard to debug since it does very little type checking.

you are correct that "45" is analogPin, when it is passed to ReadRegister it is also named analogPin.  But ReadRegister could just as easily called it aPin.  All Read register knows is that it is supposed to get a string.  ReadRegister  looks up the value stored in a register named "45" and returns a valueString (return mc.mcRegGetValueString(hreg)) to analogVal.  It then sends that string of characters to setFRO:
analogVal = ReadRegister(device, analogPin) --Save analog register value in variable
SetFRO(analogVal) -- Set FRO value in % 

setFRO takes analogVal and calls it analog now.  Still a string, convert it to a number

Code: [Select]
--Function to read value from analog register
function ReadRegister(device, analogPin)
    local inst = mc.mcGetInstance()
    local hreg = mc.mcRegGetHandle(inst, string.format("%s/Analog input %s", device, analogPin))
    return mc.mcRegGetValueString(hreg)
end

--Function to set FRO value
function SetFRO(analog)
    local percent = toNumber(analog)/1*250 --calculate percentage from 0% to 250%  *****changed here******
    local inst = mc.mcGetInstance()
    mc.mcCntlSetFRO(inst, percent)
end

--Main
local device = "PoKeys" --Change this to the name of your PoKeys device
local analogPin = "45" --Analog input pin number

analogVal = ReadRegister(device, analogPin) --Save analog register value in variable
SetFRO(analogVal) -- Set FRO value in %

HTH

RT

Offline thosj

*
  •  368 368
    • View Profile
Re: LUA Script error in PLC, PoKeys
« Reply #6 on: March 08, 2017, 06:08:14 AM »
OK, I'll try that later today, thanks. If it works I'll be all over PoKeys to get them to post the proper code for this. THEN I need to do the same thing for SRO, get an analog pot to do Speed Rate Override. I sure wish this stuff was documented somewhere better, if it is, I sure can't find it. In Mach3  the script seemed easier to understand. I have a hard time reading even a small lua script and making sense of it. If I wrote that one we're working on I'd have put a comment behind every damn line so later I'd know what I did

Oh, BTW, how do I get that "code" window, like in your last post, in the forum? I'm going to be needing to know that!!

Thanks again, I'll be back later to comment if my pot works now.

Tom

Offline thosj

*
  •  368 368
    • View Profile
Re: LUA Script error in PLC, PoKeys
« Reply #7 on: March 08, 2017, 05:20:35 PM »
I made your edit, one line, right?

local percent = toNumber(analog)/1*250 --calculate percentage from 0% to 250%  *****changed here******

I get the exact same error exiting the screen editor after adding the line to run the file.

I've attached the screenshot of the error again for ref.

Maybe I shouldn't be adding this at the bottom of the PLC file, maybe it should be in the Screen Start?

It's amazing this is this hard!! I sure wish I knew more about how all this really works, Screen Load, Screen Unload, PLC, on and on.

Tom

Offline thosj

*
  •  368 368
    • View Profile
Re: LUA Script error in PLC, PoKeys
« Reply #8 on: March 11, 2017, 04:42:52 PM »
To follow up in case anyone else reads this.

The whole issue was I copied the dofile line from Pokeys web site and pasted it into the PLC script and that somehow corrupts the text. I hand typed the dofile line in the PLC script and it worked fine. So the PoKeys downloaded lua file was fine as it was, the "45", script or not, worked fine.

The Mach support guys, James and Brett, fixed me up on this, so thanks to them.

Offline Chaoticone

*
  • *
  •  5,633 5,633
  • Precision Chaos
    • View Profile
Re: LUA Script error in PLC, PoKeys
« Reply #9 on: March 11, 2017, 09:18:03 PM »
 :)

No problem, glad you got it sorted.
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!