I made this flow for read data from PLC siemens and it works well [I used the nodes7 module]
My issue is if I want to write how can I do this ??
I get the data and the structure that I will write in it from my database:
the output of the database is :
id IP rack slot port address
4 172.31.51.5 0 3 102 DB247,WORD0
4 172.31.51.5 0 3 102 DB247,WORD2
4 172.31.51.5 0 3 102 DB247,DWORD4
4 172.31.51.5 0 3 102 DB247,DWORD12
4 172.31.51.5 0 3 102 DB247,WORD16
4 172.31.51.5 0 3 102 DB247,WORD20
4 172.31.51.5 0 3 102 DB247,WORD22
4 172.31.51.5 0 3 102 DB247,WORD30
4 172.31.51.5 0 3 102 DB247,WORD32
4 172.31.51.5 0 3 102 DB247,DWORD64
4 172.31.51.5 0 3 102 DB247,DWORD72
4 172.31.51.5 0 3 102 DB247,WORD76
4 172.31.51.5 0 3 102 DB247,WORD80
4 172.31.51.5 0 3 102 DB247,WORD82
4 172.31.51.5 0 3 102 DB247,WORD90
4 172.31.51.5 0 3 102 DB247,WORD92
4 172.31.51.5 0 3 102 DB247,DWORD124
4 172.31.51.5 0 3 102 DB247,DWORD132
4 172.31.51.5 0 3 102 DB247,WORD136
4 172.31.51.5 0 3 102 DB247,WORD140
4 172.31.51.5 0 3 102 DB247,WORD142
4 172.31.51.5 0 3 102 DB247,WORD150
4 172.31.51.5 0 3 102 DB247,WORD152
4 172.31.51.5 0 3 102 DB247,DWORD184
4 172.31.51.5 0 3 102 DB247,DWORD192
4 172.31.51.5 0 3 102 DB247,WORD196
4 172.31.51.5 0 3 102 DB247,WORD200
4 172.31.51.5 0 3 102 DB247,WORD202
4 172.31.51.5 0 3 102 DB247,WORD210
4 172.31.51.5 0 3 102 DB247,WORD212
4 172.31.51.5 0 3 102 DB247,DWORD244
4 172.31.51.5 0 3 102 DB247,DWORD252
4 172.31.51.5 0 3 102 DB247,WORD256
4 172.31.51.5 0 3 102 DB247,WORD260
4 172.31.51.5 0 3 102 DB247,WORD262
4 172.31.51.5 0 3 102 DB247,WORD270
4 172.31.51.5 0 3 102 DB247,WORD272
4 172.31.51.5 0 3 102 DB247,DWORD304
4 172.31.51.5 0 3 102 DB247,DWORD312
4 172.31.51.5 0 3 102 DB247,WORD316
4 172.31.51.5 0 3 102 DB247,WORD320
4 172.31.51.5 0 3 102 DB247,WORD322
4 172.31.51.5 0 3 102 DB247,WORD330
4 172.31.51.5 0 3 102 DB247,WORD332
4 172.31.51.5 0 3 102 DB247,DWORD364
4 172.31.51.5 0 3 102 DB247,DWORD372
4 172.31.51.5 0 3 102 DB247,WORD376
4 172.31.51.5 0 3 102 DB247,WORD380
4 172.31.51.5 0 3 102 DB247,WORD382
4 172.31.51.5 0 3 102 DB247,WORD390
4 172.31.51.5 0 3 102 DB247,WORD392
4 172.31.51.5 0 3 102 DB247,DWORD424
4 172.31.51.5 0 3 102 DB247,DWORD432
4 172.31.51.5 0 3 102 DB247,WORD436
4 172.31.51.5 0 3 102 DB247,WORD440
4 172.31.51.5 0 3 102 DB247,WORD442
4 172.31.51.5 0 3 102 DB247,WORD450
4 172.31.51.5 0 3 102 DB247,WORD452
4 172.31.51.5 0 3 102 DB247,DWORD484
4 172.31.51.5 0 3 102 DB247,DWORD492
4 172.31.51.5 0 3 102 DB247,WORD496
4 172.31.51.5 0 3 102 DB247,WORD500
4 172.31.51.5 0 3 102 DB247,WORD502
4 172.31.51.5 0 3 102 DB247,WORD510
4 172.31.51.5 0 3 102 DB247,WORD512
4 172.31.51.5 0 3 102 DB247,DWORD544
4 172.31.51.5 0 3 102 DB247,DWORD552
4 172.31.51.5 0 3 102 DB247,WORD556
4 172.31.51.5 0 3 102 DB247,WORD560
4 172.31.51.5 0 3 102 DB247,WORD562
4 172.31.51.5 0 3 102 DB247,WORD570
4 172.31.51.5 0 3 102 DB247,WORD572
4 172.31.51.5 0 3 102 DB247,WORD604
the Json Code :
[
{
"id": "508cdba203cf6e44",
"type": "inject",
"z": "2cbd64164b60e010",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 360,
"y": 340,
"wires": [
[
"f428c5dd30965452"
]
]
},
{
"id": "5eb5b24d87e3d498",
"type": "comment",
"z": "2cbd64164b60e010",
"name": "Reset Counter And Read Structure of Message Once",
"info": "Reset Counter",
"x": 810,
"y": 240,
"wires": []
},
{
"id": "f428c5dd30965452",
"type": "function",
"z": "2cbd64164b60e010",
"name": "MSG_1222",
"func": "let node_Status = flow.get('NODE_STATUS_FIRE');\nlet node_Status_value = msg.node_Status_value\n\n\nif ((node_Status_value === 10) || (node_Status === undefined)) {\n msg.payload = \"\"\n\n let variablesToInitialize = [\n 'Allocated_Billets', 'Hold_Data_From_Database','NODE_STATUS_FIRE'\n ];\n\n\n\n\n for (let variable of variablesToInitialize) {\n flow.set(variable, 0);\n }\n\n\n\n msg.payload = `SELECT\n HCP.ID AS id,\n HCP.IP AS ip,\n HCP.RACK AS rack,\n HCP.SLOT AS slot,\n HCP.PORT AS port,\n 'DB' + CAST(CMV.DATA_BLOCK AS varchar(200)) + ',' + CAST(CDBT.TYPE AS varchar(200)) + CAST(CMV.ADDRESS AS varchar(200)) AS address\nFROM [dbo].[CONFIGURATION_PLC] HCP\nLEFT OUTER JOIN [dbo].[CONFIGURATION_MESSAGE_VARIABLES] CMV ON HCP.ID = CMV.PLC_ID\nLEFT OUTER JOIN [dbo].[CONFIGURATION_DATA_BLOCK_TYPE] CDBT ON CMV.TYPE = CDBT.ID\nWHERE HCP.ENABLED = 1\n AND PLC_ID IN (4)\n AND CMV.TYPE <> 1\n AND (CMV.MESSAGE_NAME IN ('MSG_1222'))\n ORDER BY CMV.ID ASC;\n`\n\n\n return msg;\n}",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 650,
"y": 340,
"wires": [
[
"9a6703f45afa61b3"
]
]
},
{
"id": "9a6703f45afa61b3",
"type": "MSSQL",
"z": "2cbd64164b60e010",
"mssqlCN": "8e11ccb0.5a6eb",
"name": "",
"outField": "payload",
"returnType": 0,
"throwErrors": 1,
"query": "",
"modeOpt": "queryMode",
"modeOptType": "query",
"queryOpt": "payload",
"queryOptType": "editor",
"paramsOpt": "queryParams",
"paramsOptType": "none",
"rows": "rows",
"rowsType": "msg",
"parseMustache": true,
"params": [],
"x": 1000,
"y": 340,
"wires": [
[
"bc9622c95d65eea7"
]
]
},
{
"id": "bc9622c95d65eea7",
"type": "function",
"z": "2cbd64164b60e010",
"name": "Hold Data Structure",
"func": "flow.set('Hold_Data_From_Database', flow.get('Hold_Data_From_Database') || msg.payload);\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1270,
"y": 340,
"wires": [
[
"92cd43a533dae40a"
]
]
},
{
"id": "53d5e932b6255bf9",
"type": "function",
"z": "2cbd64164b60e010",
"name": "Set NODE_STATUS_FIRE ",
"func": "flow.set('NODE_STATUS_FIRE', undefined)\n\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 800,
"y": 120,
"wires": [
[
"71143e694e9289b2"
]
]
},
{
"id": "bd4440ac5c7a17a1",
"type": "inject",
"z": "2cbd64164b60e010",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 600,
"y": 120,
"wires": [
[
"53d5e932b6255bf9"
]
]
},
{
"id": "71143e694e9289b2",
"type": "debug",
"z": "2cbd64164b60e010",
"name": "debug 258",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 1050,
"y": 120,
"wires": []
},
{
"id": "3f976c218e6fd163",
"type": "comment",
"z": "2cbd64164b60e010",
"name": "Set NODE_STATUS_FIRE with 0",
"info": "Set NODE_STATUS_FIRE with 0",
"x": 750,
"y": 40,
"wires": []
},
{
"id": "0a5232eea47fa17a",
"type": "debug",
"z": "2cbd64164b60e010",
"name": "Read Whole Message from PLC 1222",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload.var0",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1470,
"y": 640,
"wires": []
},
{
"id": "059c770775cb964b",
"type": "function",
"z": "2cbd64164b60e010",
"name": "Open Connection and Write Data to PLC",
"func": "let check_Node_Status = msg.node_Status_value\n\nif (check_Node_Status === 5) {\n\n\n let holdStructurefromDatabase = flow.get('Hold_Data_From_Database')\n\n let connPLCs = [...holdStructurefromDatabase]; // create a copy of holdStructurefromDatabase\n let dataReadfromPLC = [];\n\n\n // Remove the 'address' property from each object\n let connectionPLC_without_address = connPLCs.map(obj => {\n let newObj = { ...obj }; // create a copy of each object\n delete newObj.address; // remove the 'address' property from the copy\n return newObj;\n });\n\n // Remove duplicates from the array\n let connectionPLC = [...new Set(connectionPLC_without_address.map(JSON.stringify))].map(JSON.parse);\n\n for (let i = 0; i < connectionPLC.length; i++) {\n\n\n let connParam_linkID = connectionPLC[i].id;\n let connParam_Port = connectionPLC[i].port; //102;\n let connParam_IPAddress = connectionPLC[i].ip; //'192.168.150.10';\n let connParam_rack = connectionPLC[i].rack; //0;\n let connParam_slot = connectionPLC[i].slot;\n\n\n let conn = new nodes7;\n let doneReading = false;\n let doneWriting = false;\n\n let variables = getAddressesById(holdStructurefromDatabase, connectionPLC[i].id);\n let var_names = Object.keys(variables);\n\n dataReadfromPLC.push(var_names);\n\n conn.initiateConnection({ port: connParam_Port, host: connParam_IPAddress, rack: connParam_rack, slot: connParam_slot, debug: true }, connected); // slot 2 for 300/400, slot 1 for 1200/1500, change debug to true to get more info\n\n function connected(err) {\n if (typeof (err) !== \"undefined\") {\n console.log(err);\n }\n conn.setTranslationCB(function (tag) { return variables[tag]; }); // This sets the \"translation\" to allow us to work with object names\n conn.addItems(var_names);\n\nconsole.log('Here We Reached ');\n\n\n conn.readAllItems(valuesReady)\n }\n function valuesReady(anythingBad, values) {\n if (anythingBad) {\n node.warn(\"SOMETHING WENT WRONG READING VALUES!!!!\");\n conn.dropConnection();\n conn.connectionCleanup();\n }\n // console.log(values);\n msg.payload = values;\n // flow.set('PLC1_Data', values)\n node.send(msg);\n\n conn.dropConnection();\n conn.connectionCleanup();\n }\n\n }\n\n\n\n\n\n // return { payload: dataReadfromPLC }\n if (\n !(dataReadfromPLC.length === 1 && Array.isArray(dataReadfromPLC[0]) && dataReadfromPLC[0].length === 93)\n ) {\n return { payload: dataReadfromPLC };\n }\n\n}\n\n\n\n\nfunction getAddressesById(cfg, id) {\n // filter the array of objects to include only objects with the given id\n let filteredCfg = cfg.filter(obj => obj.id === id);\n\n //map the filtered objects to an array of only the \"address\" values\n let addresses = filteredCfg.map(obj => obj.address);\n\n // create an empty result object\n let result = {};\n\n //loop over the addresses array and add each address to the result object with a unique \"var\" key\n addresses.forEach((address, index) => {\n result[`var${index}`] = address;\n });\n\n //return the result object\n return result;\n}\n\n\n\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [
{
"var": "nodes7",
"module": "nodes7"
}
],
"x": 840,
"y": 640,
"wires": [
[
"0a5232eea47fa17a"
]
]
},
{
"id": "43ce51de65dd73bf",
"type": "function",
"z": "2cbd64164b60e010",
"name": "Check_Node_Status",
"func": "let node_Status = flow.get('NODE_STATUS_FIRE');\n\nif (node_Status === undefined) {\n msg.node_Status_value = 10;\n}\nelse {\n msg.node_Status_value = 5;\n}\n\n\nmsg.payload = node_Status\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 480,
"y": 640,
"wires": [
[
"059c770775cb964b",
"f428c5dd30965452"
]
]
},
{
"id": "92cd43a533dae40a",
"type": "debug",
"z": "2cbd64164b60e010",
"name": "debug 259",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 1490,
"y": 340,
"wires": []
},
{
"id": "531461e7383f3676",
"type": "comment",
"z": "2cbd64164b60e010",
"name": "Write Allocated Billets",
"info": "Write Allocated Billets",
"x": 780,
"y": 500,
"wires": []
},
{
"id": "484965dde731ce1b",
"type": "inject",
"z": "2cbd64164b60e010",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": "1",
"topic": "",
"payload": "1",
"payloadType": "num",
"x": 270,
"y": 640,
"wires": [
[
"43ce51de65dd73bf"
]
]
},
{
"id": "8e11ccb0.5a6eb",
"type": "MSSQL-CN",
"name": "TESR",
"server": "172.31.51.100",
"port": "1433",
"encyption": false,
"trustServerCertificate": false,
"database": "SUEZSTEELRM_RML",
"useUTC": false,
"connectTimeout": "",
"requestTimeout": "",
"cancelTimeout": "",
"pool": "",
"parseJSON": false,
"enableArithAbort": true
}
]