TC read channel data

 1'''
 2Temperature_TC - TC_read_channel_data.py with asynchronous mode.
 3
 4This example demonstrates how to read thermocouple from EthanT.
 5
 6First, it shows how to open thermal port and configure thermal parameters.
 7Second, read channel 1 thermocouple data.
 8Last, close thermal port.
 9
10-------------------------------------------------------------------------------------
11Please change correct serial number or IP and port number BEFORE you run example code.
12
13For other examples please check:
14    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
15See README.md file to get detailed usage of this example.
16
17Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
18'''
19
20## Python
21import asyncio
22
23## WPC
24
25from wpcsys import pywpc
26
27async def main():
28    ## Get Python driver version
29    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
30
31    ## Create device handle
32    dev = pywpc.EthanT()
33
34    ## Connect to device
35    try:
36        dev.connect("192.168.1.110") ## Depend on your device
37    except Exception as err:
38        pywpc.printGenericError(err)
39        ## Release device handle
40        dev.close()
41        return
42
43    try:
44        ## Parameters setting
45        port = 1 ## Depend on your device
46        ch = 1
47        over_sampling_mode = 0  ## 0:1 sample, 1:2 samples, 2:4 sample, 3:8 samples, 4:16 samples
48        thermo_type = 3         ## 0:B type, 1:E type, 2:J type, 3:K type
49                                ## 4:N type, 5:R type, 6:S type, 7:T type
50
51        ## Get firmware model & version
52        driver_info = await dev.Sys_getDriverInfo_async()
53        print("Model name: " + driver_info[0])
54        print("Firmware version: " + driver_info[-1])
55
56        ## Open thermo
57        err = await dev.Thermal_open_async(port)
58        print(f"Thermal_open_async in port {port}, status: {err}")
59
60        ## Set thermo port and set over-sampling mode to no over-sampling in channel 1
61        err = await dev.Thermal_setOverSampling_async(port, ch, over_sampling_mode)
62        print(f"Thermal_setOverSampling_async in port {port}, status: {err}")
63
64        ## Set thermo port and set K type in channel 1
65        err = await dev.Thermal_setType_async(port, ch, thermo_type)
66        print(f"Thermal_setType_async in port {port}, status: {err}")
67
68        ## Wait for at least 500 ms after setting type or oversampling
69        await asyncio.sleep(0.5) ## delay [s]
70
71        ## Set thermo port and read thermo in channel 1
72        data = await dev.Thermal_readSensor_async(port, ch)
73        print(f"Read sensor in channel {ch} in port {port}: {data} deg C")
74
75        ## Close thermo
76        err = await dev.Thermal_close_async(port)
77        print(f"Thermal_close_async in port {port}, status: {err}")
78    except Exception as err:
79        pywpc.printGenericError(err)
80
81    ## Disconnect device
82    dev.disconnect()
83
84    ## Release device handle
85    dev.close()
86
87    return
88
89def main_for_spyder(*args):
90    if asyncio.get_event_loop().is_running():
91        return asyncio.create_task(main(*args)).result()
92    else:
93        return asyncio.run(main(*args))
94
95if __name__ == '__main__':
96    asyncio.run(main()) ## Use terminal
97    # await main() ## Use Jupyter or IPython(>=7.0)
98    # main_for_spyder() ## Use Spyder