Code Monkey home page Code Monkey logo

Comments (7)

bnjmnp avatar bnjmnp commented on August 16, 2024

Hi @marcAC,

good to see that you could find a solution to your initial question.

What sdo_read read returns are the raw bytes of that object. Bytes you will also get for example, if you read from a binary file (doing something like with open('file.bin', 'rb') as f: + f.read()).

If there is an integer behind thees bytes, you could do:

print(int.from_bytes(result, byteorder='little'))

If it's a signed integer you must add signed=True, see here https://docs.python.org/3/library/stdtypes.html#int.from_bytes for details.

You could also use the struct module. But thats a bit more difficult to explain.

If your next step is writing objects, take a look at issue #16 there I elaborate on this a bit. You need the revers of int.from_bytes() there, witch is int.to_bytes().

from pysoem.

marcAC avatar marcAC commented on August 16, 2024

Hey @bnjmnp,

thanks for your reply. Then maybe there is some weird problem with our slave, because the result which should be 7, is b'\x00\x00\xe0@' and 1088421888 after converting it.

EDIT: I was trying to convert "float"-bytes to int, which obviously wasn't going to work, so I was able to manage to handle it with struct.unpack. Thanks for your help!

from pysoem.

marcAC avatar marcAC commented on August 16, 2024

Hey @bnjmnp,

writing SDOs is working, are there any examples on how to write PDOs?

Kind regards
Marc

from pysoem.

bnjmnp avatar bnjmnp commented on August 16, 2024

Hi @marcAC,

good to see that you had success. Interesting that your device uses float type parameter.

For PDO, take a look at the _pdo_update_loop function in the basic_example.py. You need to know how the PDO is composed and what is behind those bytes. In the example there is just one bit toggled periodically, which toggles a digital output. The manual of your device should help you here. You need to dig in there a bit. It is not that easy to understand.

from pysoem.

marcAC avatar marcAC commented on August 16, 2024

Hey @bnjmnp,

I think it's because it's not really a standard device.

I already looked at this example but our problem is that we also mapped many values into one byte because there are also boolean-values. The C-structs we used for this looked like the following so as you can see there are 9 parameters in 2 bytes.

typedef struct PACKED {
    BOOLEAN(PulsingEnabled); /* Subindex1 - Pulsing Enabled */
    BOOLEAN(RampingInProgress); /* Subindex2 - Ramping in progress */
    BOOLEAN(ArcDetectionEnabled); /* Subindex3 - Arc Detection Enabled */
    BOOLEAN(ArcReactionEnabled); /* Subindex4 - Arc Reaction Enabled */
    BOOLEAN(CEXEnabled); /* Subindex5 - CEX Enabled */
    BOOLEAN(CEXLocked); /* Subindex6 - CEX Locked */
    BOOLEAN(FrequencyTuned); /* Subindex7 - Frequency Tuned */
    BOOLEAN(JouleModeEnergyLimitReached); /* Subindex8 - Joule Mode: Energy limit reached */
    BOOLEAN(ProcessTimerEndOfProcessTime); /* Subindex9 - Process Timer: End of process time */
    ALIGN7(SI10); /* Subindex10 */
} TOBJ6001;

Do you think there is a possibility to split up the bytearray more to map a struct-like structure like the one above to it?

Thanks for your help!

EDIT:

In case it is interesting for someone. We found a way to do it with ctypes Structures like this:

class TOBJ7004(ctypes.LittleEndianStructure):
    _fields_ = [
            ("RegulationControlMode", c_uint8, 8),
            ("SI2", c_uint8, 8),
        ]


class TOBJ7005(ctypes.LittleEndianStructure):
    _fields_ = [
            ("Setpoint", c_float)
        ]


class TOBJOUT(ctypes.LittleEndianStructure):
    _fields_ = [
            ("tobj7000", TOBJ7000),
            ("tobj7001", TOBJ7001),
            ("tobj7004", TOBJ7004),
            ("tobj7005", TOBJ7005)
        ]

The only trouble we have is with the c_float.

from pysoem.

bnjmnp avatar bnjmnp commented on August 16, 2024

Oh yes, you can also convert bytes to ctypes using ctypes.

Then you do something like:

tobj7004 = TOBJ7004(8, 8)
foo = bytes(tobj7004)

... to get the bytes of that struct instance.

Or:

tobj7004 = TOBJ7004.from_buffer_copy(bytes([0x55, 0xAA]))

... to convert a byte array to a struct.

I'm pretty sure you have a problem with alignment.

Add a _pack_ = 1 to your struct definition like this:

class TOBJOUT(ctypes.LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
            ("tobj7004", TOBJ7004),
            ("tobj7005", TOBJ7005)
        ]

Then the c_float will be correct non-aligned.

from pysoem.

marcAC avatar marcAC commented on August 16, 2024

Awesome, that did the trick, thank you!

Is my guess correct that it will work the same for PDO read?

EDIT:
Unfortunately it doesn't. In the minimal example you read the PDOs and convert the whole bytearray to int16 but in our case we have many different datatypes in the struct. The solution I found for this is to use ctypes.from_buffer_copy but if there is any more elegant solution you know feel free to share.

from pysoem.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.