Sync parser
sync parser implementation
This commit is contained in:
1 parent
96cc366595
commit
d2318b6903
4 files changed
+64
-3
No files matched your search
@@ -27,6 +27,15 @@ typedef struct {
|
||||
} data_packet;
|
||||
#pragma pack(pop)
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
uint16_t sync;
|
||||
uint8_t type;
|
||||
uint32_t r_t;
|
||||
uint32_t s_t;
|
||||
} sync_packet;
|
||||
#pragma pack(pop)
|
||||
|
||||
uint16_t crc16_ccitt(const uint8_t *data, size_t length)
|
||||
{
|
||||
uint16_t crc = 0xFFFF;
|
||||
@@ -71,6 +80,35 @@ void setup() {
|
||||
pinMode(interruptPin, INPUT);
|
||||
pinMode(lambdaPin, INPUT);
|
||||
attachInterrupt(digitalPinToInterrupt(interruptPin),interrupt,FALLING);
|
||||
bool first_packet = false;
|
||||
bool parser_finished = false;
|
||||
unsigned long ts_lp = 0;
|
||||
while(true){
|
||||
uint8_t previous = 0;
|
||||
sync_packet packet{0xBEEF,3,0,0};
|
||||
while (true) {
|
||||
if (first_packet && micros() > ts_lp + 100000){parser_finished = true; break;}
|
||||
|
||||
if (Serial.available()) {
|
||||
uint8_t byte;
|
||||
Serial.readBytes(&byte, 1);
|
||||
if (previous == 0xEF && byte == 0xBE) {
|
||||
break;
|
||||
}
|
||||
previous = byte;
|
||||
}
|
||||
}
|
||||
if(parser_finished){break;}
|
||||
uint32_t r_t = micros();
|
||||
Serial.readBytes(&packet.type, 9);
|
||||
if (packet.type == 3) {
|
||||
packet.r_t = r_t;
|
||||
packet.s_t = micros();
|
||||
Serial.write((uint8_t*)&packet, sizeof(packet));
|
||||
first_packet = true;
|
||||
ts_lp = r_t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
@@ -9,6 +9,7 @@ authors = [
|
||||
requires-python = ">=3.13"
|
||||
dependencies = [
|
||||
"pyserial>=3.5",
|
||||
"serial-sync",
|
||||
]
|
||||
|
||||
[project.scripts]
|
||||
@@ -17,3 +18,6 @@ bench-data-connector = "bench_data_connector:main"
|
||||
[build-system]
|
||||
requires = ["uv_build>=0.12.12,<0.13.0"]
|
||||
build-backend = "uv_build"
|
||||
|
||||
[tool.uv.sources]
|
||||
serial-sync = { git = "https://git.h3cx.dev/Exergie/serial-sync.git", tag = "v0.1.1" }
|
||||
@@ -1,6 +1,7 @@
|
||||
from serial.tools import list_ports
|
||||
from serial import Serial
|
||||
from dataclasses import dataclass
|
||||
from serial_sync import SerialSync
|
||||
import struct
|
||||
|
||||
DATA_STRUCT = struct.Struct("<BIf")
|
||||
@@ -21,13 +22,16 @@ def main() -> None:
|
||||
choice = None
|
||||
ports = list(list_ports.comports())
|
||||
for index, port in enumerate(ports):
|
||||
if len(ports) < 0:
|
||||
print("No serial ports found.")
|
||||
return
|
||||
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):
|
||||
while choice is None or choice >= len(ports) or choice < 0:
|
||||
|
||||
try:
|
||||
choice = int(input("Unknown port. Try again : "))
|
||||
@@ -42,10 +46,13 @@ def main() -> None:
|
||||
exit(0)
|
||||
|
||||
with Serial(selected_port,baudrate=115200,timeout=1) as ser:
|
||||
sync = SerialSync(ser)
|
||||
sync.calibrate_sync(30)
|
||||
print(f"sync ok\nOffset:{sync._offset}\nScale:{sync._scale}")
|
||||
while True:
|
||||
start_sync(ser)
|
||||
#print("paquet trouvé")
|
||||
print(DATA_STRUCT.size)
|
||||
#print(DATA_STRUCT.size)
|
||||
payload = read_exactly(ser, DATA_STRUCT.size)
|
||||
|
||||
if not payload:
|
||||
|
||||
Generated
+13
-1
@@ -8,10 +8,14 @@ version = "0.1.0"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "pyserial" },
|
||||
{ name = "serial-sync" },
|
||||
]
|
||||
|
||||
[package.metadata]
|
||||
requires-dist = [{ name = "pyserial", specifier = ">=3.5" }]
|
||||
requires-dist = [
|
||||
{ name = "pyserial", specifier = ">=3.5" },
|
||||
{ name = "serial-sync", git = "https://git.h3cx.dev/Exergie/serial-sync.git?tag=v0.1.1" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyserial"
|
||||
@@ -21,3 +25,11 @@ sdist = { url = "https://files.pythonhosted.org/packages/1e/7d/ae3f0a63f41e4d2f6
|
||||
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" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serial-sync"
|
||||
version = "0.1.1"
|
||||
source = { git = "https://git.h3cx.dev/Exergie/serial-sync.git?tag=v0.1.1#cf7e5466a9ac31bf1cc83d1e9a522cbb4afb2efb" }
|
||||
dependencies = [
|
||||
{ name = "pyserial" },
|
||||
]
|
||||
Reference in new issue
Block a user