C# Script sent via 30003 port

Greetins UR Community,

I am trying to write an URscript on C# which will later be sent to the UR5 via port 30003.

A quick example of a program I wrote is the following:
string message = “def program():”;
message = message + “\r\nset_configurable_digital_out(0,True)”;
message = message + “\r\nsleep(2)”;
message = message + “\r\nset_configurable_digital_out(0, False)”;
message = message + “\r\nhalt\r\nend\r\n”;

This is a very simple program which sets an output to to 1 for two seconds and then sets it back to 0

I can run this program perfectly. However, the app I am currently developing is a little bit more sophisticated and I need a socket communication for various reasons. I studied the script manual but I was not successful into getting this done.

This is a short part of the code:

string message = “def program():”;
message = message + “\r\nset_configurable_digital_out(0, True)”;
message = message + “\r\nglobal var_1 = socket_open(192.168.3.250,10)”;
message = message + “\r\nwhile(var_1 == False):”;
message = message + “global var_1 = socket_open(192.168.3.250,10)”;
message = message + “sleep(0.3)”;
message = message + “end”;
message = message + “\global var_2 = socket_read_ascii_float(6)”;

I am certain this is not the way to do it, but I haven’t found any solution (unlike the former, this one does not run on C#).

Finally, I also struggled with setting a timer:
string message = “def program():”;
message = message + “\r\nset_configurable_digital_out(0, True)”;
message = message + “\r\ntimer_1 = 0”;
message = message + “\r\ntimer_1_is_counting = True”;
message = message + “\r\nsleep(2)”;
message = message + “\r\ntimer_1_is_counting = False”;
message = message + “\r\nset_configurable_digital_out(0, False)”;
message = message + “\r\npopup(timer_1)”;
message = message + “\r\nhalt\r\nend\r\n”;

This program runs but timer_1 is equal to 0 instead of 2.

Any help would be much appreciated.

Hi @pmonforte77 and welcome to the Forum,

  1. Did you verify that the script code actually works? I often find it easier to write the raw script code and test it directly on the controller/simulator first before sending it from an external machine/daemon. This way you don’t have 2 layers of code to debug at the same time.

  2. If your code-snippets are copied directly from your program it seems like you are missing a few \r\n here and there.

  3. In your last example it looks like your are missing the actual timer thread which does the acutal counting.

  thread Timer_Thread():
    while (True):
      if (timer_1_is_counting):
        timer_1 = timer_1 + get_steptime()
      end
      sync()
    end
  end
  run Timer_Thread()

Hope it helps.