PySerial不会扫描所有行

时间:2013-07-05 21:51:09

标签: pyserial

我正在使用PySerial从我的智能电表(电力和燃气消耗计)中读取数据。

当使用cu时,一切似乎都工作正常,我每隔几秒就会收到一些新数据。

示例数据:

/ISk5\2ME382-1003
0-0:96.1.1(4B414C37303035313039)
1-0:1.8.1(00180.724*kWh)
1-0:1.8.2(00001.416*kWh)
1-0:2.8.1(00000.000*kWh)
1-0:2.8.2(00000.000*kWh)
0-0:96.14.0(0001)
1-0:1.7.0(0000.42*kW)
1-0:2.7.0(0000.00*kW)
0-0:17.0.0(0999.00*kW)
0-0:96.3.10(1)
0-0:96.13.1()
0-0:96.13.0()
0-1:24.1.0(3)
0-1:96.1.0(32383031313030313231)
0-1:24.3.0(120911010000)(00)(60)(1)(0-1:24.2.1)(m3)
(00007.646)
0-1:24.4.0(1)
!

然而,当使用pyserial时,我经常不接收所有数据。通常只有前15行左右。

目前我正在使用此脚本:

#Set COM port config
ser = serial.Serial()
ser.baudrate = 9600
ser.bytesize=serial.SEVENBITS
ser.parity=serial.PARITY_EVEN
ser.stopbits=serial.STOPBITS_ONE
ser.xonxoff=0
ser.rtscts=0
#ser.timeout=50
ser.port="/dev/ttyUSB0"

#Open COM port
try:
    ser.open()
except:
    sys.exit ("Fout bij het openen van %s. Programma afgebroken."  % ser.name)  


#Initialize
# stack is mijn list met de 20 regeltjes.
p1_teller=0
stack=[]

while p1_teller < 20:
    p1_line=''
#Read 1 line
    try:
        p1_raw = ser.readline()
    except:
        sys.exit ("Seriele poort %s kan niet gelezen worden. Programma afgebroken." % ser.name )
    p1_str=str(p1_raw)
    #p1_str=str(p1_raw, "utf-8")
    p1_line=p1_str.strip()
    stack.append(p1_line)
# als je alles wil zien moet je de volgende line uncommenten
    print (p1_line)
    p1_teller = p1_teller +1

#Initialize
# stack_teller is mijn tellertje voor de 20 weer door te lopen. Waarschijnlijk mag ik die p1_teller oo$
stack_teller=0

while stack_teller < 20:
   if stack[stack_teller][0:9] == "1-0:1.8.1":
        daldag = stack[stack_teller][10:15]
   elif stack[stack_teller][0:9] == "1-0:1.8.2":
        piekdag = stack[stack_teller][10:15]
# Huidige stroomafname: 1-0:1.7.0
   elif stack[stack_teller][0:9] == "1-0:1.7.0":
        p_afgenomen = stack[stack_teller][10:17]
# Gasmeter: 0-1:24.3.0
   elif stack[stack_teller][0:10] == "0-1:24.3.0":
        gas = stack[stack_teller+1]
   else:
        pass
   stack_teller = stack_teller +1

print daldag
print piekdag
print p_afgenomen
print gas

#Close port and show status
try:
    ser.close()
except:
    sys.exit ("Oops %s. Programma afgebroken." % ser.name )
exit

我该如何解决这个问题?

或者有没有办法确保pySerial继续阅读,直到它击中!线?

0 个答案:

没有答案