gps.py 文件源码

python
阅读 32 收藏 0 点赞 0 评论 0

项目:rpi-can-logger 作者: JonnoFTW 项目源码 文件源码
def read(self):
        out = {k: None for k in self.FIELDS + self.EXTRA_FIELD}
        start = datetime.now()
        self.ser.reset_input_buffer()
        while not all(out.values()):
            line = self.readline()
            if (datetime.now() - start).total_seconds() > self.timeout:
                break
            line = re.sub(r'[\x00-\x1F]|\r|\n|\t|\$', "", line)

            cmd = line.split(',')[0]
            if cmd not in ['GNGGA', 'GNRMC']:
                continue
            try:
                msg = pynmea2.parse(line)
                for key in out:
                    if hasattr(msg, key):
                        out[key] = getattr(msg, key)
            except pynmea2.ParseError as e:
                print("Parse error:", e)

        if out['datestamp'] is not None and out['timestamp'] is not None:
            timestamp = datetime.combine(out['datestamp'], out['timestamp']).replace(tzinfo=timezone.utc)
            out['timestamp'] = timestamp.isoformat()
        else:
            del out['timestamp']
        if out[self.FIELDS[-1]] is not None:
            out[self.FIELDS[-1]] *= self.KNOTS_PER_KMPH
        if out.get('latitude') is not None and out.get('longitude') is not None:
            if out['latitude'] != 0.0 and out['longitude'] != 0.0:
                out['pos'] = {
                    'type': 'Point',
                    'coordinates': [out['longitude'], out['latitude']]
                }
            del out['latitude']
            del out['longitude']
        for f in self.EXTRA_FIELD:
            if f in out:
                del out[f]

        return out
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号