BLF_DLL/example.py

50 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
Пример использования BLF библиотеки из Python с гарантированным закрытием файлов
"""
import time
from blf_wrapper import BLFWriter, can_flags, timestamp_ms_to_ns
def example_with_container():
"""Пример записи CAN сообщений с использованием контейнера"""
print("\n=== CAN example with container ===")
# Используем контекстный менеджер для автоматического закрытия
with BLFWriter("example_container.blf") as writer:
# Начинаем контейнер
print("Starting container...")
writer.start_container(timestamp_ms_to_ns(1000))
# Добавляем сообщения в контейнер
for i in range(5):
writer.add_can_message(
channel=1,
can_id=0x200 + i,
dlc=4,
data=bytes([i, i*2, i*3, i*4]),
flags=can_flags(1), # CAN_DIR_TX = 1
timestamp_ns=timestamp_ms_to_ns(i * 50)
)
print(f" Added message {i} to container")
# Заканчиваем контейнер
print("Ending container...")
writer.end_container()
print("Container example completed, closing file...")
print("File closed successfully, header updated")
if __name__ == "__main__":
print("=" * 60)
print("BLF Library Example - Container Usage")
print("=" * 60)
example_with_container()
print("\n" + "=" * 60)
print("Example completed successfully!")
print("=" * 60)