如何使用 ser.readlines 正确地将串行输出分配给值以供进一步使用?
我是新手,编写了一个脚本,该脚本通过串行 USB 端口启动与电能表的通信(“/?!”)并读出传入的电能日志。它从输出中提取不同的值并将其写入本地 sqlite-db 中。它应该每 15 分钟作为一个 cronjob 运行一次。顺便说一句,php 网站会将 sqlite 存储的能量读数可视化为图表。
目前我无法使用电表,那里非常偏远且寒冷,所以我需要在去那里之前拿到它。
如果我尝试执行脚本,则会引发错误:ser.readlines(eol='!') = z
语法错误:无法分配给函数调用
我使用 ser.readlines 读取串行输出并分配给值 z 是否错误?
如果我打开一个包含类似值的日志文件,则脚本(正则表达式和插入数据库)可以工作:例如 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 am newbie and wrote a script, which initiates communication ("/?!") 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 call
is 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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想而不是
你可能想要
I think instead of
you probably want