【发布时间】:2013-05-03 07:38:36
【问题描述】:
我想做一个从串口获取数据的程序,开始和停止位将是*和#。收到的数据将采用 *1234567# 这种形式。但它说我的“类型”对象不可下标。我真的是 Python 新手,不知道下一步该做什么,谁能帮我解决这个问题?
import serial
ser = serial.Serial(
port='COM5',\
baudrate=9600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=0)
MAX_BUF_SIZE = 16
bits = 0
v = memoryview
print("connected to: " + ser.portstr)
while(1):
for memoryview in ser.read():
if v[0] == 42:
if v[-1] == 35:
print(v[1:-1].tobytes())
else:
memoryview = 0
ser.close()
connected to: COM5
Traceback (most recent call last):
File "C:\Python33\Saves\Receive using buff.py", line 24, in <module>
if v[0] == 42:
TypeError: 'type' object is not subscriptable
>>>
【问题讨论】:
-
memoryview最初是什么?你把它分配给 v。基本上错误说你不能在v[0]中为 v 使用 [] 运算符@ -
@Juho:
memoryview是内置的。
标签: python python-3.x pyserial xbee