-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_api_use.py
53 lines (39 loc) · 2.13 KB
/
example_api_use.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
import asyncio
import logging
import sys
from aioxcom import XcomApiTcp, XcomDataset, VOLTAGE
# Setup logging to StdOut
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logger = logging.getLogger(__name__)
async def main():
dataset = await XcomDataset.create(VOLTAGE.AC240) # or use VOLTAGE.AC120
info_3023 = dataset.getByNr(3023, "xt") # the "xt" part is optional but usefull for detecting mistakes
info_7002 = dataset.getByNr(7002, "bsp")
param_5012 = dataset.getByNr(5012, "rcc")
param_1107 = dataset.getByNr(1107, "xt")
dataset = None # Release memory of the dataset
api = XcomApiTcp(4001) # port number configured in Xcom-LAN/Moxa NPort
try:
if not await api.start():
logger.info(f"Did not connect to Xcom")
return
# Retrieve info #3023 from the first Xtender (Output power)
value = await api.requestValue(info_3023, "XT1") # xt address range is 101 to 109, or use "XT1" to "XT9"
logger.info(f"XT1 {info_3023.nr}: {value} {info_3023.unit} ({info_3023.name})")
# Retrieve info #7002 from BSP (State of Charge)
value = await api.requestValue(info_7002, "BSP") # bsp address range is only 601, or use "BSP"
logger.info(f"BSP {info_7002.nr}: {value} {info_7002.unit} ({info_7002.name})")
# Retrieve param #5012 from RCC (User Level)
value = await api.requestValue(param_5012, "RCC") # rcc address range is only 501, or use "RCC"
logger.info(f"RCC {param_5012.nr}: {param_5012.enum_value(value)} ({param_5012.name})")
# Retrieve and Update param 1107 on the first Xtender (Maximum current of AC source)
value = await api.requestValue(param_1107, "XT1")
logger.info(f"XT1 {param_1107.nr}: {value} {param_1107.unit} ({param_1107.name})")
value = 4.0 # 4 Ampere
if await api.updateValue(param_1107, value, "XT1"):
logger.info(f"XT1 {param_1107.nr} updated to {value} {param_1107.unit} ({param_1107.name})")
except Exception as e:
logger.info(f"Unexpected exception: {e}")
finally:
await api.stop()
asyncio.run(main()) # main loop