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.