Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
G code reader
#1
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 Big Grin
Reply
#2
I won't attempt to answer your question as I know nothing about G code, other than what it is for, but you might want to look at the following to perhaps get some ideas: https://pypi.org/project/pycnc/#history
Reply
#3
I think the problem is line 31 (lnum=line.find(letter)). Note that find get the first instance of that letter in the string. So if you have a number after the X that also occurred before the X, it will find the one before the X. Then that won't meet the lnum > valX criteria.

To fix this problem you really need to rewrite your whole code, because this problem is indicative of several problems in how you are writing your code.

On line 19 you set i to the location of the first X. Then on line 22 you set valX to i. You never use i. Just replace i on line 19 with valX.

On line 24 you use a semi-colon. Don't do that.

On line 27, you loop over the indexes of the line. Don't do that. You can loop directly over the characters in the line. Therefore, looping over the indexes of the line introduces complexity you don't need, which can only cause problems. You could change that to for letter in line:, but think about your loop for a second. You're ignoring everything up to and including the X. You also know where the X is (it's in valX). So why not just loop over the characters after the X? It makes things so much simpler: for letter in line[valX + 1:]:.

You don't need line 27, since you are now looping directly over the letters. You never needed lines 28 and 29. All they do is reset valX to valX when you get to valX. You certainly don't need them with the new loop I described, since we're skipping over valX. Line 31 you never needed either. If it was working the way you expected to, it would have just set lnum to cont. But it wasn't working that way, so it was causing problems.

The conditional on line 31 simplifies, because every character we are looking at is greater than valX. The whole area from line 31 to line 38 is over complicated, but it should work with the other problems solved.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  xml simple reader kucingkembar 2 1,052 Aug-19-2022, 08:51 PM
Last Post: kucingkembar
  Having strange results from an RFID HID card reader - I'm stuck orbisnz 1 1,477 Mar-28-2022, 08:20 AM
Last Post: Larz60+
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,468 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  NFC reader code help johnroberts2k 1 2,570 Jul-02-2021, 08:43 PM
Last Post: deanhystad
  csv.reader(): Limit the number of columns read in Windows Pedroski55 9 5,190 Jan-23-2021, 01:03 AM
Last Post: pjfarley3
  Closing Files - CSV Reader/Writer lummers 2 2,605 May-28-2020, 06:36 AM
Last Post: Knight18
  csv reader kgiles 3 5,338 Nov-05-2019, 09:04 AM
Last Post: perfringo
  Python code for gcode reader and representation ralmeida 1 6,243 Jul-31-2018, 09:20 AM
Last Post: DeaD_EyE
  AttributeError: module 'csv' has no attribute 'reader' python1234 2 26,939 Jun-08-2018, 06:13 AM
Last Post: python1234
  PDf reader jorgelameira 2 3,933 Mar-24-2018, 02:02 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020