Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    Passing variables to DSF-Python

    Scheduled Pinned Locked Moved Solved
    DSF Development
    3
    5
    356
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • oozeBotundefined
      oozeBot
      last edited by

      Can @chrishamm or someone give me a few pointers on how to pass variables to DSF-Python using custom mCodes? I would have assumed it would be like MXXXX V{var.localVar} but DSF-Python reads parameter V as "var.localVar" instead of its value.

      Am I missing something simple? Or is there a better way? Thanks

      chrishammundefined 1 Reply Last reply Reply Quote 0
      • oozeBotundefined oozeBot marked this topic as a question
      • chrishammundefined
        chrishamm administrators @oozeBot
        last edited by

        @oozebot You can use the EvaluateExpression command to get the value of expressions. In your case the parameter of MXXXX will have a string value and a property isString which is false meaning that you're dealing with an expression.

        I will consider (optional) automatic expression evaluation for the Flush command in v3.5. DSF uses that same mechanism internally, too.

        Duet software engineer

        oozeBotundefined 1 Reply Last reply Reply Quote 0
        • oozeBotundefined
          oozeBot @chrishamm
          last edited by

          @chrishamm Thanks - but am going to have to plead ignorant and ask for a touch more guidance as I can't find any examples or documentation other than what what auto-generated here: https://duet3d.github.io/DuetSoftwareFramework/api/DuetControlServer.Commands.EvaluateExpression.html

          Could you assist by expanding the following snippet on the Python side using EvaluateExpression to read the value of var.localVar? Thanks!

          gCode:

          var.localVar = "testing"
          M1234 V{var.localVar}
          

          Python

          cde = intercept_connection.receive_code()
            if cde.type == CodeType.MCode:
              if cde.majorNumber == 1234:
                tVar = ParseParam(cde.parameter("V"))
          
          Falcounetundefined 1 Reply Last reply Reply Quote 0
          • Falcounetundefined
            Falcounet @oozeBot
            last edited by Falcounet

            @oozebot You can use the EvaluateExpression command from DSF in Python in that way :

            from dsf.connections import CommandConnection
            from dsf.commands.basecommands import evaluate_expression
            from dsf.commands.codechannel import CodeChannel
            
            def send_simple_command():
                command_connection = CommandConnection(debug=True)
                command_connection.connect()
            
                try:
                    expression = "{var.localVar}"
                    res = command_connection.perform_command(evaluate_expression(CodeChannel.HTTP, expression))
                    print(f"Evaluated expression: {res}")
                finally:
                    command_connection.close()
            
            
            if __name__ == "__main__":
                send_simple_command()
            

            You can get the channel you are intercepting the Gcode from using cde.channel instead of CodeChannel.HTTP in the above example.

            oozeBotundefined 1 Reply Last reply Reply Quote 1
            • oozeBotundefined
              oozeBot @Falcounet
              last edited by

              @falcounet Thank you! This was just the nudge we needed. I was trying to import evaluateexpression, not evaluate_expression - doh!

              1 Reply Last reply Reply Quote 0
              • oozeBotundefined oozeBot has marked this topic as solved
              • First post
                Last post
              Unless otherwise noted, all forum content is licensed under CC-BY-SA