当我尝试运行此代码时,出现错误:
line = line + device.readBuffer()
TypeError: can only concatenate list (not "bytes") to list
我搜索了一下,发现对于python 3,在尝试将字节转换为str时需要添加 b“” 。像说的那样更改后,该脚本运行了几秒钟,然后由于给出相同的错误而崩溃。
代码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import serial
import re
import datetime
class EM408GPS:
def __init__(self, serialport, baudratespeed):
self.gpsdevice = serial.Serial(port=serialport, baudrate=baudratespeed, timeout=5)
self.init()
def init(self):
if self.isOpen():
return True
return False
def open(self):
self.gpsdevice.open()
def isOpen(self):
print("hey")
return self.gpsdevice.isOpen()
def readBuffer(self):
try:
data = self.gpsdevice.read(1)
n = self.gpsdevice.inWaiting()
if n:
data = data + self.gpsdevice.read(n)
return data
except Exception as e:
print("Big time read error, what happened: ", e)
sys.exit(1)
def main():
device = EM408GPS("COM9", 9600)
newdata = b""
line = b""
while device.isOpen():
if newdata:
line = newdata
newdata = b""
line = line + device.readBuffer()
if re.search(b"\r\n", line):
data = line.split(b"\r\n")
newdata = line.split(b"\r\n")
#data, newdata = line.split(b"\r\n")
print ("----" + str(datetime.datetime.now()) + "----")
print (data)
print (newdata)
line = b""
main()
任何帮助将不胜感激
答案 0 :(得分:1)
如果 let makeSubfuel =this.state.data.map((subfuel) =>
{
return <Picker.Item label= {subfuel.name} value= {subfuel.id} key= {subfuel.price}/>
})
<Form>
<Picker style={{ width :'90%' ,borderRadius: 10,borderWidth: 3,borderColor: 'black'}}
mode="dropdown"
selectedValue={this.state.subfuelid}
onValueChange={(value,key,label) =>
{
this.setState({subfuelid: value})
//ToastAndroid.show("models id is "+ value+"price "+price+"label.."+label, ToastAndroid.LONG);
}
}>
<Picker.Item label="Select Fuel Type" value="key0" />
{makeSubfuel}
</Picker>
</Form>
为True,则拆分行,返回type(newdata)= list。然后,您分配re.search(b"\r\n", line)
并尝试连接
line = newdata