Skip to content

Issue with the "bits" mode (writing) #8

@tpoignonec

Description

@tpoignonec

Hi,

Thanks for this package, it is very useful.

I recently tried to use it to control the digital output of a Wago module (WAGO 750-1504).
I used the bits mode. The reading works as expected, but I ran into some issues when using the for wrting with the following config:

...
        <command_interface name="${interface_name}">
            <param name="register">${ address }</param>
            <param name="bits_to_read">1</param>
            <param name="write_function">bits</param>
            <param name="conversion_fn">float</param>
        </command_interface>
...

The issues, related with the writing function "float", seems to be located in the ModbusInterfaceWriteConfig::double_to_modbus_8 method.
More specifically:

https://github.com/StoglRobotics/modbus_hardware_interface/blob/c60afb2ce93c7b4ac50c31349052ebf6bed241b0/include/modbus_hardware_interface/modbus_client.hpp#L451-L457

Got it to work by only writing a single bit:

modbus_float_to_uint_8_ = [](const float & value) -> std::vector<uint8_t>
{
    std::vector<uint8_t> data(1);
+  data[0] = value == 0.0 ? 0x00 : 0xFF;
-   // TODO(Manual) Maybe clamp, round and check that its zero/one?
-   data.push_back(static_cast<uint8_t>(value));
    return data;
};

It should be noted that the rounding to 0 or 1 is simply to avoid undefined casting behavior, but it might not be strictly necessary.

Did you test only writing bits? If yes, is this an issue you faced or do I use the library incorrectly?

Cheers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions