tecki / ebpfcat

A Python-base EBPF code generator
GNU General Public License v2.0
17 stars 5 forks source link

How to use PacketDesc() ? #8

Open jinyistudio534 opened 7 months ago

jinyistudio534 commented 7 months ago

I refer to ESI/XML to midify old class to as following.

class EASYCAT32(EBPFTerminal): compatibility = {(1946, 14614238)}

input PDO

input0 = PacketDesc(1, 0,'B')
input1 = PacketDesc(1, 1,'B')
input2 = PacketDesc(1, 2,'B')
input3 = PacketDesc(1, 3,'B')
input4 = PacketDesc(1, 4,'B')
input5 = PacketDesc(1, 5,'B')
input6 = PacketDesc(1, 6,'B')
input7 = PacketDesc(1, 7,'B')  
# output PDO
output0 = PacketDesc(0, 0,'B')
output1 = PacketDesc(0, 1,'B')
output2 = PacketDesc(0, 2,'B')
output3 = PacketDesc(0, 3,'B')
output4 = PacketDesc(0, 4,'B')
output5 = PacketDesc(0, 5,'B')
output6 = PacketDesc(0, 6,'B')
output7 = PacketDesc(0, 7,'B')  

Sorry, I really don’t know how to use the SyncManager you mentioned? Can you please teach me how to write a program. Let output1 = input2 😡😡

jinyistudio534 commented 7 months ago

But I try this is ok. πŸ˜…πŸ‘ To slave/master are OK πŸ•ΊπŸ•ΊπŸ•Ί

  class EASYCAT32(EBPFTerminal):
    compatibility = {(1946, 14614238)}
    class Channel(Struct):   
        byte0 = ProcessDesc(0x0005, 1,'B')
        byte1 = ProcessDesc(0x0005, 2,'B')
        byte2 = ProcessDesc(0x0005, 3,'B')
        byte3 = ProcessDesc(0x0005, 4,'B')
        byte4 = ProcessDesc(0x0005, 5,'B')
        byte5 = ProcessDesc(0x0005, 6,'B')
        byte6 = ProcessDesc(0x0005, 7,'B')
        byte7 = ProcessDesc(0x0005, 8,'B')  
    outputs = Channel(0)
    inputs = Channel(1)

master = FastEtherCat("eth0") await master.connect() print("Number of terminals:", await master.count())
out1 = EL2088(master)
await out1.initialize(-3,3) out2 = EL2088(master)
await out2.initialize(-4,4) easycat = EASYCAT32(master)
await easycat.initialize(-5,5) do1 = DigitalOutput(out1.channel1) # use channel 1 of terminal "out" do2 = DigitalOutput(out2.channel3) # use channel 1 of terminal "out" ob0 = AnalogOutput(easycat.outputs.byte0) ob1 = AnalogOutput(easycat.outputs.byte3) ib1 = AnalogInput(easycat.inputs.byte3) sg = SyncGroup(master, [do1,do2,ob1,ob0,ib1]) # this sync group only contains one terminal task = sg.start() # start operating the terminals ox = 0 ix = 0 for i in range(5000):

we would measure an increasing value on the terminal output

    do1.value = 1 if do1.value==0 else 0
    do2.value = 1 if do2.value==0 else 0
    ob0.value = random.randint(0, 255)
    ob1.value = random.randint(0, 255)      
    print('ib1: ',ib1.value)
    await asyncio.sleep(0.1)
task.cancel()  # stop the sync group
jinyistudio534 commented 7 months ago

Hello, I can only read and write a single byte using the successful method now. Is there a way for me to read 32 bytes of input and write 32 bytes of output in one loop?

tecki commented 7 months ago

The 'B' in the ProcessDesc stands for byte. Use a 'I' and you read or write four bytes. The letters are described here: https://docs.python.org/3/library/struct.html