Aug-13-2018, 01:21 AM
Hi, i'm trying to make a g-code reader for my cnc project, i allready had some progress, but i'm strugguling to make it identify the number 0,1,4 and 9. i'm not used to python, so i prefer not to use modules, my gode goes like this, fist it gets the gcode file, then it scans line by line, in each line the code search for a particular letter like "x" than it get x's position in the string(line), after that the code starts to get each character after x's position until it finds a " "(space), then with each char that it have encouter it atributes to a variable call "xStr" then after xStr is "complete" (i say "complete" because not ever the code get every single char) it changes to a float variable, and in some step of the way something is not quite working the way it should.
# that's texto1 # N10 G0 Z2.007 # N20 G0 X70 Y0 # N30 G1 Z-0.991 F100 # N40 G1 X20 Y10.008 F200 # N50 G1 X3.232 Y10.008 # N1940 G1 X324.268 Y3.951 # N0 g1 X3.576045 y23,344 # N13 G1 X3445 Y34 # N1940 G1 X6.0025 Y3.951 # N1940 G1 X3 Y3.951 # N1940 G1 X5 Y3.951 import string s=open('texto1.txt', 'r') cont = 0 tam = 0 for line in s: if line.startswith('N'): i = line.find("X") tam=len(line) flag=False valX=i xStr='' x = 0; for cont in range(tam): letter=line[cont] if line[cont] == 'X': valX=line.find(line[cont]) lnum=line.find(letter) if lnum > valX and letter != ' ' and flag == False: if letter.isalpha() == True: flag=True break xStr=xStr + letter x = float(xStr) print(x) print('n é igual a {}'.format(cont))please, if you find the error please highlight it, as i said i'm not used to python, the error might be dumb but i'm not able to spot it. thank you
