提交 baabd04e 编写于 作者: nengyuangzhang's avatar nengyuangzhang

Replaced telnetlib with telentlib3 in myems-modbus-tcp

......@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed
- Updated English Translation in myems-admin
- Updated Meter, Point, User and Space editor dialogs in myems-admin
- Replaced telnetlib with telentlib3 in myems-modbus-tcp
### Fixed
- Fixed typo in database
......
......@@ -14,6 +14,7 @@ schedule
python-decouple
telnetlib3
### Quick Run for Development
......
import json
import math
import telnetlib
import telnetlib3
import asyncio
import time
from datetime import datetime
from decimal import Decimal
import mysql.connector
from modbus_tk import modbus_tcp
import config
from byte_swap import byte_swap_32_bit, byte_swap_64_bit
########################################################################################################################
# Check connectivity to the host and port
########################################################################################################################
async def check_connectivity(host, port):
reader, writer = await telnetlib3.open_connection(host, port)
# Close the connection
writer.close()
########################################################################################################################
# Acquisition Procedures
# Step 1: telnet the host
# Step 1: Check connectivity to the host and port
# Step 2: Get point list
# Step 3: Read point values from Modbus slaves
# Step 4: Bulk insert point values and update latest values in historical database
......@@ -22,18 +29,17 @@ from byte_swap import byte_swap_32_bit, byte_swap_64_bit
def process(logger, data_source_id, host, port):
while True:
# begin of the outermost while loop
################################################################################################################
# Step 1: telnet the host
# Step 1: Check connectivity to the host and port
################################################################################################################
try:
telnetlib.Telnet(host, port, 10)
print("Succeeded to telnet %s:%s in acquisition process ", host, port)
asyncio.run(check_connectivity(host, port))
print("Succeeded to connect %s:%s in acquisition process ", host, port)
except Exception as e:
logger.error("Failed to telnet %s:%s in acquisition process: %s ", host, port, str(e))
logger.error("Failed to connect %s:%s in acquisition process: %s ", host, port, str(e))
# go to begin of the outermost while loop
time.sleep(300)
continue
......@@ -61,7 +67,7 @@ def process(logger, data_source_id, host, port):
" FROM tbl_points "
" WHERE data_source_id = %s AND is_virtual = 0 "
" ORDER BY id ")
cursor_system_db.execute(query, (data_source_id, ))
cursor_system_db.execute(query, (data_source_id,))
rows_point = cursor_system_db.fetchall()
except Exception as e:
logger.error("Error in step 2.2 of acquisition process: " + str(e))
......@@ -142,18 +148,17 @@ def process(logger, data_source_id, host, port):
continue
if 'slave_id' not in address.keys() \
or 'function_code' not in address.keys() \
or 'offset' not in address.keys() \
or 'number_of_registers' not in address.keys() \
or 'format' not in address.keys() \
or 'byte_swap' not in address.keys() \
or address['slave_id'] < 1 \
or address['function_code'] not in (1, 2, 3, 4) \
or address['offset'] < 0 \
or address['number_of_registers'] < 0 \
or len(address['format']) < 1 \
or 'function_code' not in address.keys() \
or 'offset' not in address.keys() \
or 'number_of_registers' not in address.keys() \
or 'format' not in address.keys() \
or 'byte_swap' not in address.keys() \
or address['slave_id'] < 1 \
or address['function_code'] not in (1, 2, 3, 4) \
or address['offset'] < 0 \
or address['number_of_registers'] < 0 \
or len(address['format']) < 1 \
or not isinstance(address['byte_swap'], bool):
logger.error('Data Source(ID=%s), Point(ID=%s) Invalid address data.',
data_source_id, point['id'])
# invalid point is found
......@@ -452,7 +457,7 @@ def process(logger, data_source_id, host, port):
" SET last_seen_datetime_utc = '" + current_datetime_utc.isoformat() + "' "
" WHERE id = %s ")
try:
cursor_system_db.execute(update_row, (data_source_id, ))
cursor_system_db.execute(update_row, (data_source_id,))
cnx_system_db.commit()
except Exception as e:
logger.error("Error in step 4.6 of acquisition process " + str(e))
......
......@@ -2,3 +2,4 @@ mysql-connector-python
modbus_tk
schedule
python-decouple
telnetlib3
\ No newline at end of file
import sys
import telnetlib
import telnetlib3
import asyncio
from modbus_tk import modbus_tcp
import byte_swap
########################################################################################################################
# Check connectivity to the host and port
########################################################################################################################
async def check_connectivity(host, port):
reader, writer = await telnetlib3.open_connection(host, port)
# Close the connection
writer.close()
########################################################################################################################
# main procedure
########################################################################################################################
def main():
if len(sys.argv) > 1:
host = sys.argv[1]
......@@ -15,7 +28,7 @@ def main():
port = 502
try:
telnetlib.Telnet(host, port, 10)
asyncio.run(check_connectivity(host, port))
print("Succeeded to telnet %s:%s ", host, port)
except Exception as e:
print("Failed to telnet %s:%s : %s ", host, port, str(e))
......@@ -56,40 +69,17 @@ def main():
master.set_timeout(5.0)
print("Connected to %s:%s ", host, port)
print("read registers...")
result = master.execute(slave=1, function_code=3, starting_address=6401, quantity_of_x=2, data_format='<l')
print("51AL1-1-KWHimp = " + str(byte_swap.byte_swap_32_bit(result[0])))
result = master.execute(slave=1, function_code=3, starting_address=6403, quantity_of_x=2, data_format='<l')
print("51AL2-1-KWHimp = " + str(byte_swap.byte_swap_32_bit(result[0])))
result = master.execute(slave=1, function_code=3, starting_address=6405, quantity_of_x=2, data_format='<l')
print("51AL3-1-KWHimp = " + str(byte_swap.byte_swap_32_bit(result[0])))
result = master.execute(slave=1, function_code=3, starting_address=6407, quantity_of_x=2, data_format='<l')
print("51AL4-1-KWHimp = " + str(byte_swap.byte_swap_32_bit(result[0])))
result = master.execute(slave=1, function_code=3, starting_address=6409, quantity_of_x=2, data_format='<l')
print("51AL5-1-KWHimp = " + str(byte_swap.byte_swap_32_bit(result[0])))
# result = master.execute(slave=1, function_code=3, starting_address=11, quantity_of_x=2, data_format='>f')
# print("Volatage Vc-a = " + str(result))
# result = master.execute(slave=1, function_code=3, starting_address=13, quantity_of_x=2, data_format='>f')
# print("Current a = " + str(result))
# result = master.execute(slave=1, function_code=3, starting_address=15, quantity_of_x=2, data_format='>f')
# print("Current b = " + str(result))
# result = master.execute(slave=1, function_code=3, starting_address=17, quantity_of_x=2, data_format='>f')
# print("Current c = " + str(result))
# result = master.execute(slave=1, function_code=3, starting_address=19, quantity_of_x=2, data_format='>f')
# print("Active Power a = " + str(result))
# result = master.execute(slave=1, function_code=3, starting_address=25, quantity_of_x=2, data_format='>f')
# print("Active Power b = " + str(result))
# result = master.execute(slave=1, function_code=3, starting_address=27, quantity_of_x=2, data_format='>f')
# print("Active Power c = " + str(result))
# result = master.execute(slave=1, function_code=3, starting_address=29, quantity_of_x=2, data_format='>f')
# print("Total Active Power = " + str(result))
# result = master.execute(slave=1, function_code=3, starting_address=65, quantity_of_x=2, data_format='>f')
# print("Total Power Factor = " + str(result))
# result = master.execute(slave=1, function_code=3, starting_address=71, quantity_of_x=2, data_format='>f')
# print("Amplitude Unbalance - Volatage = " + str(result))
# result = master.execute(slave=1, function_code=3, starting_address=73, quantity_of_x=2, data_format='>f')
# print("Amplitude Unbalance - Current = " + str(result))
# result = master.execute(slave=1, function_code=3, starting_address=801, quantity_of_x=4, data_format='>d')
# print("Active Energy Import Tariff 1 = " + str(result))
result = master.execute(slave=1, function_code=3, starting_address=1, quantity_of_x=2, data_format='<f')
print("51AL1 = " + str(byte_swap.byte_swap_32_bit(result[0])))
result = master.execute(slave=1, function_code=3, starting_address=3, quantity_of_x=2, data_format='<f')
print("51AL2 = " + str(byte_swap.byte_swap_32_bit(result[0])))
result = master.execute(slave=1, function_code=3, starting_address=5, quantity_of_x=2, data_format='<f')
print("51AL3 = " + str(byte_swap.byte_swap_32_bit(result[0])))
result = master.execute(slave=1, function_code=3, starting_address=7, quantity_of_x=2, data_format='<f')
print("51AL4 = " + str(byte_swap.byte_swap_32_bit(result[0])))
result = master.execute(slave=1, function_code=3, starting_address=9, quantity_of_x=2, data_format='<f')
print("51AL5 = " + str(byte_swap.byte_swap_32_bit(result[0])))
master.close()
except Exception as e:
print(str(e))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册