Started implementing message class, parsing rx and tx pgn list

This commit is contained in:
2026-02-21 08:45:39 +01:00
parent 1f5aac778c
commit 02f92bef2a
4 changed files with 231 additions and 7 deletions

114
device.py
View File

@@ -12,6 +12,24 @@ import time
import struct
from . import lookup
def set_to_lines(values, maxlen, indent=0):
spaces = ' ' * indent
lines = []
current = ""
for v in values:
part = str(v)
emax = maxlen - indent
if not current:
current = part
elif len(current) + 1 + len(part) <= emax:
current += ',' + part
else:
lines.append(spaces + current + ',')
current = part
if current:
lines.append(spaces + current)
return lines
class Device():
def __init__(self, address):
@@ -21,6 +39,7 @@ class Device():
self.lastpinfo = None # Wann letztes Mal Productinfo erhalten?
self.lastcinfo = None # Wann letztes Mal Configurationinfo erhalten?
self.has_cinfo = True # Weitere Abfragen können verhindert werden
self.has_pgnlist = False # PGN-Listen Tx,Rx vorhanden
# Device info
self.NAME = 0 # Wird über getNAME (address claim) gefüllt, 64bit Integer
@@ -30,14 +49,13 @@ class Device():
self.instlower = 0 # 3bit, ISO ECU Instance
self.instupper = 0 # 5bit, ISO Function Instance
self.sysinstance = 0 # used with bridged networks, default 0
self.industrygroup = None
self.devicefunction = None
self.deviceclass = None
# Product info
self.product = None # Product name
self.productcode = None # Product code
self.serial = None
self.product = None # Product name (ModelID)
self.productcode = None # Product code (16bit unsigned)
self.serial = None # Device serial number
self.modelvers = None # Hardware Version
self.softvers = None # Current Software Version
self.n2kvers = None # NMEA2000 Network Message Database Version
@@ -47,7 +65,11 @@ class Device():
# Configuration info
self.instdesc1 = None
self.instdesc2 = None
self.manufinfo = None
self.manufinfo = None
# PGN lists, fill with functions defined below
self.pgns_transmit = set()
self.pgns_receive = set()
# Additional data
self.customname = None # User defined device name
@@ -120,7 +142,7 @@ class Device():
print("Ignore collision because of our lower NAME")
else:
print(f"Answer: DEST={dest}")
print(msg.data)
print("Data:", msg.data)
if dest == self.address:
print("We are addressed: WIP!")
else:
@@ -133,6 +155,84 @@ class Device():
print("claim seems ok after 250ms")
return claim_ok
# TODO String handling
# Spec says ASCII is only 0x20 to 0x7E. But some devices use full
# Latin-1 / ISO-8859-1 code set.
# Unicode:
# UTF-16 Little Endian
# UTF-8 eventually used in some PGNs?
def getStringFix(self, text, maxlength=32, filler=b' '):
if not text:
text = ''
if text.isascii():
# text.encode('latin1')
# text.encode('ascii') can throw errors
# text.encode('ascii', 'replace') sets question mark for unknown codes
#return b'x01' + bytes(text or '', 'ascii').ljust(maxlength, b' ')
return b'x01' + text.encode('ascii', 'ignore').ljust(maxlength, filler)
else:
# convert to UTF-16 Little Endian
return b'x00' + text.encode('utf-16-le').ljust(maxlength, filler)
def getProductInfo(self):
"""
Returns data for msg prepared to be sent out as fast packet
"""
print("Set Productinfo:", self.product)
data = bytearray()
data.extend(struct.pack('<H', self.n2kvers)) # 2 Byte
data.extend(struct.pack('<H', self.productcode)) # 2 Byte
data.append(0x01) # String
data.extend(bytes(self.product, 'latin1').ljust(32, b' '))
#data.extend(getStringFix(self.product))
data.append(0x01) # ASCII String
data.extend(bytes(self.softvers, 'latin1').ljust(32, b' '))
data.append(0x01)
data.extend(bytes(self.modelvers, 'latin1').ljust(32, b' '))
data.append(0x01)
data.extend(bytes(self.serial, 'latin1').ljust(32, b' '))
data.append(0x00) # Certification level
data.append(0x00) # Load equivalency 0=Not powered by bus
return data
def getConfigInfo(self):
"""
Configuration info
"""
print("Set Configinfo")
data = bytearray()
data.extend(self.getStringFix(self.instdesc1), 32)
data.extend(self.getStringFix(self.instdesc2), 32)
data.extend(self.getStringFix(self.manufinfo), 32)
#data.append(0x01) # ASCII String
#data.extend(bytes(self.instdesc1 or '', 'ascii').ljust(32, b' '))
#data.append(0x01)
#data.extend(bytes(self.instdesc2 or '', 'ascii').ljust(32, b' '))
#data.append(0x01)
#data.extend(bytes(self.manufinfo or '', 'ascii').ljust(32, b' '))
return data
"""
PGN lists (mandatory)
59904 ISO Request
60928 ISO Address Claim
126208 Group Function (Request / Command / Acknowledge)
126464 PGN List (Transmit / Receive)
126993 Heartbeat
126996 Product Information
126998 Configuration Information
"""
def setTxPGNs(self, pgnlist, mandatory=True):
self.pgns_transmit = set(pgnlist)
if mandatory:
self.pgns_transmit.add((60928, 126208, 126464, 126996))
def setRxPGNs(self, pgnlist, mandatory=True):
self.pgns_receive = set(pgnlist)
if mandatory:
self.pgns_receive.add((59904, 60928, 126208, 126996, 126998))
def __str__(self):
out = f"Device: {self.address} : '{self.product}'\n"
out += " NAME: {} ({})\n".format(self.getNAME(), self.NAME)
@@ -177,4 +277,6 @@ class Device():
out += f" Manufacturer info: {self.manufinfo}\n"
else:
out += " not available\n"
out += " PGNs RX: \n{}\n".format('\n'.join(set_to_lines(self.pgns_receive, 72, 4)) or 'n/a')
out += " PGNs TX: \n{}\n".format('\n'.join(set_to_lines(self.pgns_transmit, 72, 4)) or 'n/a')
return out