Python first version

implementation of the python program, with parser and crc verification
This commit is contained in:
Martin Girard committed 2026-09-15 17:37:30 +02:00
1 parent 93054253e6
commit c6b299e8ed
6 files changed
+117

No files matched your search

+1
View File
@@ -0,0 +1 @@
__pycache__/
+1
View File
@@ -0,0 +1 @@
3.13
View File
Whitespace-only changes.
+19
View File
@@ -0,0 +1,19 @@
[project]
name = "bench-data-connector"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
authors = [
{ name = "Martin Girard", email = "mgkik@outlook.fr" }
]
requires-python = ">=3.13"
dependencies = [
"pyserial>=3.5",
]
[project.scripts]
bench-data-connector = "bench_data_connector:main"
[build-system]
requires = ["uv_build>=0.12.12,<0.13.0"]
build-backend = "uv_build"
@@ -0,0 +1,73 @@
from serial.tools import list_ports
from serial import Serial
import struct
def main() -> None:
try:
print("Hello from bench-data-connector!")
choice = None
ports = list(list_ports.comports())
for index, port in enumerate(ports):
print(f"{index}: {port.device} {port.description}")
try:
choice = int(input("Choose a port to connect: "))
except ValueError:
pass
while choice is None or choice >= len(ports):
try:
choice = int(input("Unknown port. Try again : "))
except ValueError:
pass
selected_port = ports[choice].device
print(f"Selected port : {selected_port}")
except KeyboardInterrupt:
print("\n")
exit(0)
with Serial(selected_port,baudrate=115200,timeout=1) as ser:
while True:
start_sync(ser)
print("paquet trouvé")
payload = ser.read(9) # 9 corresponds to the payload size of the data frame
if not payload:
continue
crc = ser.read(2)
if not crc:
continue
received_crc = struct.unpack("<H", crc)[0]
calculated_crc = crc16_ccitt(payload)
if calculated_crc != received_crc:
continue
print("paquet sécurisé")
def crc16_ccitt(data: bytes) -> int:
crc = 0xFFFF
for byte in data:
crc ^= byte << 8
for _ in range(8):
if crc & 0x8000:
crc = (crc << 1) ^ 0x1021
else:
crc <<= 1
crc &= 0xFFFF
return crc
def start_sync(ser: Serial) -> None:
previous = None
while True:
byte = ser.read(1)
if not byte :
continue
if previous == b"\xEF" and byte == b"\xBE" :
return
previous = byte
+23
View File
@@ -0,0 +1,23 @@
version = 1
revision = 3
requires-python = ">=3.13"
[[package]]
name = "bench-data-connector"
version = "0.1.0"
source = { editable = "." }
dependencies = [
{ name = "pyserial" },
]
[package.metadata]
requires-dist = [{ name = "pyserial", specifier = ">=3.5" }]
[[package]]
name = "pyserial"
version = "3.5"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/1e/7d/ae3f0a63f41e4d2f6cb66a5b57197850f919f59e558159a4dd3a818f5082/pyserial-3.5.tar.gz", hash = "sha256:3c77e014170dfffbd816e6ffc205e9842efb10be9f58ec16d3e8675b4925cddb", size = 159125, upload-time = "2020-11-23T03:59:15.045Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/07/bc/587a445451b253b285629263eb51c2d8e9bcea4fc97826266d186f96f558/pyserial-3.5-py2.py3-none-any.whl", hash = "sha256:c4451db6ba391ca6ca299fb3ec7bae67a5c55dde170964c7a14ceefec02f2cf0", size = 90585, upload-time = "2020-11-23T03:59:13.41Z" },
]