Passing variables to DSF-Python
-
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
-
-
@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 propertyisString
which isfalse
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. -
@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"))
-
@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 ofCodeChannel.HTTP
in the above example. -
@falcounet Thank you! This was just the nudge we needed. I was trying to import evaluateexpression, not evaluate_expression - doh!
-