How to properly assign serial output to value with ser.readlines for further usage?
I am newbie and wrote a script, which initiates com开发者_运维百科munication ("/?!") to an energy meter through serial USBport and reads out the incoming energy log. It extracts different values from the output and writes those into a local sqlite-db. It is suppose to run as a cronjob every 15min. A php-website will visualize the sqlite-stored energy readings as a graph btw.
At the moment I don't have access to the meter, it is very remote and cold there, so I need to have it right before I go there.
If I am trying to execute script it raises Error: ser.readlines(eol='!') = z SyntaxError: can't assign to function callis my reading with ser.readlines of the serial output and assigning to value z wrong?
the script (regex & inserting into DB) works if I open a logfile with similiar values in it: e.g.with open ("log") as z:..
#!/usr/bin/env python
import serial
import time
import re
import sqlite3
ser = serial.Serial('/dev/ttyUSB0', 300, bytesize=serial.SEVENBITS, parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE, timeout=2)
ser.open()
ser.write("/?!")
time.sleep(0.001)
ser.write("\r")
connection = sqlite3.connect('/path/to/dbname.db')
cursor = connection.cursor()
extrakt = []
ser.readlines(eol='!') = z
for line in z:
match = re.search(r'(0\.0\.0|1\.6\.1|1\.8\.1)\(([0-9\.]+)', line)
if match:
version,value = match.groups()
extrakt.append(value)
cursor.execute('INSERT INTO energielog (sernr, peak, kwh) values (?, ?, ?)', extrakt)
connection.commit()
ser.close()
cursor.close()
connection.close()
I think instead of
ser.readlines(eol='!') = z
for line in z:
you probably want
for line in ser.readlines(eol='!'):
精彩评论