sciopy 0.7.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sciopy-0.7.1/LICENSE +19 -0
- sciopy-0.7.1/PKG-INFO +60 -0
- sciopy-0.7.1/README.md +45 -0
- sciopy-0.7.1/sciopy/3d_mesh.py +59 -0
- sciopy-0.7.1/sciopy/__init__.py +193 -0
- sciopy-0.7.1/sciopy/com_handling.py +110 -0
- sciopy-0.7.1/sciopy/configurations.py +589 -0
- sciopy-0.7.1/sciopy/doteit.py +190 -0
- sciopy-0.7.1/sciopy/eth_2.py +26 -0
- sciopy-0.7.1/sciopy/eth_3.py +53 -0
- sciopy-0.7.1/sciopy/eth_4.py +62 -0
- sciopy-0.7.1/sciopy/meshing.py +174 -0
- sciopy-0.7.1/sciopy/npztocsv.py +309 -0
- sciopy-0.7.1/sciopy/prepare_data.py +341 -0
- sciopy-0.7.1/sciopy/print_command_info.py +117 -0
- sciopy-0.7.1/sciopy/sciopy_dataclasses.py +166 -0
- sciopy-0.7.1/sciopy/setup_m.py +844 -0
- sciopy-0.7.1/sciopy/usb_hs_handling.py +268 -0
- sciopy-0.7.1/sciopy/visualization.py +236 -0
- sciopy-0.7.1/sciopy.egg-info/PKG-INFO +60 -0
- sciopy-0.7.1/sciopy.egg-info/SOURCES.txt +23 -0
- sciopy-0.7.1/sciopy.egg-info/dependency_links.txt +1 -0
- sciopy-0.7.1/sciopy.egg-info/top_level.txt +1 -0
- sciopy-0.7.1/setup.cfg +4 -0
- sciopy-0.7.1/setup.py +19 -0
sciopy-0.7.1/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2023 Jacob Peter Thönes
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
sciopy-0.7.1/PKG-INFO
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: sciopy
|
|
3
|
+
Version: 0.7.1
|
|
4
|
+
Summary: Python based interface module for communication with the Sciospec Electrical Impedance Tomography (EIT) device.
|
|
5
|
+
Home-page: https://github.com/spatialaudio/sciopy.git
|
|
6
|
+
Author: Jacob Peter Thönes
|
|
7
|
+
Author-email: jacob.thoenes@uni-rostock.de
|
|
8
|
+
Keywords: Sciospec,EIT
|
|
9
|
+
Platform: any
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
|
|
16
|
+
# 
|
|
17
|
+
|
|
18
|
+
This package offers the serial interface for communication with an EIT device from ScioSpec. Commands can be written serially and the system response can be read out. With the current version, it is possible to start and stop measurements with defined burst counts and to read out the measurement data. In addition, the measurement data is packed into a data class for better further processing.
|
|
19
|
+
|
|
20
|
+
## Install Requirements
|
|
21
|
+
|
|
22
|
+
All requirements are provided inside the `requirements.txt`. To install them navigate inside the sciopy directory and type:
|
|
23
|
+
|
|
24
|
+
pip3 install -r requirements.txt # Linux, macOS, Windows
|
|
25
|
+
pip install -r requirements.txt # Windows
|
|
26
|
+
|
|
27
|
+
## Run Example Script
|
|
28
|
+
|
|
29
|
+
For a single measurement, you can simply run one of the `example` scripts using the command:
|
|
30
|
+
|
|
31
|
+
python custom_measurement.py
|
|
32
|
+
|
|
33
|
+
To find the right port you can use:
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
from sciopy import available_serial_ports
|
|
37
|
+
available_serial_ports()
|
|
38
|
+
|
|
39
|
+
This script establishes a serial connection to the ScioSpec device, sends the measurement configuration, and starts a
|
|
40
|
+
measurement. For a successful measurement with 16 electrodes, you have to plug in the 16 electrodes to the port "channel 1-16" at the ScioSpec device. For saving the data, set `save = True` and insert a valid `s_path` to the `scio_spec_measurement_config` at the beginning of the script. If you don't change the path the files will be saved to the current directory.
|
|
41
|
+
|
|
42
|
+
The example script `prep_data_for_ml.py` can be used for the conversion of a finished measurement.
|
|
43
|
+
This script creates a new folder with the ending `_prepared` and puts together the potential values and object positions for all measurements. This could be useful for later application of machine learning.
|
|
44
|
+
|
|
45
|
+
## Explanation of stored files (.npz)
|
|
46
|
+
|
|
47
|
+
- `potential matrix` (e.g. variable P) is a 16x16 matrix (n_el=16). If you visualize it using `from sciopy import plot_potential_matrix` you can recognize the used excitation pattern or if an electrode is a defect.
|
|
48
|
+
- `p_with_exc` is the matrix P with the excitation electrodes
|
|
49
|
+
- `p_without_exc` is the matrix P without the excitation electrodes
|
|
50
|
+
- `abs_p_norm_without_ext` is the matrix, normalized between (I think) 0-1 without the excitation electrodes.
|
|
51
|
+
- `v_with_ext` is the computed voltage from the potential values containing the excitation electrodes
|
|
52
|
+
- `v_without_ext` is the computed voltage from the potential values without the excitation electrodes
|
|
53
|
+
- `abs_v_norm_without_ext` is the voltage data, normalized between 0-1 without the excitation electrodes.
|
|
54
|
+
- `config` contains some information regarding the measurement procedure.
|
|
55
|
+
|
|
56
|
+
## Contact
|
|
57
|
+
|
|
58
|
+
If you have ideas or other advice don't hesitate to contact me!
|
|
59
|
+
|
|
60
|
+
Email: jacob.thoenes@uni-rostock.de
|
sciopy-0.7.1/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# 
|
|
2
|
+
|
|
3
|
+
This package offers the serial interface for communication with an EIT device from ScioSpec. Commands can be written serially and the system response can be read out. With the current version, it is possible to start and stop measurements with defined burst counts and to read out the measurement data. In addition, the measurement data is packed into a data class for better further processing.
|
|
4
|
+
|
|
5
|
+
## Install Requirements
|
|
6
|
+
|
|
7
|
+
All requirements are provided inside the `requirements.txt`. To install them navigate inside the sciopy directory and type:
|
|
8
|
+
|
|
9
|
+
pip3 install -r requirements.txt # Linux, macOS, Windows
|
|
10
|
+
pip install -r requirements.txt # Windows
|
|
11
|
+
|
|
12
|
+
## Run Example Script
|
|
13
|
+
|
|
14
|
+
For a single measurement, you can simply run one of the `example` scripts using the command:
|
|
15
|
+
|
|
16
|
+
python custom_measurement.py
|
|
17
|
+
|
|
18
|
+
To find the right port you can use:
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
from sciopy import available_serial_ports
|
|
22
|
+
available_serial_ports()
|
|
23
|
+
|
|
24
|
+
This script establishes a serial connection to the ScioSpec device, sends the measurement configuration, and starts a
|
|
25
|
+
measurement. For a successful measurement with 16 electrodes, you have to plug in the 16 electrodes to the port "channel 1-16" at the ScioSpec device. For saving the data, set `save = True` and insert a valid `s_path` to the `scio_spec_measurement_config` at the beginning of the script. If you don't change the path the files will be saved to the current directory.
|
|
26
|
+
|
|
27
|
+
The example script `prep_data_for_ml.py` can be used for the conversion of a finished measurement.
|
|
28
|
+
This script creates a new folder with the ending `_prepared` and puts together the potential values and object positions for all measurements. This could be useful for later application of machine learning.
|
|
29
|
+
|
|
30
|
+
## Explanation of stored files (.npz)
|
|
31
|
+
|
|
32
|
+
- `potential matrix` (e.g. variable P) is a 16x16 matrix (n_el=16). If you visualize it using `from sciopy import plot_potential_matrix` you can recognize the used excitation pattern or if an electrode is a defect.
|
|
33
|
+
- `p_with_exc` is the matrix P with the excitation electrodes
|
|
34
|
+
- `p_without_exc` is the matrix P without the excitation electrodes
|
|
35
|
+
- `abs_p_norm_without_ext` is the matrix, normalized between (I think) 0-1 without the excitation electrodes.
|
|
36
|
+
- `v_with_ext` is the computed voltage from the potential values containing the excitation electrodes
|
|
37
|
+
- `v_without_ext` is the computed voltage from the potential values without the excitation electrodes
|
|
38
|
+
- `abs_v_norm_without_ext` is the voltage data, normalized between 0-1 without the excitation electrodes.
|
|
39
|
+
- `config` contains some information regarding the measurement procedure.
|
|
40
|
+
|
|
41
|
+
## Contact
|
|
42
|
+
|
|
43
|
+
If you have ideas or other advice don't hesitate to contact me!
|
|
44
|
+
|
|
45
|
+
Email: jacob.thoenes@uni-rostock.de
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# TBD
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
import pyvista as pv
|
|
5
|
+
from pyvista import CellType
|
|
6
|
+
|
|
7
|
+
from tempfile import NamedTemporaryFile
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
import pyvista
|
|
12
|
+
from pyvista import examples
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# There are 10 cells here, each cell is [4, INDEX0, INDEX1, INDEX2, INDEX3]
|
|
16
|
+
# where INDEX is one of the corners of the tetrahedron.
|
|
17
|
+
#
|
|
18
|
+
# Note that the array does not need to be shaped like this, we could have a
|
|
19
|
+
# flat array, but it's easier to make out the structure of the array this way.
|
|
20
|
+
cells = np.array(
|
|
21
|
+
[
|
|
22
|
+
[4, 6, 5, 8, 7],
|
|
23
|
+
[4, 7, 3, 8, 9],
|
|
24
|
+
[4, 7, 3, 1, 5],
|
|
25
|
+
[4, 9, 3, 1, 7],
|
|
26
|
+
[4, 2, 6, 5, 8],
|
|
27
|
+
[4, 2, 6, 0, 4],
|
|
28
|
+
[4, 6, 2, 0, 8],
|
|
29
|
+
[4, 5, 2, 8, 3],
|
|
30
|
+
[4, 5, 3, 8, 7],
|
|
31
|
+
[4, 2, 6, 4, 5],
|
|
32
|
+
]
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
celltypes = np.full(10, fill_value=CellType.TETRA, dtype=np.uint8)
|
|
36
|
+
|
|
37
|
+
# These are the 10 points. The number of cells does not need to match the
|
|
38
|
+
# number of points, they just happen to in this example
|
|
39
|
+
points = np.array(
|
|
40
|
+
[
|
|
41
|
+
[-0.0, 0.0, -0.5],
|
|
42
|
+
[0.0, 0.0, 0.5],
|
|
43
|
+
[-0.43, 0.0, -0.25],
|
|
44
|
+
[-0.43, 0.0, 0.25],
|
|
45
|
+
[-0.0, 0.43, -0.25],
|
|
46
|
+
[0.0, 0.43, 0.25],
|
|
47
|
+
[0.43, 0.0, -0.25],
|
|
48
|
+
[0.43, 0.0, 0.25],
|
|
49
|
+
[0.0, -0.43, -0.25],
|
|
50
|
+
[0.0, -0.43, 0.25],
|
|
51
|
+
]
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
# Create and plot the unstructured grid
|
|
55
|
+
grid = pv.UnstructuredGrid(cells, celltypes, points)
|
|
56
|
+
grid.plot(show_edges=True)
|
|
57
|
+
|
|
58
|
+
split_cells = grid.explode(1.5)
|
|
59
|
+
split_cells.plot(show_edges=True, ssao=True)
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
from .doteit import (
|
|
2
|
+
doteit_in_SingleEitFrame,
|
|
3
|
+
list_eit_files,
|
|
4
|
+
list_all_files,
|
|
5
|
+
single_eit_in_pickle,
|
|
6
|
+
load_pickle_to_dict,
|
|
7
|
+
convert_fulldir_doteit_to_pickle,
|
|
8
|
+
convert_fulldir_doteit_to_npz,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
from .com_handling import (
|
|
12
|
+
available_serial_ports,
|
|
13
|
+
connect_COM_port,
|
|
14
|
+
serial_write,
|
|
15
|
+
disconnect_COM_port,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
from .usb_hs_handling import (
|
|
19
|
+
connect_COM_port_usb_hs,
|
|
20
|
+
set_measurement_config_usb_hs,
|
|
21
|
+
SystemMessageCallback_usb_hs,
|
|
22
|
+
StartStopMeasurement_usb_hs,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
from .print_command_info import (
|
|
26
|
+
print_syntax,
|
|
27
|
+
print_general_system_messages,
|
|
28
|
+
print_acknowledge_messages,
|
|
29
|
+
print_command_list,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
from .prepare_data import (
|
|
33
|
+
create_prep_directory,
|
|
34
|
+
extract_potentials_from_sample_n_el_16,
|
|
35
|
+
comp_tank_relative_r_phi,
|
|
36
|
+
check_n_el_condition,
|
|
37
|
+
extract_electrodepotentials,
|
|
38
|
+
norm_data,
|
|
39
|
+
prepare_all_samples_for_16_el,
|
|
40
|
+
prepare_all_samples_for_16_el_single,
|
|
41
|
+
compute_v,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
from .visualization import (
|
|
45
|
+
plot_el_sign,
|
|
46
|
+
plot_potential_matrix,
|
|
47
|
+
plot_completeness,
|
|
48
|
+
plot_temperatur_curve,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
from .setup_m import (
|
|
52
|
+
SystemMessageCallback,
|
|
53
|
+
SaveSettings,
|
|
54
|
+
SoftwareReset,
|
|
55
|
+
ResetMeasurementSetup,
|
|
56
|
+
SetMeasurementSetup,
|
|
57
|
+
GetMeasurementSetup,
|
|
58
|
+
SetBurstCount,
|
|
59
|
+
StartStopMeasurement,
|
|
60
|
+
single_hex_to_int,
|
|
61
|
+
del_hex_in_list,
|
|
62
|
+
bytesarray_to_float,
|
|
63
|
+
bytesarray_to_int,
|
|
64
|
+
bytesarray_to_byteslist,
|
|
65
|
+
reduce_burst_to_less_x,
|
|
66
|
+
reduce_burst_to_available_parts,
|
|
67
|
+
parse_single_frame,
|
|
68
|
+
reshape_full_message_in_bursts,
|
|
69
|
+
split_bursts_in_frames,
|
|
70
|
+
GetTemperature,
|
|
71
|
+
SetBatteryControll,
|
|
72
|
+
GetBatteryControll,
|
|
73
|
+
SetLEDControl,
|
|
74
|
+
GetLEDControl,
|
|
75
|
+
SetLED_Mode,
|
|
76
|
+
DisableLED_AutoMode,
|
|
77
|
+
EnableLED_AutoMode,
|
|
78
|
+
PowerPlugDetect,
|
|
79
|
+
GetDeviceInfo,
|
|
80
|
+
GetFirmwareIDs,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
from .configurations import (
|
|
84
|
+
set_measurement_config,
|
|
85
|
+
conf_n_el_16_adjacent,
|
|
86
|
+
conf_n_el_32_adjacent,
|
|
87
|
+
conf_n_el_16_opposite,
|
|
88
|
+
conf_n_el_32_opposite,
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
from .npztocsv import (
|
|
92
|
+
clear_s_dict,
|
|
93
|
+
single_measurement_to_csv_n_el_16,
|
|
94
|
+
convert_measurement_directory_n_el_16,
|
|
95
|
+
convert_measurement_directory_n_el_16_r_split,
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
from .meshing import (
|
|
99
|
+
create_empty_2d_mesh,
|
|
100
|
+
add_circle_anomaly,
|
|
101
|
+
plot_mesh,
|
|
102
|
+
mesh_sample,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
# TBD from .configurations import configuration_01
|
|
106
|
+
|
|
107
|
+
__all__ = [
|
|
108
|
+
# .doteit
|
|
109
|
+
"doteit_in_SingleEitFrame",
|
|
110
|
+
"list_eit_files",
|
|
111
|
+
"list_all_files",
|
|
112
|
+
"single_eit_in_pickle",
|
|
113
|
+
"load_pickle_to_dict",
|
|
114
|
+
"convert_fulldir_doteit_to_pickle",
|
|
115
|
+
"convert_fulldir_doteit_to_npz",
|
|
116
|
+
# .com_handling
|
|
117
|
+
"available_serial_ports",
|
|
118
|
+
"connect_COM_port",
|
|
119
|
+
"serial_write",
|
|
120
|
+
"disconnect_COM_port",
|
|
121
|
+
# .usb_hs_handling
|
|
122
|
+
"connect_COM_port_usb_hs",
|
|
123
|
+
"set_measurement_config_usb_hs",
|
|
124
|
+
"SystemMessageCallback_usb_hs",
|
|
125
|
+
"StartStopMeasurement_usb_hs",
|
|
126
|
+
# .print_command_info
|
|
127
|
+
"print_syntax",
|
|
128
|
+
"print_general_system_messages",
|
|
129
|
+
"print_acknowledge_messages",
|
|
130
|
+
"print_command_list",
|
|
131
|
+
# .setup_m
|
|
132
|
+
"SystemMessageCallback",
|
|
133
|
+
"SaveSettings",
|
|
134
|
+
"SoftwareReset",
|
|
135
|
+
"ResetMeasurementSetup",
|
|
136
|
+
"SetMeasurementSetup",
|
|
137
|
+
"GetMeasurementSetup",
|
|
138
|
+
"SetBurstCount",
|
|
139
|
+
"StartStopMeasurement",
|
|
140
|
+
"single_hex_to_int",
|
|
141
|
+
"del_hex_in_list",
|
|
142
|
+
"bytesarray_to_float",
|
|
143
|
+
"bytesarray_to_int",
|
|
144
|
+
"bytesarray_to_byteslist",
|
|
145
|
+
"reduce_burst_to_less_x",
|
|
146
|
+
"reduce_burst_to_available_parts",
|
|
147
|
+
"parse_single_frame",
|
|
148
|
+
"reshape_full_message_in_bursts",
|
|
149
|
+
"split_bursts_in_frames",
|
|
150
|
+
"GetTemperature",
|
|
151
|
+
"SetBatteryControll",
|
|
152
|
+
"GetBatteryControll",
|
|
153
|
+
"SetLEDControl",
|
|
154
|
+
"GetLEDControl",
|
|
155
|
+
"SetLED_Mode",
|
|
156
|
+
"DisableLED_AutoMode",
|
|
157
|
+
"EnableLED_AutoMode",
|
|
158
|
+
"PowerPlugDetect",
|
|
159
|
+
"GetDeviceInfo",
|
|
160
|
+
"GetFirmwareIDs",
|
|
161
|
+
# .configurations
|
|
162
|
+
"set_measurement_config",
|
|
163
|
+
"conf_n_el_16_adjacent",
|
|
164
|
+
"conf_n_el_32_adjacent",
|
|
165
|
+
"conf_n_el_16_opposite",
|
|
166
|
+
"conf_n_el_32_opposite",
|
|
167
|
+
# "configure_configuration",
|
|
168
|
+
# .prepare_data
|
|
169
|
+
"create_prep_directory",
|
|
170
|
+
"extract_potentials_from_sample_n_el_16",
|
|
171
|
+
"comp_tank_relative_r_phi",
|
|
172
|
+
"check_n_el_condition",
|
|
173
|
+
"extract_electrodepotentials",
|
|
174
|
+
"norm_data",
|
|
175
|
+
"prepare_all_samples_for_16_el",
|
|
176
|
+
"prepare_all_samples_for_16_el_single",
|
|
177
|
+
"compute_v",
|
|
178
|
+
# .visualization
|
|
179
|
+
"plot_potential_matrix",
|
|
180
|
+
"plot_el_sign",
|
|
181
|
+
"plot_completeness",
|
|
182
|
+
"plot_temperatur_curve",
|
|
183
|
+
# .npztocsv
|
|
184
|
+
"clear_s_dict",
|
|
185
|
+
"single_measurement_to_csv_n_el_16",
|
|
186
|
+
"convert_measurement_directory_n_el_16",
|
|
187
|
+
"convert_measurement_directory_n_el_16_r_split",
|
|
188
|
+
# .meshing
|
|
189
|
+
"create_empty_2d_mesh",
|
|
190
|
+
"add_circle_anomaly",
|
|
191
|
+
"plot_mesh",
|
|
192
|
+
"mesh_sample",
|
|
193
|
+
]
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
try:
|
|
2
|
+
import serial
|
|
3
|
+
except ImportError:
|
|
4
|
+
print("Could not import module: serial")
|
|
5
|
+
|
|
6
|
+
import sys
|
|
7
|
+
import glob
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def available_serial_ports() -> list:
|
|
11
|
+
"""
|
|
12
|
+
Lists serial port names.
|
|
13
|
+
|
|
14
|
+
Returns
|
|
15
|
+
-------
|
|
16
|
+
list
|
|
17
|
+
a list of the serial ports available on the system
|
|
18
|
+
|
|
19
|
+
Raises
|
|
20
|
+
------
|
|
21
|
+
EnvironmentError
|
|
22
|
+
on unsupported or unknown platforms
|
|
23
|
+
OtherError
|
|
24
|
+
when an other error
|
|
25
|
+
"""
|
|
26
|
+
if sys.platform.startswith("win"):
|
|
27
|
+
ports = ["COM%s" % (i + 1) for i in range(256)]
|
|
28
|
+
elif sys.platform.startswith("linux") or sys.platform.startswith("cygwin"):
|
|
29
|
+
# this excludes your current terminal "/dev/tty"
|
|
30
|
+
ports = glob.glob("/dev/tty[A-Za-z]*")
|
|
31
|
+
elif sys.platform.startswith("darwin"):
|
|
32
|
+
ports = glob.glob("/dev/tty.*")
|
|
33
|
+
else:
|
|
34
|
+
raise EnvironmentError("Unsupported platform")
|
|
35
|
+
|
|
36
|
+
result = []
|
|
37
|
+
for port in ports:
|
|
38
|
+
try:
|
|
39
|
+
s = serial.Serial(port)
|
|
40
|
+
s.close()
|
|
41
|
+
result.append(port)
|
|
42
|
+
except (OSError, serial.SerialException):
|
|
43
|
+
pass
|
|
44
|
+
return result
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def connect_COM_port(port: str = "COM3", baudrate: int = 9600, timeout: int = 1):
|
|
48
|
+
"""
|
|
49
|
+
Etablishes a serial connection to a com port.
|
|
50
|
+
|
|
51
|
+
Parameters
|
|
52
|
+
----------
|
|
53
|
+
port : str = "COM3"
|
|
54
|
+
name of the com port
|
|
55
|
+
baudrate : int = 9600
|
|
56
|
+
communication rate
|
|
57
|
+
timeout : = 1
|
|
58
|
+
timeout in seconds if no connection is available
|
|
59
|
+
|
|
60
|
+
Returns
|
|
61
|
+
-------
|
|
62
|
+
serial
|
|
63
|
+
a serial connection
|
|
64
|
+
"""
|
|
65
|
+
ser = serial.Serial(
|
|
66
|
+
port=port,
|
|
67
|
+
baudrate=baudrate,
|
|
68
|
+
timeout=timeout,
|
|
69
|
+
parity=serial.PARITY_NONE,
|
|
70
|
+
stopbits=serial.STOPBITS_ONE,
|
|
71
|
+
bytesize=serial.EIGHTBITS,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
print("Connection to", ser.name, "is established.")
|
|
75
|
+
return ser
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def serial_write(ser, cmd: str) -> None:
|
|
79
|
+
"""
|
|
80
|
+
Write a string to a serial connection.
|
|
81
|
+
|
|
82
|
+
Parameters
|
|
83
|
+
----------
|
|
84
|
+
ser :
|
|
85
|
+
serial connection
|
|
86
|
+
cmd : str
|
|
87
|
+
message to write
|
|
88
|
+
|
|
89
|
+
Returns
|
|
90
|
+
-------
|
|
91
|
+
None
|
|
92
|
+
"""
|
|
93
|
+
ser.write(cmd)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def disconnect_COM_port(ser) -> None:
|
|
97
|
+
"""
|
|
98
|
+
Disconnect serial connection.
|
|
99
|
+
|
|
100
|
+
Parameters
|
|
101
|
+
----------
|
|
102
|
+
ser :
|
|
103
|
+
serial connection
|
|
104
|
+
|
|
105
|
+
Returns
|
|
106
|
+
-------
|
|
107
|
+
None
|
|
108
|
+
"""
|
|
109
|
+
ser.close()
|
|
110
|
+
print("Disconnected from {port}.".format(port=ser.name))
|