core

#### Sample Code for Read Line

The sample code below should be entered inside the Edit window's Command mode and not inside the File I/O dialog box.

Consider this example that reads in a line from a data file one line at a time until the FILE/READ_LINE command encounters an empty line. The measurement routine then displays the resulting block of text and quits.

V1=FILE/EXISTS,test.txt

IF/V1<>0

COMMENT/OPER,Able to read from data file. Click OK to continue.

ASSIGN/V3=""

FPTR=FILE/OPEN,D:\Program Files\pcdmis35\test.txt,READ

DO/

V2=FILE/READLINE,FPTR,{LINE}

ASSIGN/V3=V3+LINE

COMMENT/OPER,"The current value of variable V3 is:

,V3

UNTIL/V2=="EOF"

FILE/CLOSE,FPTR

COMMENT/OPER,"The block of text reads as follows: "

,V3

END_IF/

ELSE/

COMMENT/OPER,Wasn't able to read from data file. Routine will now quit.

GOTO/END

END_ELSE/

END=LABEL/

ROUTINE/END

Code Explanation

Much of this code is similar to that explained in "Sample Code for Read Character". Only code unique to this example is listed here.

DO
This line begins a DO / UNTIL loop. It bounds the FILE/READ_LINE code so that the lines are continually read in one at a time. The loop exits when it reaches the end of the file.

V2=FILE/READLINE,FPTR,{LINE}
This line reads in all the text until it encounters a carriage return. Instead of storing the text in V2, like FILE/READ_CHARACTER would, however, this code acts differently.

- V2 in this case returns two values: either "OK" or "EOF". "OK" if there's still a line to read in. "EOF" if the end of the file is reached.

- The {LINE} code is a user-entered variable that stores the actual text. It is enclosed in curly brackets to tell PC-DMIS it's a variable and not a part of any delimiting text. Without the curly brackets, PC-DMIS would look for a string of characters in the file called "LINE" and would return only the text after "LINE" and before the carriage return.

ASSIGN/V3=V3+LINE
This line uses the empty V3 variable and concatenates the string V3 with LINE, and then reassigns the concatenated value to V3. So, with subsequent runs of the DO/UNTIL loop, V3 will get one more line added to it.

UNTIL/V2=="EOF"
This line tests for the condition of the DO / UNTIL loop. Once the FILE/READLINE code encounters the end of file, the loop exits. Once the routine flow exits the loop, the rest of the code finishes running and displays the entire block of code inside an operator comment.

Result=File/ReadLine,F1,"Part ID :"+{V1} - This causes any text appearing in the read in line after the text "Part ID:" to be assigned to V1. The line is read in from the file opened using F1 as the file pointer name. The result of the read (success or failure) would be stored in the variable Result.

File/ReadLine,F1,"Location:"+{VARX}+","+{VARY}+","+{VARZ}+","+{VARI}+","+{VARJ}+","+{VARK}

ASSIGN/CIR1.XYZ=MPOINT(VARX,VARY,VARZ)

ASSIGN/CIR1.IJK=MPOINT(VARI,VARJ,VARK)

The above three command lines read in comma delimited text after the string "Location:" and store the values in the X, Y, Z, and I, J, K values of CIR1.

File/ReadLine,F1,"Value # "+loopvar+" : "+{var2} - This causes var2 to be filled with the text appearing after the colon. The loopvar variable in this example is not surrounded by curly brackets and as a result contributes to the delimiting text.

#### Sample Code Dealing with Numbers Containing Preceding Zeros

If the file you are reading contains lines of numbers, you will notice that PC-DMIS ignores preceding zero characters. For example, if your line contained a value of 005450, it would read this value strictly as a number and return the value of 5450, ignoring the two preceding zeros. You may or may not want this.

Suppose you have a text file created by an external barcode reader software and it contains these two lines of data:

290291143;582750;0010

291143;5827;0010

