RobotStudio event

Reading Coordinates from txt file

Hello to all..
an error occurs every time i execute the rapid code.
Rapid code to extract coordinates data from a txt file that is placed inside the robot controller Home folder of controller.

error occurs when the code is executed, all the values of line 1 are saved in coord "string" like "123 234 345; 456 567 678; 789 891 122; 133 144 155;\0D";
i tried add optional arguments of \Delimetr or Remove CR, it ll give an error "can not change command"  or "unable to add command due to syntax error".
any hint what could be the reason? as i need 3 values at a time for pos1. 
regards..

Best Answers

  • SomeTekk
    SomeTekk
    Answer ✓
    I have not tried what you are attempting, with that said I suggest checking the text below about delimiters. It appears you may need to integrate hex code in what you are doing.

    Delimiters
    Data type: string
    A string containing the delimiters to use when parsing a line in the file or serial
    channel. By default the file is read line by line and the line-feed character (\0A) is
    the only delimiter considered by the parsing. When the \Delim argument is used,
    any character in the specified string argument plus by default line-feed character
    will be considered to determine the significant part of the line.
    To specify non-alphanumeric characters, use \xx, where xx is the hexadecimal
    representation of the ASCII code of the character (example: TAB is specified by
    \09).

    It seems you may need to do some string operations. Hopefully, people with advanced RAPID knowledge will chime in.
  • lemster68
    lemster68 ✭✭✭
    Answer ✓
    See here for good examples of opening, reading and parsing through files:
    https://www.robot-forum.com/blog/entry/4-another-program-that-writes-a-program/
    Lee Justice

Answers

  • It is likely it is because the data structure of the pos data type has three discreet components — x, y, z. 

    It'll probably take some code rework, but RAPID may need to assign the values one component at a time:

    pos1.x:=(your data here)
  • thanks for help sir.
    Error generates at the command 
    coord := ReadStr(file), i tried to add delim and remove CR to the ReadStr command,  "to take 3 values" separated by ; ..
    is there some special format to write the data in txt file like 123,234,345; etc
  • what is the error?
  • @graemepaulin sir using simply coord := ReadStr(file) generates no error. when try to use delim or RemoveCR, it will pop up a window with message.....
    either this: Can not change the command
    Or : Command could not be added due to syntax error.

  • So the issue is you need to extract the x, y, z values out of the string variable where you now have got the whole first line from the text file?
  • yes sir. i can extract the data just need to add delim and Remove CR. which adding them cause an error" syntax error"
  • as per the manual i tried adding delim like:
    coord := ReadStr(file\Delim:="\0d"); 
    it gives can not add instruction due to syntax error.
    the same happens for 
    coord:= ReadStr(file\Remove CR); 
  • Hi LordSikander, 

    You need to format the string so it is exactly the same as it would be if you wrote it in RAPID;
    the string should read "[123,234,345]" before it is converted. Below is a simple way to do it for the first pos in the file. If you want to extract multiple positions from one file it will take some extra work to separate them first.

    StrMap() repalces the spaces " " with commas "," and then the brackets are added either side.

    PROC Main()
    	
    	Open "HOME:"\File:="5.txt", file, \Read;
    	coord := ReadStr(file, \Delim:=";");
    	
    	coord := "[" + StrMap(coord, " ", ",") + "]";
    	
    	Close file;
    	ok := StrToVal(coord, pos1);
    	
    ENDPROC

    Good Luck, 

    Harry
  • thanks for such a valuable reply sir @Forge_Engineering.. 
    i give it a try and most of the issues are resolved. 
    just one point, i have data in txt file in this sequence:
    123,234,345;
    456,567,678; etc
    now in a while loop 
    for first line with a delim of ";" in command readstr := StrRead(....\Delim:= ";",\RemoveCR); the rapid code picks the coordinates and store it in string as [123,234,345] 
    now second time when while loop executes, instead of picking coordinates from 2nd line , it gives an empty space in program data "readstr" variable value..
    is the sequence of data in txt file is correct or i have to modify this like
    123,234,345; 456,567,678;
    regards..

  • thank you soo much sir @lemster68.. this helps me a lot.
    with Delim of ";" and the coordinates pattern 123,234,345;456,567,678; . the code grab first coordinate then go to second in a loop.
    now i just have to add multitasking code to read coordinates data for next motion point in parallel.