MCPcopy Create free account
hub / github.com/secdev/scapy / ScalingField

Class ScalingField

scapy/fields.py:3696–3736  ·  view source on GitHub ↗

Handle physical values which are scaled and/or offset for communication Example: >>> from scapy.packet import Packet >>> class ScalingFieldTest(Packet): fields_desc = [ScalingField('data', 0, scaling=0.1, offset=-1, unit='mV')] # noqa: E501

Source from the content-addressed store, hash-verified

3694
3695
3696class ScalingField(_ScalingField,
3697 Field[Union[int, float], Union[int, float]]):
3698 """ Handle physical values which are scaled and/or offset for communication
3699
3700 Example:
3701 >>> from scapy.packet import Packet
3702 >>> class ScalingFieldTest(Packet):
3703 fields_desc = [ScalingField('data', 0, scaling=0.1, offset=-1, unit='mV')] # noqa: E501
3704 >>> ScalingFieldTest(data=10).show2()
3705 ###[ ScalingFieldTest ]###
3706 data= 10.0 mV
3707 >>> hexdump(ScalingFieldTest(data=10))
3708 0000 6E n
3709 >>> hexdump(ScalingFieldTest(data=b"\x6D"))
3710 0000 6D m
3711 >>> ScalingFieldTest(data=b"\x6D").show2()
3712 ###[ ScalingFieldTest ]###
3713 data= 9.9 mV
3714
3715 bytes(ScalingFieldTest(...)) will produce 0x6E in this example.
3716 0x6E is 110 (decimal). This is calculated through the scaling factor
3717 and the offset. "data" was set to 10, which means, we want to transfer
3718 the physical value 10 mV. To calculate the value, which has to be
3719 sent on the bus, the offset has to subtracted and the scaling has to be
3720 applied by division through the scaling factor.
3721 bytes = (data - offset) / scaling
3722 bytes = ( 10 - (-1) ) / 0.1
3723 bytes = 110 = 0x6E
3724
3725 If you want to force a certain internal value, you can assign a byte-
3726 string to the field (data=b"\x6D"). If a string of a bytes object is
3727 given to the field, no internal value conversion will be applied
3728
3729 :param name: field's name
3730 :param default: default value for the field
3731 :param scaling: scaling factor for the internal value conversion
3732 :param unit: string for the unit representation of the internal value
3733 :param offset: value to offset the internal value during conversion
3734 :param ndigits: number of fractional digits for the internal conversion
3735 :param fmt: struct.pack format used to parse and serialize the internal value from and to machine representation # noqa: E501
3736 """
3737
3738
3739class BitScalingField(_ScalingField, BitField): # type: ignore

Callers 15

NRF2_Packet_EventClass · 0.90
OBD_PID81Class · 0.90
OBD_PID82Class · 0.90
OBD_PID83Class · 0.90
OBD_PID21Class · 0.90
OBD_PID22Class · 0.90
OBD_PID23Class · 0.90
_OBD_PID24_2BClass · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…