You could use some simple code like this to get the number values in-between the semi-colons:

ASSIGN/FIRST_VALUE=0

ASSIGN/SECOND_VALUE=0

ASSIGN/THIRD_VALUE=0

ASSIGN/LINENUM=1

FPTR=FILE/OPEN,D:\TEMP\CODES.TXT,READ

DO/

INLINE=FILE/READLINE,FPTR,{FIRST_VALUE}+";"+{SECOND_VALUE}+";"+{THIRD_VALUE}

COMMENT/OPER,NO,"LINE NUMBER: "+LINENUM

,"First Value: "+FIRST_VALUE

,"Second Value: "+SECOND_VALUE

,"Third Value: "+THIRD_VALUE

UNTIL/INLINE=="EOF"

FILE/CLOSE,FPTR,KEEP

While this will successfully parse the lines of text and return the number values, it will also remove any preceding zeros for any values it returns. So, the THIRD_VALUE variable would contain a value of 10, instead of 0010.

To keep the preceding zero values, you would need to treat the whole line as a string and instead use the INDEX, LEFT, and MID string functions to locate the positions of semi-colons in a line of text and obtain the number values:

FPTR=FILE/OPEN,D:\TEMP\CODES.TXT,READ

ASSIGN/LINENUM=1

DO/

LINESTATUS=FILE/READLINE,FPTR,{LINESTR}

ASSIGN/LINESTR=STR(LINESTR)

ASSIGN/FIRST_INDEX=INDEX(LINESTR,";")

ASSIGN/FIRST_VALUE=STR(LEFT(LINESTR,FIRST_INDEX-1))

ASSIGN/REMAINSTR=STR(MID(LINESTR,(FIRST_INDEX)))

ASSIGN/SECOND_INDEX=INDEX(REMAINSTR,";")

ASSIGN/SECOND_VALUE=STR(LEFT(REMAINSTR,SECOND_INDEX-1))

ASSIGN/THIRD_VALUE=STR(MID(REMAINSTR,SECOND_INDEX))

COMMENT/OPER,NO,"LINE NUMBER: "+LINENUM

,"First Value: "+FIRST_VALUE

,"Second Value: "+SECOND_VALUE

,"Third Value: "+THIRD_VALUE

ASSIGN/LINENUM=LINENUM+1

UNTIL/LINESTATUS=="EOF"

FILE/CLOSE,FPTR,KEEP

Explanation of Code

Much of this code is similar to what is explained above. Only code explanations unique to the mentioned string functions are listed here.

ASSIGN/FIRST_INDEX=INDEX(LINESTR,";")
This line locates the position of the first semi-colon in the line and assigns that to the FIRST_INDEX variable.

ASSIGN/FIRST_VALUE=STR(LEFT(LINESTR,FIRST_INDEX-1))
This line assigns the FIRST_VALUE variable the string of characters up to, but not including, the first semi-colon in the LINESTR variable. LINESTR contains the entire line of text.

ASSIGN/REMAINSTR=STR(MID(LINESTR,(FIRST_INDEX)))
This line assigns the REMAINSTR variable (standing for "remaining string") the string of left over characters starting from the FIRST_INDEX position (the position of the first semi-colon) until the end of the line.

ASSIGN/SECOND_INDEX=INDEX(REMAINSTR,";")
This searches inside the REMAINSTR variable for another semi-colon (the second semi colon in the line) and assigns the position to the SECOND_INDEX variable.

ASSIGN/SECOND_VALUE=STR(LEFT(REMAINSTR,SECOND_INDEX-1))
This line assigns the SECOND_VALUE variable the string of characters up to, but not including, the first semi colon in the REMAINSTR variable (the second semi-colon in the entire line).

ASSIGN/THIRD_VALUE=STR(MID(REMAINSTR,SECOND_INDEX))
This line assigns the THIRD_VALUE variable the string of characters starting from the SECOND_INDEX position, until the end of the line.