Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MyEMS
MyEMS能源管理系统
提交
baabd04e
M
MyEMS能源管理系统
项目概览
MyEMS
/
MyEMS能源管理系统
通知
46
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
MyEMS能源管理系统
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
baabd04e
编写于
5月 19, 2023
作者:
nengyuangzhang
浏览文件
操作
浏览文件
下载
差异文件
Replaced telnetlib with telentlib3 in myems-modbus-tcp
上级
77e10156
b5acc294
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
56 addition
and
58 deletion
+56
-58
CHANGELOG.md
CHANGELOG.md
+1
-0
myems-modbus-tcp/README.md
myems-modbus-tcp/README.md
+1
-0
myems-modbus-tcp/acquisition.py
myems-modbus-tcp/acquisition.py
+27
-22
myems-modbus-tcp/requirements.txt
myems-modbus-tcp/requirements.txt
+1
-0
myems-modbus-tcp/test.py
myems-modbus-tcp/test.py
+26
-36
未找到文件。
CHANGELOG.md
浏览文件 @
baabd04e
...
...
@@ -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
...
...
myems-modbus-tcp/README.md
浏览文件 @
baabd04e
...
...
@@ -14,6 +14,7 @@ schedule
python-decouple
telnetlib3
### Quick Run for Development
...
...
myems-modbus-tcp/acquisition.py
浏览文件 @
baabd04e
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 hos
t
# Step 1:
Check connectivity to the host and por
t
# 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 hos
t
# Step 1:
Check connectivity to the host and por
t
################################################################################################################
try
:
telnetlib
.
Telnet
(
host
,
port
,
10
)
print
(
"Succeeded to
telne
t %s:%s in acquisition process "
,
host
,
port
)
asyncio
.
run
(
check_connectivity
(
host
,
port
)
)
print
(
"Succeeded to
connec
t %s:%s in acquisition process "
,
host
,
port
)
except
Exception
as
e
:
logger
.
error
(
"Failed to
telne
t %s:%s in acquisition process: %s "
,
host
,
port
,
str
(
e
))
logger
.
error
(
"Failed to
connec
t %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
))
...
...
myems-modbus-tcp/requirements.txt
浏览文件 @
baabd04e
...
...
@@ -2,3 +2,4 @@ mysql-connector-python
modbus_tk
schedule
python-decouple
telnetlib3
\ No newline at end of file
myems-modbus-tcp/test.py
浏览文件 @
baabd04e
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录