#!/usr/bin/env python
# -*- coding: utf-8 -*-

#https://scapy.readthedocs.io/en/latest/api/scapy.packet.html#scapy.packet.Packet
#https://scapy.readthedocs.io/en/latest/api/scapy.contrib.modbus.html

import scapy.all as scapy
import scapy.contrib.modbus as mb

def decode(pkt):
    if "ModbusADU" in pkt:
        miniL=[]
        modpkt = pkt["ModbusADU"]
        print(modpkt.payload.name)
        for i in modpkt.payload.fields:
            miniL.append(modpkt.payload.getfieldval(i))
            print(i,modpkt.payload.getfieldval(i))
            if(i=="coilStatus"):
                bi=str(bin(modpkt.payload.getfieldval(i)[0]))
                bi=bi[2:]
                bi=bi.rjust(8,'0')
                print([*bi])
        bigL.append(miniL)
        print(bigL)
bigL = []
scapy.sniff(iface="lo", prn=decode)
