# Receive robtarget from String

**URL:** <https://tech-community.robotics.abb.com/t/receive-robtarget-from-string/7126>\
**Category:** RobotStudio\
**Tags:** rapid, socket, labview, string-to-robtarget\
**Created:** [April 29, 2016, 7:50pm UTC](https://tech-community.robotics.abb.com/t/receive-robtarget-from-string/7126 "2016-04-29T19:50:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![SteveMob](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/stevemob/32/1600_2.png) [@SteveMob](https://tech-community.robotics.abb.com/u/SteveMob)\
**Post date:** [April 29, 2016, 7:50pm UTC](https://tech-community.robotics.abb.com/t/receive-robtarget-from-string/7126/1 "2016-04-29T19:50:15Z")

</div>

Hello,

I am doing some Robot/LabVIEW communication. I want LV to send a string for a robtarget. This string will essentially say:

“CONST robtarget p10:=[[461.29,144.16,221.59],[0.29728,-0.143906,0.942789,0.0454265],[0,0,-1,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]];” Defining CONST robtarget, giving it a name, position in space with tool and axes configuration data.

I need RAPID to take that string and convert it into a robtarget. Is this possible? I have this code:  
" _MoveJ RelTool(Home,read\_positionX,read\_positionY,read\_positionZ,\Rx:=orientationRX,\Ry:=orientationRY,\Rz:=orientationRZ),v200,z0,tool0,\WObj:=wobj0;"_

But this will only take positions in space, and tool orientation. Is there a way to accept an entire string as above, and convert it to a target declaration?  
I tried doing: " _VAR robtarget vartarget:=[[positionx,positiony,positionz],[orientq1,orientq2,orientq3,orientq4],[cf1,cf4,cf6,cfx],[9E9,9E9,9E9,9E9,9E9,9E9]];"_ and declared all those as number variables, and those variables take numbers sent from LV, but that didn’t really work either…  
I also looked into byte to string but couldn’t figure much out…

Thanks for any help!  
SM

---

<div class="post-metadata">

**Author:** ![PerSvensson](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/persvensson/32/1040_2.png) [@PerSvensson](https://tech-community.robotics.abb.com/u/PerSvensson)\
**Post date:** [April 30, 2016, 7:22am UTC](https://tech-community.robotics.abb.com/t/receive-robtarget-from-string/7126/2 "2016-04-30T07:22:38Z")

</div>

Hi  
You need to convert back and forth I usually separate the values with a ; character. Here is an example how you could do it. Haven’t included all the data but I think you will get an idea also you should take in to consideration error handling when converting string to values. Depending on how many decimals you want in your values (in my case 0) you may have to split the robtarget structure into separate strings because a string could only hold 80 characters.

 ![image.png](https://europe1.discourse-cdn.com/flex005/uploads/robotics1/original/1X/1d9eb0a3846662c2859b0b09ec4995b8f5e72524.png)  
 ![image.png](https://europe1.discourse-cdn.com/flex005/uploads/robotics1/original/1X/1d9eb0a3846662c2859b0b09ec4995b8f5e72524.png)

---

<div class="post-metadata">

**Author:** ![SteveMob](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/stevemob/32/1600_2.png) [@SteveMob](https://tech-community.robotics.abb.com/u/SteveMob)\
**Post date:** [May 2, 2016, 1:51pm UTC](https://tech-community.robotics.abb.com/t/receive-robtarget-from-string/7126/3 "2016-05-02T13:51:41Z")

</div>

Per,

Thank you so much for the help, it definitely got me moving along better than I was. So now I am slightly confused on where I need the incoming data to parse to. This is my connection/data receiving/parsing code:

**PROC main()**  
**data:=TargetToString(myRobtarget);**  
**TPWrite data;**  
**pTarget:=StringToTarget(data);**  
**MoveJ Home,v150,z0,tool0;**  
**WaitTime 2;**

**SocketClose client\_socket;**  
**Socketcreate client\_socket;**  
**SocketConnect client\_socket,“192.168.125.5”,3333;**  
**SocketSend client\_socket\Str:=“Connection Successful”\NoOfBytes:=21;**  
**! Clear the port of any data**  
**!SocketReceive client\_socket\RawData:=parse\_data\Time:=1;**  
**IF ERRNO=ERR\_SOCK\_TIMEOUT THEN**

**ENDIF**

**ClearRawBytes parse\_data;**  
**i:=1;**  
**WHILE i=1 DO**  
**peek\_value:=SocketPeek(client\_socket);**  
**IF peek\_value\>=1 THEN**  
**ClearRawBytes parse\_data;**  
**SocketReceive client\_socket\RawData:=parse\_data\Time:=25;**  
**bytes\_available:=RawBytesLen(parse\_data);**  
**SocketSend client\_socket\RawData:=parse\_data;**  
**IF bytes\_available\>=1 THEN**  
**UnpackRawBytes parse\_data,1,command\Hex1;**  
**IF 42=command THEN**  
**! Read a position.**  
**! UnpackRawBytes parse\_data\Network,2,read\_positionX\Float4;**  
**! UnpackRawBytes parse\_data\Network,6,read\_positionY\Float4;**  
**! UnpackRawBytes parse\_data\Network,10,read\_positionZ\Float4;**  
**! ! Read an orientation.**  
**! UnpackRawBytes parse\_data\Network,14,orientationRX\Float4;**  
**! UnpackRawBytes parse\_data\Network,18,orientationRY\Float4;**  
**! UnpackRawBytes parse\_data\Network,22,orientationRZ\Float4;**

**! Moving relative to the tool coordinates, absolute move from position “Home” (all axes at 0 degrees)**  
**!MoveJ RelTool(Home,read\_positionX,read\_positionY,read\_positionZ,\Rx:=orientationRX,\Ry:=orientationRY,\Rz:=orientationRZ),v200,z0,tool0,\WObj:=wobj0;**

**!Read a position (x,y,z).**  
**UnpackRawBytes parse\_data\Network,2,robtarget.pos.x\Float4;**  
**UnpackRawBytes parse\_data\Network,6,robtarget.pos.y\Float4;**  
**UnpackRawBytes parse\_data\Network,10,robtarget.pos.z\Float4;**

**! Read tool orientation (quaternion angles).**  
**UnpackRawBytes parse\_data\Network,30,robtarget.orient.q1\Float4;**  
**UnpackRawBytes parse\_data\Network,34,robtarget.orient.q2\Float4;**  
**UnpackRawBytes parse\_data\Network,38,robtarget.orient.q3\Float4;**  
**UnpackRawBytes parse\_data\Network,42,robtarget.orient.q4\Float4;**

**!Read axes configuration (axis1,axis4,axis6,axisx)(“x” can refer to axis 5 at times).**  
**UnpackRawBytes parse\_data\Network,46,robtarget.confdata.cf1\Float4;**  
**UnpackRawBytes parse\_data\Network,50,robtarget.confdata.cf4\Float4;**  
**UnpackRawBytes parse\_data\Network,54,robtarget.confdata.cf6\Float4;**  
**UnpackRawBytes parse\_data\Network,58,robtarget.confdata.cfx\Float4;**

**!MoveJ vartarget,v100,z10,tool0,\WObj:=wobj0;**  
**WaitTime 2;**  
**ELSE**  
**TPWrite "Unhandled command: "\Num:=command;**  
**ENDIF**  
**ENDIF**

**IF ERRNO\>0 THEN**  
**IF ERRNO=ERR\_SOCK\_TIMEOUT THEN**

**ELSE**  
**i:=0;**  
**ENDIF**

**ENDIF**  
**WaitTime 1;**  
**ENDIF**  
**ENDWHILE**  
**ENDPROC**

I included what you posted above, below all of this code. What do I need to change in order to parse the incoming string? In other words, how would I receive a string from LabVIEW, and where what variables do I need to place the data to?

Thanks for all the help!  
SM

---

<div class="post-metadata">

**Author:** ![PerSvensson](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/persvensson/32/1040_2.png) [@PerSvensson](https://tech-community.robotics.abb.com/u/PerSvensson)\
**Post date:** [May 2, 2016, 4:59pm UTC](https://tech-community.robotics.abb.com/t/receive-robtarget-from-string/7126/4 "2016-05-02T16:59:15Z")

</div>

Hi  
I usually do the communication in a background task and yield to the foreground when new data has arrived.  
Here is an example of the code that almost use every time as the base and then just do what ever I need to do in the ParseMessage routine.

In my cases the robot is usually the server

The RobOS() is just for testing if I the code is running on a real controller or a Virtual controller

 ![image.png](https://europe1.discourse-cdn.com/flex005/uploads/robotics1/original/2X/4/4a09f097948b87b964643c3b0733e67158427f8d.png)  
 ![image.png](https://europe1.discourse-cdn.com/flex005/uploads/robotics1/original/2X/4/4a09f097948b87b964643c3b0733e67158427f8d.png)

---

<div class="post-metadata">

**Author:** ![SteveMob](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/stevemob/32/1600_2.png) [@SteveMob](https://tech-community.robotics.abb.com/u/SteveMob)\
**Post date:** [May 2, 2016, 5:13pm UTC](https://tech-community.robotics.abb.com/t/receive-robtarget-from-string/7126/5 "2016-05-02T17:13:37Z")

</div>

Ok thanks a ton!

One more question, how should I go about with the error handling? I’ve never really done much with the errors. I want the robot to send a message to LB via socket to say it arrived at position “X,Y,Z…”. And if it doesn’t arrive at that position for whatever reason (joint limit, target out of reach, collision, etc.) I don’t want the RAPID program to stop, but rather send a message to LV saying it didn’t get to the target for reason of Error #“…”, but have the RAPID program wait on “standby” for LV to tell it what to do next. How could I go about doing that?

Thanks for all your help!  
SM

---

<div class="post-metadata">

**Author:** ![PerSvensson](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/persvensson/32/1040_2.png) [@PerSvensson](https://tech-community.robotics.abb.com/u/PerSvensson)\
**Post date:** [May 2, 2016, 6:56pm UTC](https://tech-community.robotics.abb.com/t/receive-robtarget-from-string/7126/6 "2016-05-02T18:56:00Z")

</div>

You can calculate if the robot is going to be able to reach a robtarget by using CalcJointT before you actually move to that position. It’s quite well described in the manual,

---

<div class="post-metadata">

**Author:** ![SteveMob](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/stevemob/32/1600_2.png) [@SteveMob](https://tech-community.robotics.abb.com/u/SteveMob)\
**Post date:** [May 2, 2016, 7:30pm UTC](https://tech-community.robotics.abb.com/t/receive-robtarget-from-string/7126/7 "2016-05-02T19:30:47Z")

</div>

Ok I’ll look into it, thanks. Do you have any ideas on how to keep the RAPID program running even when it encounters an error? I need the RAPID program to be on like “standby” and sends the error message to LV, and then LV will tell RAPID what to do next. Any Ideas?

Thanks,  
SM

---

<div class="post-metadata">

**Author:** ![PerSvensson](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/persvensson/32/1040_2.png) [@PerSvensson](https://tech-community.robotics.abb.com/u/PerSvensson)\
**Post date:** [May 2, 2016, 7:57pm UTC](https://tech-community.robotics.abb.com/t/receive-robtarget-from-string/7126/8 "2016-05-02T19:57:04Z")

</div>

Then you need to take care of it in Error handlers that you define at the end of each FUNC or PROC. If you know what errors that could happen you a can compare it with ERRNO and take the actions you need. There are a couple of instructions related to error handling like retry(try them same instruction that failed again), trynext (try the instruction after the one that failed),raise (call the error handler in the method that called the failing method). It’s also possible to define your own ERRNO.

---

<div class="post-metadata">

**Author:** ![SteveMob](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/stevemob/32/1600_2.png) [@SteveMob](https://tech-community.robotics.abb.com/u/SteveMob)\
**Post date:** [May 2, 2016, 8:33pm UTC](https://tech-community.robotics.abb.com/t/receive-robtarget-from-string/7126/9 "2016-05-02T20:33:55Z")

</div>

I am terrible at this error handling stuff… Is there a joint limit and/or target out of reach error handler? I found ERR\_OUTSIDE\_REACH, but couldn’t find any documentation on it or how it works. I also tried to make my own error handler, ERR\_JOINTLIM and get it to send LV a message when CalcJointT encounters this error, but I am doing something wrong because it did not do what I wanted it to do. This is my code for the JOINTLIM error handler:

**VAR errstr JointLim:=“Robot will reach joint limit!”;**  
**VAR errstr OutReach:=“This target is out of reach for the robot arm!”;**  
**VAR jointtarget jointpos1;**  
**VAR errnum ERR\_JOINTLIM:=1;**  
**.**  
**.**  
**.**  
**IF ERRNO=ERR\_OUTSIDE\_REACH THEN**  
**SocketSend client\_socket\Str:=OutReach;**  
**!TRYNEXT;**  
**ENDIF**

**jointpos1:=calcjointt(myRobtarget,tool0\WObj:=wobj0\ErrorNumber:=ERR\_JOINTLIM);**  
**IF ERRNO=ERR\_JOINTLIM THEN**  
**SocketSend client\_socket\Str:=JointLim;**  
**!TRYNEXT;**  
**ENDIF**

**MoveJ myRobtarget,v100,fine\Inpos:=inpos50,tool0,\WObj:=wobj0;**  
With this code, it won’t even send the message to the client. I need it to send a message to the client, and the RAPID program to just sit there until LV tells it something else to do. But so far it will just hault the program and say throw out the errors. Could it be that since joint limit and target out of reach are “fatal” errors that you can’t use an error handler with them?  
I also can’t seem to get the “TryNext” to work, but 1st things 1st, the error handlers… Any ideas?

Thanks,  
SM

---

<div class="post-metadata">

**Author:** ![Asad](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Asad](https://tech-community.robotics.abb.com/u/Asad)\
**Post date:** [May 23, 2019, 12:09pm UTC](https://tech-community.robotics.abb.com/t/receive-robtarget-from-string/7126/10 "2019-05-23T12:09:43Z")

</div>

hey man need a little help, so basically i am a real noob in this stuff but i need to pass the course :D.. I have to establish a communication between robot and laptop using visual studio C#. i have creating a client app on VS. just a basic program having connect and send buttons along with ip etc. what i want to do is to send string from that client e.g [[95,180,270],[1,-0.143906,0.942789,0.0454265],[0,0,-1,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]] or something similar and i want from robotstudio to receive this string and adjust to that postition, or use this value as a robottarget and adjust its position accordingly but i have no idea how to do it, i get so many errors in anything i try. i saw ur post and tried to do it as you said but i targettostring wasnt recognised in RS and was labeled as target identifier not found. please help any code anything would be helpful

i just have basic program in RS for socket communication. it looks like this  
MODULE Module1

VAR socketdev serversocket;  
VAR socketdev clientsocket;  
VAR string data;

PROC main()

SocketCreate serversocket;  
SocketBind serversocket,“127.0.0.1”,8910;  
SocketListen serversocket;

SocketAccept serversocket,clientsocket,\Time:=WAIT\_MAX;  
SocketReceive clientsocket\str:=data;  
SocketSend clientsocket\str:=“Command Received”;

SocketClose clientsocket;  
SocketClose serversocket;

ERROR  
IF ERRNO=ERR\_SOCK\_TIMEOUT THEN  
RETRY;

ELSEIF ERRNO=ERR\_SOCK\_CLOSED THEN

SocketClose clientsocket;  
SocketClose serversocket;  
SocketCreate serversocket;  
SocketBind serversocket,“127.0.0.1”,8910;  
SocketListen serversocket;  
SocketAccept serversocket,clientsocket,\Time:=WAIT\_MAX;

RETRY;

ELSE  
stop;

ENDIF

ENDPROC

ENDMODULE

---

<div class="post-metadata">

**Author:** ![lemster68](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/lemster68/32/1945_2.png) [@lemster68](https://tech-community.robotics.abb.com/u/lemster68)\
**Post date:** [May 23, 2019, 12:25pm UTC](https://tech-community.robotics.abb.com/t/receive-robtarget-from-string/7126/11 "2019-05-23T12:25:19Z")

</div>

Read the code posted once more, TargettoString is a routine in that code. It is unidentified because you mistyped the name, or just left it out.

---

<div class="post-metadata">

**Author:** ![A\_Mantzios](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@A\_Mantzios](https://tech-community.robotics.abb.com/u/A_Mantzios)\
**Post date:** [September 23, 2019, 9:14am UTC](https://tech-community.robotics.abb.com/t/receive-robtarget-from-string/7126/12 "2019-09-23T09:14:02Z")

</div>

> [@PerSvensson](#):
>
> Hi  
> You need to convert back and forth I usually separate the values with a ; character. Here is an example how you could do it. Haven’t included all the data but I think you will get an idea also you should take in to consideration error handling when converting string to values. Depending on how many decimals you want in your values (in my case 0) you may have to split the robtarget structure into separate strings because a string could only hold 80 characters.
> 
> ![image.png](https://europe1.discourse-cdn.com/flex005/uploads/robotics1/original/1X/1d9eb0a3846662c2859b0b09ec4995b8f5e72524.png)

Hi Per,

I have applied your solution on splitting the string and it works great with the exception that for posX which would be my trans.x position it always gives me a value of 0. I cant to seem to find the reason, would you be able to assist? Many thanks in advance.

Kind Regards  
Alex
