Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Py串行读线在Python3中不工作_Python_Python 3.6_Raspberry Pi3_Pyserial - Fatal编程技术网

Py串行读线在Python3中不工作

Py串行读线在Python3中不工作,python,python-3.6,raspberry-pi3,pyserial,Python,Python 3.6,Raspberry Pi3,Pyserial,我使用Python 2.7编写了以下代码: import serial, io import pynmea2 ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) while True: reading = ser.readline() if reading.find('GGA') > 0: msg = pynmea2.parse(reading) print "Lat: %s " %

我使用Python 2.7编写了以下代码:

import serial, io
import pynmea2


ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
while True:
    reading = ser.readline()
    if reading.find('GGA') > 0:
        msg = pynmea2.parse(reading)
        print "Lat: %s " % (msg.lat)
但这里的相同代码在Python3中不起作用:

import serial, io
import pynmea2


ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
while True:
    reading = ser.readline()
    if reading.find('GGA') > 0:
        msg = pynmea2.parse(reading)
        print("Lat: %s " % (msg.lat))
错误消息是:

如果正在读取。查找'GGA'>0: TypeError:参数应该是整数或类似对象的字节,而不是'str'


如何让代码在Python3中工作在Python3中字符串类型与字节序列分离。这意味着“GGA”是一个字符串文字,而读取是一个字节序列。有几种方法可以解决此问题。 第一通过调用reading=reading.decode将读取转换为字符串。您需要知道存在哪种编码。
第二使用“GGA”将文字转换为字节。编码,或创建字节文字b'GGA。

@user3141181如果这回答了您的问题,请将其标记为解决方案。