584,858 active members*
4,592 visitors online*
Register for free
Login
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2017
    Posts
    143

    [TNGv2] API permissions problem

    I started playing around with some of the API stuff today and what you can do is pretty cool, but I am having a problem running the python demo's (all the test0x.py scripts). When I run those I get this dialog when PlaneCNC starts.



    If I hit ok on that button and go into settings I also get this error when I check Python.



    If I grant the "Computer\Users" group modify permissions that first error goes away, but I still have the second problem, which means I can run any Python scripts

    This is on a Windows 10 computer that is fully patched.

    This was of running a Python script is not so elegant either, as you end up with a python console window as well as the PlanetCNC window. I am not sure if it is possible, but maybe a nicer approach would be to be able to specify a script in the settings that would get spawned as a separate thread at startup. That would avoid having the Python console window and maybe solve the permission problems. The script I am using to start PlanetCNC subscribes to some topics on a MQTT Home Assistant server that controls things like an AirCompressor, Dust Extractor and Vacuum. I have got it so that when they get turn on buttons in PlanetCNC also change state. The buttons also work to turn things on/off if Python is available (ie. just run PlanetCNC by itself).

    Hopefully I am not completely missing the point of how all this is meant to hang together.

    Cheers
    Simon

  2. #2
    Join Date
    Mar 2017
    Posts
    1295

    Re: [TNGv2] API permissions problem

    Ok, I see that there is some things that need explanation.

    There are two different ways to use Python with TNG.

    1. Python script is used internally, Mostly for g-code generators and custom commands but some pretty advanced scenarios are possible. For this to work path to Python dll must be set in settings. If it is done correctly then "Check" button returns Python version number.
    There are some python scripts included in samples that can be used to test this feature. API also has folder "PythonInt" with examples for "Run Python Script" command.

    2. TNG is used by external application. For example Python scripts (examples are in API/Python folder) or some other way. Basically you control TNG from another process.In this case internal Python is disabled hence message that you got.
    There are two possible cases:
    A.) calling process connects to already running TNG (as extrnal process)
    B.) calling process starts its own TNG instance using one of Run commands (as in-process)

    In A) case not all commands are possible and communication is slower. This is not recommended for high intensity scenarios
    In B) case TNG runs in-process. You can basically build your own app and use TNG only as engine. DemoC# is good example of this.

    In your case Python creates TNG as in-process. TNG is probably installed in protected folder (C:/Program Files). TNG wants to read/write settings file. Because calling process is Python its permissions are used and TNG fails to read/write. You have two options:
    1. Run Python as admin (usually I run it from console that is run as administrator).
    2. Move TNG somewhere else. There is no reason that TNG is in "Program Files". You can simply copy/move files somewhere else without any worries. There are no dependencies or anything else preventing this.

  3. #3
    Join Date
    Jun 2017
    Posts
    143

    Re: [TNGv2] API permissions problem

    Ok that mostly makes sense. I tried a different approach this morning. Rather than having a python script start TNG I wrote it so that it can be started using a custom M code. The python script starts a thread and exits, but the thread it creates survives and does everything correctly. So what I have is an M code files and a py file.

    Code:
    M990.gcode
    (print,Start M990)
    (py,scripts/mqtt.py)
    (print,End M990)


    Code:
    mqtt.py
    import sys
    import time
    import ctypes
    import threading
    import paho.mqtt.client as mqtt
    import planetcnc
    
    
    # The callback for when the client receives a CONNACK response from the server.
    def on_connect(client, userdata, flags, rc):
      client.connected_flag = rc
      if rc==0:
        client.subscribe("+/stat/POWER")
        
    def on_message(client, userdata, message):
        print(message.topic + " " + str(message.qos) + " " + str(message.payload.decode("utf-8")))
        print(message.payload.decode("utf-8"))
        if message.topic=="garage_compressor/stat/POWER":
          if message.payload.decode("utf-8")=="ON":
            v = 1
          else:
            v = 0
          print("Setting _compressor")
          planetcnc.setParam("_compressor", v)
    
    def main():
      try:
        mqtt.Client.connected_flag = -1
        client = mqtt.Client("cnc")
        client.on_connect = on_connect
        client.on_message = on_message
        client.username_pw_set("username", "password")
    
        client.connect_async("x.x.x.x")
        client.loop_forever()
    
      except Exception as e:
        print("Exception: ", str(e))
    
    t = threading.Thread(target=main)
    t.start()
    With this as long as I remember to execute M990 once after starting TNG everything works perfectly. I may as well removed the print statements though as they don't get output anywhere visible.

    I know somewhere you mentioned you where going to add more event type at a later date, could you please think about including one for "TNG Start" and maybe "TNG End". Then I could have this code run automatically at TNG startup.


  4. #4
    Join Date
    Mar 2017
    Posts
    1295

    Re: [TNGv2] API permissions problem

    What does "Check" button say?

  5. #5
    Join Date
    Jun 2017
    Posts
    143

    Re: [TNGv2] API permissions problem

    If I start PlanetCNC directly the check button gives me v3.8.0:fa919fd, Oct 14 2019, 19:37:50.

    I only get the Python is Disabled message if I use a Python script to start TNG.

  6. #6
    Join Date
    Mar 2017
    Posts
    1295

    Re: [TNGv2] API permissions problem

    Is "File/Run Python Script" enabled or disabled?

  7. #7
    Join Date
    Jun 2017
    Posts
    143

    Re: [TNGv2] API permissions problem

    It is enabled if I run TNG directly, disabled if started from a Python script.

    Here is a short video of what I have now got it to do using the compressor.

    https://1drv.ms/v/s!Aqjw1nXr8Ho8jgbtREmuPc_kfZ2s

  8. #8
    Join Date
    Mar 2017
    Posts
    1295

    Re: [TNGv2] API permissions problem

    Quote Originally Posted by theRat View Post
    I may as well removed the print statements though as they don't get output anywhere visible.
    Print statements are printed to "Output Tab" in Utilities panel. See user manual, chapter 2.3.9.1 Output Tab (page 58)

  9. #9
    Join Date
    Jun 2017
    Posts
    143

    Re: [TNGv2] API permissions problem

    Once I start a new thread in the script, that threads output no longer goes to the output tab. I am no python expert (this is the first week of me using python) so I don't know if there is a way to change this.

    [Edit] Actually it will print from main() but not from the callback functions [/Edit]

  10. #10
    Join Date
    Mar 2017
    Posts
    1295

    Re: [TNGv2] API permissions problem

    Try planetcnc.print("blabla") instead of just print("blabla").

    planetcnc.print is a command to print in TNG output window.
    print is Python statement to print to standard IO. TNG tries to redirect this to its own output window which may not always work.

  11. #11
    Join Date
    Jun 2017
    Posts
    143

    Re: [TNGv2] API permissions problem

    Thank you. That works perfectly!

Similar Threads

  1. Replies: 1
    Last Post: 11-24-2017, 03:56 AM
  2. Predator Save Directory Permissions.
    By Machineit in forum BobCad-Cam
    Replies: 2
    Last Post: 09-10-2013, 07:32 PM
  3. daewoo puma 12lb tape format problem/parameter problem
    By robb12877 in forum Daewoo/Doosan
    Replies: 0
    Last Post: 08-25-2011, 06:13 AM
  4. X4 and Windows 7 non-admin permissions
    By mbutler in forum Mastercam
    Replies: 1
    Last Post: 05-14-2011, 05:30 AM
  5. Multiple users without permissions?
    By GMTool in forum BobCad-Cam
    Replies: 3
    Last Post: 05-19-2008, 07:22 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •