import pyodbc

import snap7
from snap7.util import *

# Create a connection to the PLC
plc = snap7.client.Client()
plc.connect('192.168.xxx.xxx', 0, 1)
# Read the value of MW10 & MW20
result = plc.read_area(snap7.types.Areas['MK'], 0, 10, 2)
result1 = plc.read_area(snap7.types.Areas['MK'], 0, 20, 2)

# Convert the result to a Python integer
mw10 = snap7.util.get_int(result, 0)
mw20 = snap7.util.get_int(result1, 0)
# Print the value of MW10
print(mw10)
print(mw20)
plc.disconnect()


# Connect to the database


conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=YOUR SERVER NAME;'
                      'Database=DBNAME;'
                      'Trusted_Connection=yes;')

cursor = conn.cursor()
   
cursor.execute("INSERT INTO TABLENAME(column1, column2) VALUES (?, ?)", (mw10, mw20,))

conn.commit()

DON'T COPY BELOW LINES
FIRST INSTALL SNAP7 LIBRARY BY EXECUTING BELOW COMMAND IN 'CMD'
pip install python-snap7
IT IS TESTED SCRIPT....

Comments