Python – Unusual "Index Out of Range" while reading from USB

Question:

Happy new Year together,

normaly, i try to solve everything myself, to keep my grey Mushroom alive.
But in this case im really stuck…

My Task:

  • Read from an USB Device
  • Create an Animation based in Data of an analog Axis

Problem:

  • It works on one PC while in pyCharm, also as EXE
  • Index Out of Range on other PC´s

Info

  • Error belongs to Line 82 – Read_X2 = readout[2]
  • Console shows successful line of the USB List
  • Programm isnt frozen while Error (OK Print Button works)
  • I started Coding 3 Days ago… So i made it like this, becouse im still to stupide
    to end a "while" without closing my Programm…
    Also List-"Drehung" could be made more elegant. But my first intention for the error
    was about my auto counting "range" creation. So i deleted it and made it this way.
    Meanwhile i know, its from my USB List.

I bet, its a dead simple Problem. I just overlooking something.
Youre also allowed to laugh about it if you tell me the solution afterwards.

Greetings Emanresu

My Code Salad:

from tkinter import *
from PIL import ImageTk, Image
import hid

# START Part to avoid Problems with "One File"

import sys
import os


def resource_path(relative_path):
    """ Get the absolute path to the resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
       base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

# END Part to avoid Problems with "One File"


win = Tk("Test")
win.geometry("460x410")
win.config(bg="grey")
Btn = Button(win, text="ok", activebackground='gray', bg='grey', bd=0)

# Choose a "Path"- Option for "One Directory" or "One File"

# path = "./Animation/"
path = (resource_path("Animation/"))

List = ['000.png',
        '001.png', '002.png', '003.png', '004.png', '005.png', '006.png', '007.png', '008.png', '009.png', '010.png',
        '011.png', '012.png', '013.png', '014.png', '015.png', '016.png', '017.png', '018.png', '019.png', '020.png',
        '021.png', '022.png', '023.png', '024.png', '025.png', '026.png', '027.png', '028.png', '029.png', '030.png',
        '031.png', '032.png', '033.png', '034.png', '035.png', '036.png', '037.png', '038.png', '039.png', '040.png',
        '041.png', '042.png', '043.png', '044.png', '045.png', '046.png', '047.png', '048.png', '049.png', '050.png',
        '051.png', '052.png', '053.png', '054.png', '055.png', '056.png', '057.png', '058.png', '059.png', '060.png',
        '061.png', '062.png', '063.png', '064.png', '065.png', '066.png', '067.png', '068.png', '069.png', '070.png',
        '071.png', '072.png', '073.png', '074.png', '075.png', '076.png', '077.png', '078.png', '079.png', '080.png',
        '081.png', '082.png', '083.png', '084.png', '085.png', '086.png', '087.png', '088.png', '089.png', '090.png',
        '091.png', '092.png', '093.png', '094.png', '095.png', '096.png', '097.png', '098.png', '099.png', '100.png',
        '101.png', '102.png', '103.png', '104.png', '105.png', '106.png', '107.png', '108.png', '109.png', '110.png',
        '111.png', '112.png', '113.png', '114.png', '115.png', '116.png', '117.png', '118.png', '119.png', '120.png',
        '121.png', '122.png', '123.png', '124.png', '125.png', '126.png', '127.png', '128.png', '129.png', '130.png',
        '131.png', '132.png', '133.png', '134.png', '135.png', '136.png', '137.png', '138.png', '139.png', '140.png',
        '141.png', '142.png', '143.png', '144.png', '145.png', '146.png', '147.png', '148.png', '149.png', '150.png',
        '151.png', '152.png', '153.png', '154.png', '155.png', '156.png', '157.png', '158.png', '159.png', '160.png',
        '161.png', '162.png', '163.png', '164.png', '165.png', '166.png', '167.png', '168.png', '169.png', '170.png',
        '171.png', '172.png', '173.png', '174.png', '175.png', '176.png', '177.png', '178.png', '179.png', '180.png',
        '181.png', '182.png', '183.png', '184.png', '185.png', '186.png', '187.png', '188.png', '189.png', '190.png',
        '191.png', '192.png', '193.png', '194.png', '195.png', '196.png', '197.png', '198.png', '199.png', '200.png',
        '201.png', '202.png', '203.png', '204.png', '205.png', '206.png', '207.png', '208.png', '209.png', '210.png',
        '211.png', '212.png', '213.png', '214.png', '215.png', '216.png', '217.png', '218.png', '219.png', '220.png',
        '221.png', '222.png', '223.png', '224.png', '225.png', '226.png', '227.png', '228.png', '229.png', '230.png',
        '231.png', '232.png', '233.png', '234.png', '235.png', '236.png', '237.png', '238.png', '239.png', '240.png',
        '241.png', '242.png', '243.png', '244.png', '245.png', '246.png', '247.png', '248.png', '249.png', '250.png',
        '251.png', '252.png', '253.png', '254.png', '255.png', '256.png', '257.png', '258.png', '259.png', '260.png',
        ]


# Creating Canvas for the Animation
def to_pil2(img, button, x, y, w, h):
    image = Image.open(img)
    image = image.resize((w, h))
    pic = ImageTk.PhotoImage(image)
    button['image'] = pic
    button.image = pic
    button.place(x=x, y=y)


# Optional Visualisation of Readout
label = Label(win, font=('Stencil', 30, 'bold'), bg='grey', fg='black')
label.place(x=5, y=5)


# Collecting Date from USB-Device
# Col0=unknown, Col1+2= X-Axis, Col3+4= Y-Axis, Col5+6= Z-Axis, Col7+8+9= Analog unused, Col10= Buttons
def run():
    simpad = hid.device()
    simpad.open(0x2341, 0x8037)
    simpad.set_nonblocking(True)

    readout = simpad.read(11)
    # read_x1 = readout[1]
    read_x2 = readout[2]
    # read_multiply = (read_x2 * 256)
    # read_full = (read_multiply + read_X1)
    # animation = (read_full / 700) #Option For smoother Movement

# Drawing Animation + Number on Canvas
    to_pil2(path + List[int(read_x2)], Btn, 5, 5, 450, 400)
    label['text'] = read_x2
    win.after(10, run)
    print(readout)


# Pray
run()
Btn["command"] = lambda: print('Freeze-test')
win.mainloop()
Asked By: Emanresu

||

Answers:

I’m not familiar with all the libraries you are using but I have done I/O stuff before. Just glancing at the code I see you are doing a nonblocking read (you called set_nonblocking with True). Therefore, you either need to handle the case when no data or a smaller amount of data is read, or you need to change your code to do a blocking read, which will wait until all 11 bytes have been read.

Answered By: David Grayson
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.