I am trying to define a function in which I have a thread that is checking whether a modbus connection is connected or not. When using the function in my program node I get the following error message: Compile error: An expression was found which produces a result that is not used
def my_function():
global modbus_conn = False
thread thread_modbus_conn():
while(True):
modbus_conn = rpc.ModbusHandler.getModbusConnection()
if(modbus_conn == False):
popup("Not connected to modbus. Stopping program", warning=True, blocking=False)
halt
end
sleep(1)
end
end
thread_handler_modbus_conn = run thread_modbus_conn()
end
I have looked on the forum and this is the most accurate representation of what I am aiming for.
I just tested a similar construction with another function call instead of your RPC call. that worked just fine. Can you elaborate on your rpc server, is your method called “ModbusHandler.getModbusConnection”? And does your code work if you substitute the call with general purpose URScript code?
another thing to note is that your sample differs from the sample you refer to in the sense that your will run ones, while the other will continue to evaluate the connection
The RPC method is called “MosbusHandler.getModbusConnection()” and returns a boolean. The code works with RPC call if I remove the thread and popup pops up as expected when there is no connection and does not pop up, when there is a connection to the modbus server.
I have edited the code to loop.
I have also tried to remove all content in the thread, so all that is run is:
thread thread_modbus_conn():
end
thread_handler_modbus_conn = run thread_modbus_conn()
I have defined a new function with the same funcitonality and now the function is working. I don’t know what the problem was, but it seems fixed now. Thank you for the help