-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
85 lines (74 loc) · 3.25 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import sys
from modbus_tk import modbus_tcp
import telnetlib
import time
import config
def main():
if len(sys.argv) > 2:
host = sys.argv[1]
port = sys.argv[2]
else:
print('Usage: python3 main.py HOST PORT ')
return
try:
telnetlib.Telnet(host, int(port), 10)
print("Succeeded to telnet %s:%s ", host, port)
except Exception as e:
print("Failed to telnet %s:%s : %s ", host, port, str(e))
return
"""
Functions to convert between Python values and C structs.
Python bytes objects are used to hold the data representing the C struct
and also as format strings (explained below) to describe the layout of data
in the C struct.
The optional first format char indicates byte order, size and alignment:
@: native order, size & alignment (default)
=: native order, std. size & alignment
<: little-endian, std. size & alignment
>: big-endian, std. size & alignment
!: same as >
The remaining chars indicate types of args and must match exactly;
these can be preceded by a decimal repeat count:
x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;
?: _Bool (requires C99; if not available, char is used instead)
h:short; H:unsigned short; i:int; I:unsigned int;
l:long; L:unsigned long; f:float; d:double.
Special cases (preceding decimal count indicates length):
s:string (array of char); p: pascal string (with count byte).
Special cases (only available in native format):
n:ssize_t; N:size_t;
P:an integer type that is wide enough to hold a pointer.
Special case (not in native mode unless 'long long' in platform C):
q:long long; Q:unsigned long long
Whitespace between formats is ignored.
The variable struct.error is an exception raised on errors.
"""
try:
master = modbus_tcp.TcpMaster(host=host, port=int(port), timeout_in_sec=5.0)
master.set_timeout(5.0)
print("Connected to %s:%s ", host, port)
while True:
print("read registers...")
# point_id : 0
r0 = master.execute(slave=1, function_code=3, starting_address=0, quantity_of_x=2, data_format='>l')
print("r0 = " + str(r0))
# point_id : 1
r1 = master.execute(slave=1, function_code=3, starting_address=2, quantity_of_x=2, data_format='>l')
print("r1 = " + str(r1))
# point_id : 2
r2 = master.execute(slave=1, function_code=3, starting_address=4, quantity_of_x=2, data_format='>l')
print("r2 = " + str(r2))
# point_id : 3
r3 = master.execute(slave=1, function_code=3, starting_address=6, quantity_of_x=2, data_format='>l')
print("r3 = " + str(r3))
# point_id : 4
r4 = master.execute(slave=1, function_code=3, starting_address=8, quantity_of_x=2, data_format='>l')
print("r4 = " + str(r4))
# todo: save point_id, datetime and value to database
time.sleep(config.interval_in_seconds)
except Exception as e:
print(str(e))
finally:
master.close()
if __name__ == "__main__":
main()