Skip to content

pyads.connection.Connection.write_list_by_name exception when trying to write to SINT variable #454

@AskicV

Description

@AskicV

In my very simple test TwinCat project, in the MAIN POU, I have variables declared like this:

PROGRAM MAIN
VAR
	
	SINTTest : SINT;
	INTTest : INT;
	UINTTest : UINT;

END_VAR

In my python script, I have defined the data structure and trying to write values to these variables in the following way:

data = {
        "MAIN.SINTTest": -1,
        "MAIN.INTTest": 22,
        "MAIN.UINTTest": 552,
    }
def write_data_list(plc, data):
    try:
        plc.write_list_by_name(data)
    except Exception as e:
        print(f"Error writing: {e}")
        return False

write_data_list(plc, data)

And this generates the following exception:
Error writing: ubyte format requires 0 <= number <= 255

The error is caused by trying to write to SINT (int8) variable SINTTest in the MAIN Program in TwinCAT.
The current workaround is the following:

data = {
        # "MAIN.SINTTest": -1,
        "MAIN.INTTest": 22,
        "MAIN.UINTTest": 552,
    }

write_data_list(plc, data)
plc.write_by_name("MAIN.SINTTest", -1)

SINT type should be able to accept any number in range from -128 to 127, which works good when using write_by_name function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions