python:从设备读取时失败或减少输入缓冲区

时间:2016-04-17 11:36:37

标签: python io buffer

(覆盆子硬件上的raspbian上的python 2.7.9) 从串口读取GPS数据,我发现没办法打败输入缓冲。这使得我的数据在很大程度上变得模糊不清。我搜索了这个网站和其他网站,并应用了#34;缓冲"在开放声明中以各种形式(缓冲= 0,缓冲= -1,缓冲= 5,......),都无济于事。

INPORT='/dev/ttyAMA0'
with open(INPORT,'rb',buffering=-1) as infile:
 for INLIN in INFILE:
  INFIELD=INLIN.split(",")
   if INFIELD[0][0:1] == "$":
    print datetime.datetime.now(),INFIELD[0]

输出样本:

2016-04-17 11:59:20.469383 $GNGSA
2016-04-17 11:59:20.471004 $GPGSV
2016-04-17 11:59:20.472660 $GPGSV
2016-04-17 11:59:20.475128 $GPGSV
2016-04-17 11:59:20.475743 $GPGSV
2016-04-17 11:59:20.477228 $GLGSV
2016-04-17 11:59:20.478902 $GLGSV
2016-04-17 11:59:20.480494 $GLGSV
2016-04-17 11:59:20.481994 $GNGLL
2016-04-17 11:59:20.483569 $GNRMC
2016-04-17 11:59:20.484283 $GNVTG
2016-04-17 11:59:33.681551 $GNGGA
2016-04-17 11:59:33.682010 $GNGSA
2016-04-17 11:59:33.682357 $GNGSA
2016-04-17 11:59:33.682693 $GPGSV
2016-04-17 11:59:33.683025 $GPGSV
2016-04-17 11:59:33.683359 $GPGSV
2016-04-17 11:59:33.683680 $GPGSV
2016-04-17 11:59:33.684017 $GLGSV
2016-04-17 11:59:33.684467 $GLGSV

1 个答案:

答案 0 :(得分:1)

with open(INPORT,'rb',buffering=-1) as infile:
 for INLIN in iter(infile.readline,""):
  INFIELD=INLIN.split(",")
  if INFIELD[0] == "$GPRMC" or INFIELD[0] == "$GNRMC":
   print datetime.datetime.now(),INLIN

(正如建议的那样)完美无缺 - 非常感谢Padraig!

2016-04-17 12:59:23.119878 $GNRMC,125923.00,A,5058.48999,N,00437.42029,E,0.050,,170416,,,D*65
2016-04-17 12:59:24.119874 $GNRMC,125924.00,A,5058.48998,N,00437.42032,E,0.066,,170416,,,D*6C
2016-04-17 12:59:25.119593 $GNRMC,125925.00,A,5058.48999,N,00437.42034,E,0.070,,170416,,,D*6D
2016-04-17 12:59:26.121776 $GNRMC,125926.00,A,5058.49001,N,00437.42033,E,0.082,,170416,,,D*6D
2016-04-17 12:59:27.119076 $GNRMC,125927.00,A,5058.49002,N,00437.42033,E,0.025,,170416,,,D*62
2016-04-17 12:59:28.119765 $GNRMC,125928.00,A,5058.49003,N,00437.42035,E,0.113,,170416,,,D*6E