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
| 3694 | |
| 3695 | |
| 3696 | class 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 | |
| 3739 | class BitScalingField(_ScalingField, BitField): # type: ignore |
no outgoing calls
no test coverage detected
searching dependent graphs…