索引器:字符串索引超出范围,pyserial tkinter,python 3.4

索引器:字符串索引超出范围,pyserial tkinter,python 3.4,python,string,while-loop,tkinter,pyserial,Python,String,While Loop,Tkinter,Pyserial,一旦我的串行端口识别**@*或**`* 这是我的密码 while set_ser.isOpen(): time.sleep(0.0001) bytess = set_ser.inWaiting() bytes = set_ser.read(bytess) get_serial = str(bytes.decode("utf-8", errors="ignore")) loop.set(get_serial) if (get_serial[2]=='@'): #busy s

一旦我的串行端口识别**@*或**`*

这是我的密码

while set_ser.isOpen():
 time.sleep(0.0001)
 bytess = set_ser.inWaiting()
 bytes = set_ser.read(bytess)
 get_serial = str(bytes.decode("utf-8", errors="ignore"))
 loop.set(get_serial)

 if (get_serial[2]=='@'): #busy
     sLabelRead.configure(bg = 'red', fg='white')
     sLabelRead.place(x=10,y=600)
     mGui.update()
 elif (get_serial[2]=='`'): #ready:
     sLabelRead.configure(bg = 'orange', fg='black')
     sLabelRead.place(x=10,y=600)
     mGui.update()
 elif (get_serial==''): #none:
     sLabelRead.configure(bg = 'black', fg='white')
     sLabelRead.place(x=10,y=600)
     mGui.update()
 mGui.update()
else:
 print (get_serial)
 mGui.update()
但我得到了这个错误

if (get_serial[1]=='@'): #busy
IndexError: string index out of range
谁能帮帮我吗
谢谢

事实上,我的字符串小于2个字符。所以我只是检查了它的长度并修改了我的条件,谢谢,我没有得到所有的时间2或3个元素,有时只有1或没有,这意味着你的字符串小于2个字符。您可能得到一个空字符串,只需使用
print
或/和
len