我做了一个检查串口上是否有活动的函数,从tkinter Gui获取串口数据,然后返回答案,但是我在初始化变量时遇到了问题。
我尝试将其设置为int,但是遇到了这个问题,如果没有,我会收到另一个错误,告诉我波特率无效。
#serial port checking function
def check_SERIAL(port_name='/dev/ttyUSB0',baud_rate=115200,serial_parity='serial.PARITY_NONE',stop_bits=8,byte_size=8,time_out=1): #pings serial interface, VAR: port name, baud rate, parity, stopB, byteS, Timeout
logger.debug('checking Serial Connection')
ser = serial.Serial(
port=port_name,
baudrate=baud_rate,
parity=serial_parity,
stopbits=stop_bits,
bytesize=byte_size,
timeout=time_out
)
time.sleep(1)
A=0
try:
ser.write("||;1>get logfile\r\n".encode())
for x in range(0,1000):
if ser.inWaiting() > 10:
A=1
finally:
ser.close()
if A==0:
raise device_crashed(" Device Crashed")
time.sleep(1)
#entries from the GUI
entry_PORT = Entry(second_frame, bg="white")
entry_PORT.grid(row=14,column=1,padx=0,pady=5)
entry_BAUD = Entry(second_frame, bg="white")
entry_BAUD.grid(row=16,column=1,padx=0,pady=5)
entry_PARITY = Entry(second_frame, bg="white")
entry_PARITY.grid(row=18,column=1,padx=0,pady=5)
entry_STOPB = Entry(second_frame, bg="white")
entry_STOPB.grid(row=20,column=1,padx=0,pady=5)
entry_BYTES = Entry(second_frame, bg="white")
entry_BYTES.grid(row=22,column=1,padx=0,pady=5)
#running code:
def start_testing():
try:
logger.debug('test has started ')
#checking device interfaces if are ok
check_SERIAL(port_name=str(entry_PORT),baud_rate=int(entry_BAUD),serial_parity=entry_PARITY,stop_bits=entry_STOPB,byte_size=entry_BYTES)
except Exception as e: #general exceptions
logger.error('Something went wrong: ' + str(e))
button=Button(second_frame, text='Start Test',borderwidth=10,command= start_testing)
button.place(relx=0.8,rely=0.4,relwidth=0.15,relheight=0.12)
我得到的错误:
int() argument must be as 'string, a bytes like object or a number, not 'Entry'