我需要从变量数据中减去481310,但是我得到了这个错误。 TypeError:不支持的操作数类型 - =:' bytes'和' int' 它在python中。 我想从变量中减去481310因为它应该是0但是出于某种原因它是481310如果有人知道其他方法来解决它让我知道!
import serial
import serial.tools.list_ports
import sys
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
#print result[1]
#ports = list(serial.tools.list_ports.comports())
#print ports[1]
arduino = serial.Serial(result[1], 9600)
try:
while True:
data = arduino.readline()
data -= 481310
if data:
#print data
datalist = list(data)
#print len(datalist)
#print datalist
str1 = ''.join([str(i) for i in datalist])
#print str1
f = open('data.txt', 'a')
f.write(str1+ '\n')
f.close()
except (KeyboardInterrupt, SystemExit):
arduino.close()
quit