Context value, bit to word. Bit is refreshed with value 0 but word keep with 1 value

Hi folks,

I've serched for something similar to my issue, but I have'nt found.
A dashboard button send value 1 if is pressed. After 5 seconds (delay), this bit is set to 0 again.

With function setbit, bit 0 of a word is set with the button value (0 or 1). Then this word is writted with Modbus FC06.

The problem is that bit returns to 0 after 5 secons of (value:1), but the word keeps in 1 value. I don't understand why.

See attached the code:

function setBit(number, bitPosition) {
    return number | (1 << bitPosition);
}

var xAuxPLCRentadorax0=global.get("AuxRentadora.b0");
var wAuxPLCRentadora=setBit(xAuxPLCRentadorax0,0);
global.set("AuxRentadoraWord", wAuxPLCRentadora);

Global vars values:

AuxRentadora{
b0:0,
Word:1}

AuxRentadoraWord: 1

Thanks in advance

It is not easy to understand exactly what the problem is.
Can you provide a simple flow illustrating the problem, that we can import and test?

Hi @Colin,

See attached the flow part affected.

[{"id":"63bf743b57d7dad3","type":"ui_button","z":"6f0f171f.39d858","name":"","group":"b2147235.818","order":7,"width":0,"height":0,"passthru":false,"label":"Reset Bomba","tooltip":"","color":"","bgcolor":"","icon":"","payload":"1","payloadType":"num","topic":"topic","topicType":"msg","x":320,"y":740,"wires":[["db50437ae9b45029"]]},{"id":"132dec09.2c9ce4","type":"modbus-flex-write","z":"6f0f171f.39d858","name":"","showStatusActivities":false,"showErrors":false,"server":"ca7a28d558d23b0a","emptyMsgOnFail":false,"keepMsgProperties":false,"x":870,"y":800,"wires":[["3be8578.b5476a8"],[]]},{"id":"3be8578.b5476a8","type":"debug","z":"6f0f171f.39d858","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1110,"y":800,"wires":[]},{"id":"db50437ae9b45029","type":"change","z":"6f0f171f.39d858","name":"","rules":[{"t":"set","p":"AuxRentadora.b0","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":740,"wires":[["bd5634d79c96b550"]]},{"id":"af960b1bccdb78b6","type":"function","z":"6f0f171f.39d858","name":"Bit a word AuxPLCRentadora","func":"function setBit(number, bitPosition) {\n    return number | (1 << bitPosition);\n}\n\nvar xAuxPLCRentadorax0, wAuxPLCRentadora,word;\nxAuxPLCRentadorax0=global.get(\"AuxRentadora.b0\");\nwAuxPLCRentadora=setBit(xAuxPLCRentadorax0,0);\n\n\nif (xAuxPLCRentadorax0==1){\n    word=1;\n    global.set(\"AuxRentadora.Word\", 1);\n}\nelse{\n    word=0;\n    global.set(\"AuxRentadora.Word\", 0);\n}\n\n\nmsg.payload = {\n    'value': word,\n    'fc': 6,\n    'unitid': 1,\n    'address': 114,\n    'quantity': 1\n}\nmsg.topic = \"WriteAuxRentadora\";\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":800,"wires":[["132dec09.2c9ce4"]]},{"id":"4e0d07c757ea0a46","type":"inject","z":"6f0f171f.39d858","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":310,"y":800,"wires":[["af960b1bccdb78b6"]]},{"id":"bd5634d79c96b550","type":"delay","z":"6f0f171f.39d858","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"x":800,"y":740,"wires":[["47305e8466ee811a"]]},{"id":"0b42e813ff1b2e31","type":"debug","z":"6f0f171f.39d858","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1110,"y":740,"wires":[]},{"id":"47305e8466ee811a","type":"function","z":"6f0f171f.39d858","name":"Reset","func":"global.set(\"AuxRentadora.b0\", 0);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":950,"y":740,"wires":[["0b42e813ff1b2e31"]]},{"id":"b2147235.818","type":"ui_group","name":"Consums","tab":"aecc735e.171a1","order":2,"disp":true,"width":"6","collapse":false},{"id":"ca7a28d558d23b0a","type":"modbus-client","name":"M241Rentadora","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"10.10.5.31","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true},{"id":"aecc735e.171a1","type":"ui_tab","name":"Consums","icon":"dashboard","order":3,"disabled":false,"hidden":false}]

Function "Bit a word AuxPLCRentadora" was different previously:

function setBit(number, bitPosition) {
    return number | (1 << bitPosition);
}

var xAuxPLCRentadorax0, wAuxPLCRentadora,word;
xAuxPLCRentadorax0=global.get("AuxRentadora.b0");
wAuxPLCRentadora=setBit(xAuxPLCRentadorax0,0);
global.set("wAuxPLCRentadora", wAuxPLCRentadora);

msg.payload = {
    'value': wAuxPLCRentadora,
    'fc': 6,
    'unitid': 1,
    'address': 114,
    'quantity': 1
}
msg.topic = "WriteAuxRentadora";
return msg

The problem is that in context variable, bit turns false but word keeps in 1 value.

Thanks in advance!

That isn't a simple flow that we can test, as it includes modbus nodes, which obviously won't work unless you have the device available.

[Edit] If you can configure inject node or nodes to inject sample data to show the problem then that would be ideal.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.