Conditions | 4 |
Total Lines | 87 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import sys |
||
7 | def main(): |
||
8 | if len(sys.argv) > 1: |
||
9 | host = sys.argv[1] |
||
10 | else: |
||
11 | print('Usage: python3 test.py HOST_IP_ADDR ') |
||
12 | return |
||
13 | |||
14 | port = 502 |
||
15 | try: |
||
16 | telnetlib.Telnet(host, port, 10) |
||
17 | print("Succeeded to telnet %s:%s ", host, port) |
||
18 | except Exception as e: |
||
19 | print("Failed to telnet %s:%s : %s ", host, port, str(e)) |
||
20 | return |
||
21 | |||
22 | """ |
||
23 | Functions to convert between Python values and C structs. |
||
24 | Python bytes objects are used to hold the data representing the C struct |
||
25 | and also as format strings (explained below) to describe the layout of data |
||
26 | in the C struct. |
||
27 | |||
28 | The optional first format char indicates byte order, size and alignment: |
||
29 | @: native order, size & alignment (default) |
||
30 | =: native order, std. size & alignment |
||
31 | <: little-endian, std. size & alignment |
||
32 | >: big-endian, std. size & alignment |
||
33 | !: same as > |
||
34 | |||
35 | The remaining chars indicate types of args and must match exactly; |
||
36 | these can be preceded by a decimal repeat count: |
||
37 | x: pad byte (no data); c:char; b:signed byte; B:unsigned byte; |
||
38 | ?: _Bool (requires C99; if not available, char is used instead) |
||
39 | h:short; H:unsigned short; i:int; I:unsigned int; |
||
40 | l:long; L:unsigned long; f:float; d:double. |
||
41 | Special cases (preceding decimal count indicates length): |
||
42 | s:string (array of char); p: pascal string (with count byte). |
||
43 | Special cases (only available in native format): |
||
44 | n:ssize_t; N:size_t; |
||
45 | P:an integer type that is wide enough to hold a pointer. |
||
46 | Special case (not in native mode unless 'long long' in platform C): |
||
47 | q:long long; Q:unsigned long long |
||
48 | Whitespace between formats is ignored. |
||
49 | |||
50 | The variable struct.error is an exception raised on errors. |
||
51 | """ |
||
52 | try: |
||
53 | master = modbus_tcp.TcpMaster(host=host, port=port, timeout_in_sec=5.0) |
||
54 | master.set_timeout(5.0) |
||
55 | print("Connected to %s:%s ", host, port) |
||
56 | print("read registers...") |
||
57 | result = master.execute(slave=1, function_code=3, starting_address=6401, quantity_of_x=2, data_format='<l') |
||
58 | print("51AL1-1-KWHimp = " + str(byte_swap.byte_swap_32_bit(result[0]))) |
||
59 | result = master.execute(slave=1, function_code=3, starting_address=6403, quantity_of_x=2, data_format='<l') |
||
60 | print("51AL2-1-KWHimp = " + str(byte_swap.byte_swap_32_bit(result[0]))) |
||
61 | result = master.execute(slave=1, function_code=3, starting_address=6405, quantity_of_x=2, data_format='<l') |
||
62 | print("51AL3-1-KWHimp = " + str(byte_swap.byte_swap_32_bit(result[0]))) |
||
63 | result = master.execute(slave=1, function_code=3, starting_address=6407, quantity_of_x=2, data_format='<l') |
||
64 | print("51AL4-1-KWHimp = " + str(byte_swap.byte_swap_32_bit(result[0]))) |
||
65 | result = master.execute(slave=1, function_code=3, starting_address=6409, quantity_of_x=2, data_format='<l') |
||
66 | print("51AL5-1-KWHimp = " + str(byte_swap.byte_swap_32_bit(result[0]))) |
||
67 | # result = master.execute(slave=1, function_code=3, starting_address=11, quantity_of_x=2, data_format='>f') |
||
68 | # print("Volatage Vc-a = " + str(result)) |
||
69 | # result = master.execute(slave=1, function_code=3, starting_address=13, quantity_of_x=2, data_format='>f') |
||
70 | # print("Current a = " + str(result)) |
||
71 | # result = master.execute(slave=1, function_code=3, starting_address=15, quantity_of_x=2, data_format='>f') |
||
72 | # print("Current b = " + str(result)) |
||
73 | # result = master.execute(slave=1, function_code=3, starting_address=17, quantity_of_x=2, data_format='>f') |
||
74 | # print("Current c = " + str(result)) |
||
75 | # result = master.execute(slave=1, function_code=3, starting_address=19, quantity_of_x=2, data_format='>f') |
||
76 | # print("Active Power a = " + str(result)) |
||
77 | # result = master.execute(slave=1, function_code=3, starting_address=25, quantity_of_x=2, data_format='>f') |
||
78 | # print("Active Power b = " + str(result)) |
||
79 | # result = master.execute(slave=1, function_code=3, starting_address=27, quantity_of_x=2, data_format='>f') |
||
80 | # print("Active Power c = " + str(result)) |
||
81 | # result = master.execute(slave=1, function_code=3, starting_address=29, quantity_of_x=2, data_format='>f') |
||
82 | # print("Total Active Power = " + str(result)) |
||
83 | # result = master.execute(slave=1, function_code=3, starting_address=65, quantity_of_x=2, data_format='>f') |
||
84 | # print("Total Power Factor = " + str(result)) |
||
85 | # result = master.execute(slave=1, function_code=3, starting_address=71, quantity_of_x=2, data_format='>f') |
||
86 | # print("Amplitude Unbalance - Volatage = " + str(result)) |
||
87 | # result = master.execute(slave=1, function_code=3, starting_address=73, quantity_of_x=2, data_format='>f') |
||
88 | # print("Amplitude Unbalance - Current = " + str(result)) |
||
89 | # result = master.execute(slave=1, function_code=3, starting_address=801, quantity_of_x=4, data_format='>d') |
||
90 | # print("Active Energy Import Tariff 1 = " + str(result)) |
||
91 | master.close() |
||
92 | except Exception as e: |
||
93 | print(str(e)) |
||
94 | |||
98 |