mirror of
https://github.com/controllerzz/carbus_lib.git
synced 2026-05-23 04:01:42 +03:00
add CanIdRouter and examle
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
from .carbus_iface import CarBusCanTransport
|
||||
from .transport import IsoTpChannel
|
||||
from .transport import IsoTpChannel, IsoTpConnection
|
||||
from .api import open_isotp
|
||||
|
||||
__all__ = [
|
||||
"CarBusCanTransport",
|
||||
"IsoTpChannel",
|
||||
"IsoTpConnection",
|
||||
"open_isotp",
|
||||
]
|
||||
@@ -3,7 +3,8 @@ from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from isotp_async import IsoTpChannel, CarBusCanTransport
|
||||
# from carbus_async import CanIdRouter, RoutedCarBusCanTransport
|
||||
from isotp_async import IsoTpChannel, CarBusCanTransport, IsoTpConnection
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -13,9 +14,16 @@ class IsoTpEndpoint:
|
||||
channel: int = 1
|
||||
|
||||
|
||||
async def open_isotp(dev: Any, *, endpoint: IsoTpEndpoint | None = None,
|
||||
channel: int = 1, tx_id: int | None = None, rx_id: int | None = None,
|
||||
**channel_kwargs) -> IsoTpChannel:
|
||||
async def open_isotp(
|
||||
dev: Any,
|
||||
*,
|
||||
endpoint: IsoTpEndpoint | None = None,
|
||||
channel: int = 1,
|
||||
tx_id: int | None = None,
|
||||
rx_id: int | None = None,
|
||||
router: Any | None = None,
|
||||
**channel_kwargs,
|
||||
) -> IsoTpChannel:
|
||||
|
||||
if endpoint is not None:
|
||||
channel = endpoint.channel
|
||||
@@ -25,5 +33,30 @@ async def open_isotp(dev: Any, *, endpoint: IsoTpEndpoint | None = None,
|
||||
if tx_id is None or rx_id is None:
|
||||
raise ValueError("tx_id and rx_id are required (or pass endpoint=...)")
|
||||
|
||||
can_tr = CarBusCanTransport(dev, channel=channel, rx_id=rx_id)
|
||||
# ЛЕНИВЫЕ ИМПОРТЫ, чтобы не было circular import:
|
||||
if router is None:
|
||||
from isotp_async.carbus_iface import CarBusCanTransport # <-- подстрой путь под твой проект
|
||||
can_tr = CarBusCanTransport(dev, channel=channel, rx_id=rx_id)
|
||||
else:
|
||||
from carbus_async.can_router import RoutedCarBusCanTransport # <-- подстрой путь
|
||||
can_tr = RoutedCarBusCanTransport(dev, channel=channel, rx_id=rx_id, router=router)
|
||||
|
||||
return IsoTpChannel(can_tr, tx_id=tx_id, rx_id=rx_id, **channel_kwargs)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class IsoTpCanEndpoint:
|
||||
rx_id: int # что слушаем (request -> ECU)
|
||||
tx_id: int # куда отвечаем (ECU -> tester)
|
||||
|
||||
|
||||
class IsoTpNetwork:
|
||||
def __init__(self, base_send, router: CanIdRouter):
|
||||
self._send = base_send
|
||||
self._router = router
|
||||
|
||||
def endpoint(self, ep: IsoTpCanEndpoint) -> IsoTpConnection:
|
||||
transport = RoutedCarBusCanTransport(
|
||||
dev=..., channel=..., rx_id=ep.rx_id, router=self._router
|
||||
)
|
||||
return IsoTpConnection(transport=transport, tx_id=ep.tx_id)
|
||||
|
||||
@@ -24,7 +24,7 @@ class IsoTpChannel:
|
||||
tx_id: int
|
||||
rx_id: int
|
||||
|
||||
block_size: int = 8
|
||||
block_size: int = 0
|
||||
st_min_ms: int = 0
|
||||
fc_timeout: float = 1.0
|
||||
cf_timeout: float = 1.0
|
||||
@@ -214,10 +214,24 @@ class IsoTpChannel:
|
||||
|
||||
payload.extend(cf.data[1:])
|
||||
expected_sn = (expected_sn + 1) & 0x0F
|
||||
if expected_sn == 0:
|
||||
expected_sn = 1
|
||||
# надо подумать, гдето нужно так((((
|
||||
# if expected_sn == 0:
|
||||
# expected_sn = 1
|
||||
|
||||
if st_min > 0:
|
||||
await asyncio.sleep(st_min)
|
||||
|
||||
return bytes(payload[:total_length])
|
||||
|
||||
|
||||
class IsoTpConnection(IsoTpChannel):
|
||||
async def send(self, payload: bytes) -> None:
|
||||
await self.send_pdu(payload)
|
||||
|
||||
async def recv(self, timeout: float = 1.0) -> Optional[bytes]:
|
||||
return await self.recv_pdu(timeout=timeout)
|
||||
|
||||
async def request(self, payload: bytes, timeout: float = 1.0) -> Optional[bytes]:
|
||||
|
||||
await self.send_pdu(payload)
|
||||
return await self.recv_pdu(timeout=timeout)
|
||||
|
||||
Reference in New Issue
Block a user