class PYLOGIX_LOGGER_FLAT_ASCII(PYLOGIX_LOGGER):
def __init__(self,log_name,*args,fmtstr="{0} - {1} - {2}\n",**kwargs):
self.log_name = log_name
self.format = fmtstr.format
super().__init__(*args,**kwargs)
def __call__(self,*args,**kwargs):
if self.changeds: ### Do nothing for no changes
with open(self.log_name, "a") as fOut:
for changed in self.changeds:
fOut.write(self.format(*changed))
@turbohamad:
Python reads tags from PLCs thru a module called pylogix, I wrote this in another thread started by you some days ago.
Python for begginners can be use with an IDE by downloading Thonny.
Go to : https://thonny.org/
After thonny is installed together with python, you have to download pylogix.
Go to :https://pypi.org/project/pylogix/
or https://github.com/dmroeder/pylogix , Dustin is part of PLCs.net
You can also download pylogix thru Thonny by searching on pypi.
Dustin also has a discord, with some examples, and help.
As I mentioned the esasiest way is thru XLRporter and RSlogix professional but it is expensive, is up to you.
widelto, I downloaded Thonny and install pylogix on it. Do you have good tutorial of how to use it?
Thank you
### Cf. https://www.plctalk.net/qanda/showthread.php?t=132004
import sys
import time
import pylogix
import datetime
import pandas as pd
import MySQLdb as Mdb
### Parse command-line arguments: PLC IP address; MariaDB/MySQL DB server IP address; DB username
plcip = (['192.168.1.160']+[a[8:] for a in sys.argv[1:] if a[:8]=='--plcip=']).pop()
ip = (['127.0.0.1']+[a[5:] for a in sys.argv[1:] if a[:5]=='--ip=']).pop()
usr = (['test_log']+[a[7:] for a in sys.argv[1:] if a[:7]=='--user=']).pop()
### Ensure DATABASE 'test_log' and TABLE 'log' exist on MariaDB/MySQL server
Mdb.connect(host=ip,user=usr).cursor().execute('CREATE DATABASE IF NOT EXISTS test_log')
cu = Mdb.connect(host=ip,user=usr,db='test_log',autocommit=True).cursor()
cu.execute('CREATE TABLE IF NOT EXISTS log (tim datetime NOT NULL,tag varchar(32) NOT NULL,val double NOT NULL,PRIMARY KEY tn (tim,tag))')
### Sample data from PLC at ~1Hz, write data to MariaDB/MySQL DATABASE.TABLE 'test_log.log'
with pylogix.PLC(plcip) as plc:
plc.Micro800 = not ('--no-micro8xx' in sys.argv[1:])
for i in range(10):
time.sleep(1.001)
dt = datetime.datetime.utcnow()
cu.executemany('INSERT INTO log VALUES (%s,%s,%s)',[(dt,tag,plc.Read(tag).Value,) for tag in 'et_ms c.Year c.Month c.Day c.Hours c.Minutes c.Seconds'.split()])
### Retrieve data from 'test_log.log' into list of dicts
cu.execute('select * from log')
ads = [dict(ts=None)]
for ts,tag,val in cu:
if ts.isoformat() != ads[-1]['ts']: ads.append(dict(ts=ts.isoformat()))
ads[-1][tag] = val
cu.close()
### Write list of dicts to eXcel workbook
pd.DataFrame.from_records(ads[1:],index='ts').to_excel('test_log.xlsx')
$ mysql test_log -e "select * from log"
+---------------------+-----------+------+
| tim | tag | val |
+---------------------+-----------+------+
| 2022-02-09 20:37:28 | c.Day | 9 |
| 2022-02-09 20:37:28 | c.Hours | 15 |
| 2022-02-09 20:37:28 | c.Minutes | 38 |
| 2022-02-09 20:37:28 | c.Month | 2 |
| 2022-02-09 20:37:28 | c.Seconds | 25 |
| 2022-02-09 20:37:28 | c.Year | 2022 |
| 2022-02-09 20:37:28 | et_ms | 46 |
| 2022-02-09 20:37:29 | c.Day | 9 |
| 2022-02-09 20:37:29 | c.Hours | 15 |
...