Form and event to control machins and send data to csv fil

hi everyone i wish that you are fine,
as @Colin and @Steve-Mcl and others helped me I'm thankful for you ...
i have worked on a project it's about tracking events in a machine we want to not turn on this machine just if the operator entre the code and his name in a form node it's a Mitsubishi PLC so i have this flow but i have a problem with data type and issues with object array and if you can help me to clean my flow please i just want send data to CSV file with the current DateTime and send a TRUE to the PLC when the operator enter data in form if the operator didn't write anything the msg will send nothing to PLC using th MC Write NODE

[{"id":"4a6ba2c4.481ffc","type":"tab","label":"Flow 8","disabled":false,"info":""},{"id":"317e7cb2.f1c6a4","type":"function","z":"4a6ba2c4.481ffc","name":"Store Data","func":"var data1 = context.get(\"data1\") || 0; //get from context or default to 0\nvar data2 = context.get(\"data2\") || 0; //get from context or default to 0\n\nswitch (msg.topic){\n\n    case \"task1\":\n        data1 = msg.payload; \n        context.set(\"data1\", data1);//store in context for next time \n        return;\n        \n    case \"task2\":\n        data2 = msg.payload;\n        context.set(\"data2\", data2); //store in context for next time \n        return;\n        \n    case \"Marche\":\n        \n     {\n    if( data1 == '0' && data2 == '0')\n     \n      {  msg.payload = undefined}\n      \n    else \n     {msg.payload = \" text 1 \"+ data1 +\" , text2 \" + data2;}\n     \n     }\n}\n\n//msg.topic = \"go\";\nreturn msg;\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":120,"wires":[["61dcc596.e55b5c","cdefef64.81613"]]},{"id":"b74b6e1.b73569","type":"ui_text_input","z":"4a6ba2c4.481ffc","name":"","label":"code","tooltip":"","group":"a63b29d.f4c81d8","order":2,"width":0,"height":0,"passthru":false,"mode":"text","delay":"0","topic":"task1","topicType":"str","x":70,"y":80,"wires":[["317e7cb2.f1c6a4"]]},{"id":"9439235c.d3aef","type":"ui_text_input","z":"4a6ba2c4.481ffc","name":"","label":"Operateur","tooltip":"","group":"a63b29d.f4c81d8","order":3,"width":0,"height":0,"passthru":false,"mode":"text","delay":"0","topic":"task2","topicType":"str","x":80,"y":120,"wires":[["317e7cb2.f1c6a4"]]},{"id":"d29cd09b.cf787","type":"ui_button","z":"4a6ba2c4.481ffc","name":"","group":"a63b29d.f4c81d8","order":4,"width":0,"height":0,"passthru":false,"label":"Marche","tooltip":"","color":"","bgcolor":"green","icon":"","payload":"","payloadType":"str","topic":"Marche","topicType":"str","x":90,"y":160,"wires":[["317e7cb2.f1c6a4"]]},{"id":"7863dc7e.13b5d4","type":"debug","z":"4a6ba2c4.481ffc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":850,"y":60,"wires":[]},{"id":"61dcc596.e55b5c","type":"json","z":"4a6ba2c4.481ffc","name":"","property":"payload","action":"str","pretty":true,"x":470,"y":120,"wires":[["36fd165f.4d09ca"]]},{"id":"36fd165f.4d09ca","type":"file","z":"4a6ba2c4.481ffc","name":"","filename":"C:\\Users\\pc\\Desktop\\test.csv","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":680,"y":120,"wires":[["7863dc7e.13b5d4"]]},{"id":"cdefef64.81613","type":"function","z":"4a6ba2c4.481ffc","name":"Store Data","func":"if (msg.payload == undefined)\n\n{\t\n    msg.payload = \"Entre the reason\";\t\n\n}\nelse\n{\n       msg.payload = true;\n\n\n}\nreturn msg;\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":450,"y":200,"wires":[["7bae3da0.ffb114","4f0b37f3.4670d8","e8777ad0.04c158"]]},{"id":"7bae3da0.ffb114","type":"debug","z":"4a6ba2c4.481ffc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":200,"wires":[]},{"id":"4f0b37f3.4670d8","type":"ui_toast","z":"4a6ba2c4.481ffc","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":true,"topic":"","name":"","x":500,"y":320,"wires":[]},{"id":"e4f3e3a0.c8f38","type":"MC Write","z":"4a6ba2c4.481ffc","name":"marche","topic":"","connection":"8a67d1bd.c84ff","data":"payload","address":"D0","addressType":"str","dataType":"msg","errorHandling":"throw","outputs":1,"x":740,"y":260,"wires":[["6f06958a.8d219c"]]},{"id":"6f06958a.8d219c","type":"debug","z":"4a6ba2c4.481ffc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":440,"wires":[]},{"id":"e8777ad0.04c158","type":"function","z":"4a6ba2c4.481ffc","name":"Store Data","func":"if (msg.payload == undefined)\n\n{\t\n    msg.payload = \"Entre the reason\";\t\n\n}\nelse\n{\n       msg.payload = true;\n\n\n}\nreturn msg;\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":570,"y":260,"wires":[["e4f3e3a0.c8f38"]]},{"id":"a63b29d.f4c81d8","type":"ui_group","name":"Form","tab":"4bed3390.45d6dc","order":1,"disp":true,"width":"15","collapse":false},{"id":"8a67d1bd.c84ff","type":"MC Protocol Connection","name":"","host":"localhost","port":"5003","protocol":"UDP","frame":"4E","plcType":"Q","ascii":false,"PLCStation":"","PCStation":"","PLCModuleNo":"","network":"","octalInputOutput":false,"timeout":"3000"},{"id":"4bed3390.45d6dc","type":"ui_tab","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]```

It is no good just telling us you have problems, tell us exactly which bit is not working.

1 Like
  • Your logic in the 2nd function was sending an error string in the payload to the MC Write node when the operator didnt enter details. I have changed this to send the error string out of a 2nd output
  • You are trying to write true to a WORD address (D0 is a 16 bit number). This means the whole word (not just bit 0) will be overwritten. I have changed this to send to PLC address M0

Try this (untested as I dont have a mitsi PLC to hand)...

[{"id":"317e7cb2.f1c6a4","type":"function","z":"4a6ba2c4.481ffc","name":"Store Data","func":"var data1 = context.get(\"data1\") || 0; //get from context or default to 0\nvar data2 = context.get(\"data2\") || 0; //get from context or default to 0\n\nswitch (msg.topic){\n\n    case \"task1\":\n        data1 = msg.payload; \n        context.set(\"data1\", data1);//store in context for next time \n        return;\n        \n    case \"task2\":\n        data2 = msg.payload;\n        context.set(\"data2\", data2); //store in context for next time \n        return;\n        \n    case \"Marche\":  {\n        if( data1 == '0' && data2 == '0') {  \n            msg.payload = undefined\n        } else {\n            msg.payload = \" text 1 \"+ data1 +\" , text2 \" + data2;\n        }\n     }\n}\n\n//msg.topic = \"go\";\nreturn msg;\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":278,"y":128,"wires":[["61dcc596.e55b5c","cdefef64.81613"]]},{"id":"b74b6e1.b73569","type":"ui_text_input","z":"4a6ba2c4.481ffc","name":"","label":"code","tooltip":"","group":"a63b29d.f4c81d8","order":2,"width":0,"height":0,"passthru":false,"mode":"text","delay":"0","topic":"task1","x":66,"y":80,"wires":[["317e7cb2.f1c6a4"]]},{"id":"9439235c.d3aef","type":"ui_text_input","z":"4a6ba2c4.481ffc","name":"","label":"Operateur","tooltip":"","group":"a63b29d.f4c81d8","order":3,"width":0,"height":0,"passthru":false,"mode":"text","delay":"0","topic":"task2","x":76,"y":128,"wires":[["317e7cb2.f1c6a4"]]},{"id":"d29cd09b.cf787","type":"ui_button","z":"4a6ba2c4.481ffc","name":"","group":"a63b29d.f4c81d8","order":4,"width":0,"height":0,"passthru":false,"label":"Marche","tooltip":"","color":"","bgcolor":"green","icon":"","payload":"","payloadType":"str","topic":"Marche","x":76,"y":176,"wires":[["317e7cb2.f1c6a4"]]},{"id":"7863dc7e.13b5d4","type":"debug","z":"4a6ba2c4.481ffc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":80,"wires":[]},{"id":"61dcc596.e55b5c","type":"json","z":"4a6ba2c4.481ffc","name":"","property":"payload","action":"str","pretty":true,"x":450,"y":128,"wires":[["36fd165f.4d09ca"]]},{"id":"36fd165f.4d09ca","type":"file","z":"4a6ba2c4.481ffc","name":"","filename":"C:\\Users\\pc\\Desktop\\test.csv","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":664,"y":128,"wires":[["7863dc7e.13b5d4"]]},{"id":"cdefef64.81613","type":"function","z":"4a6ba2c4.481ffc","name":"Check input","func":"if (msg.payload == undefined || msg.payload.trim() == \"\") {\t\n    msg.payload = \"Entre the reason\";\t\n    return [null, msg]\n}\n\nmsg.payload = [1];\nreturn [msg, null];\n\n\n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":294,"y":208,"wires":[["7bae3da0.ffb114","e4f3e3a0.c8f38"],["4f0b37f3.4670d8"]]},{"id":"7bae3da0.ffb114","type":"debug","z":"4a6ba2c4.481ffc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":514,"y":192,"wires":[]},{"id":"4f0b37f3.4670d8","type":"ui_toast","z":"4a6ba2c4.481ffc","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":true,"topic":"","name":"","x":320,"y":272,"wires":[]},{"id":"e4f3e3a0.c8f38","type":"MC Write","z":"4a6ba2c4.481ffc","name":"marche","topic":"","connection":"8a67d1bd.c84ff","data":"payload","address":"M0","addressType":"str","dataType":"msg","errorHandling":"throw","outputs":1,"x":524,"y":224,"wires":[["71c98c5a.1c8144"]]},{"id":"d931c432.aaf798","type":"ui_toast","z":"4a6ba2c4.481ffc","position":"top right","displayTime":"3","highlight":"","sendall":true,"outputs":0,"ok":"OK","cancel":"Cancel","raw":true,"topic":"","name":"","x":1104,"y":240,"wires":[]},{"id":"71c98c5a.1c8144","type":"switch","z":"4a6ba2c4.481ffc","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":674,"y":224,"wires":[["5b3fd681.bd11d8"],["cb553084.4e3a1"]]},{"id":"cb553084.4e3a1","type":"change","z":"4a6ba2c4.481ffc","name":"Failed to write to PLC","rules":[{"t":"set","p":"payload","pt":"msg","to":"Failed to write to PLC","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":884,"y":256,"wires":[["d931c432.aaf798"]]},{"id":"5b3fd681.bd11d8","type":"change","z":"4a6ba2c4.481ffc","name":"Machine ready to operate","rules":[{"t":"set","p":"payload","pt":"msg","to":"Machine ready to operate","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":894,"y":208,"wires":[["d931c432.aaf798"]]},{"id":"a63b29d.f4c81d8","type":"ui_group","name":"Form","tab":"4bed3390.45d6dc","order":1,"disp":true,"width":"15","collapse":false},{"id":"8a67d1bd.c84ff","type":"MC Protocol Connection","name":"","host":"localhost","port":"5003","protocol":"UDP","frame":"4E","plcType":"Q","ascii":false,"PLCStation":"","PCStation":"","PLCModuleNo":"","network":"","octalInputOutput":false,"timeout":"3000"},{"id":"4bed3390.45d6dc","type":"ui_tab","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Other recommendations...

  • Use a ui_form instead of separate UI items (you get all values at once when the submit button is pressed - far easier
  • Clear the form once input is sent
1 Like

thank you so much @Steve-Mcl thank you
yes, I will try the UI_form just I don't have full knowledge about data structure... so I use the algorithm logic
and about clearing the form input I still don't know how is I will do it with a function or with nodes options
thank you so much for your time

hi @Colin, yes I am sorry for my topic was not clear it's just I want to clean my flow and the goal is if an operator didn't write a why he wants to turn off the machine, this one must note turn on and my problem is just was with data type and structure of payload

I think ui_form does this by default

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