Skip to content

Instantly share code, notes, and snippets.

@arthur1990
Created February 5, 2017 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthur1990/fbc1e620351def66f0e3164fa56b2fef to your computer and use it in GitHub Desktop.
Save arthur1990/fbc1e620351def66f0e3164fa56b2fef to your computer and use it in GitHub Desktop.
RS232/Serial/Asyncronous Trigger->Response

Let say you are communicating with a machine in RS232 or any other protocol : you are receiving a response from the machine following a specific trigger you send.

this flow allow you to link the topic from the trigger you send to the machine to the payload of the response you get from the machine. so you can then process the result knowing what data are expressed in the machine response.

The main element of the flow is the fonction named : Combinator v5 this fonction wait to receive an input from the trigger of the machine (very important) THEN wait to receive the payload from the machine response and create a new message containing the response from the machine as msg.payload and the message from the trigger as msg.topic

the code of this fonction is bellow :

context.data = context.data || {};
switch (msg.topic) {
        case "tpc_payload":
            
            if(context.data.tpc_flag==1) {
            context.data.tpc_payload = msg.payload;
            }
            msg = null;
            break;
        case "tpc_topic":
            context.data.tpc_flag = 1;
            context.data.tpc_topic = msg.payload;
            //context.data.time2 = new Date().toString();
            msg = null;
            break;
        default:
            msg = null;
            break;
}

if(context.data.tpc_payload != null && context.data.tpc_topic != null ) {
    var output_msg = context.data.tpc_payload;
    var output_tpc = context.data.tpc_topic;
    context.data=null;
    
    return {payload:output_msg,topic:output_tpc};
} else return msg;
[{"id":"be480cc5.a2fb78","type":"inject","z":"c0fd550e.bc4408","name":"","topic":"tpc_topic","payload":"MEAS:VOLT:ALL?","payloadType":"str","repeat":"","crontab":"","once":false,"x":341.6363830566406,"y":333.36376953125,"wires":[["36de8282.50a74e","b94d49ef.94302","d4d507b4.3db338"]]},{"id":"b2c4b4e.d0b2fc8","type":"comment","z":"c0fd550e.bc4408","name":"-> RS232 ->","info":"","x":610.5454711914062,"y":364.5228576660156,"wires":[]},{"id":"e5e993e3.2f5ec8","type":"inject","z":"c0fd550e.bc4408","name":"","topic":"tpc_topic","payload":"MEAS:CURR:ALL?","payloadType":"str","repeat":"","crontab":"","once":false,"x":323,"y":552.2728271484375,"wires":[["3734cf6a.e7d3b8","b94d49ef.94302","4c754bb5.d52e2c"]]},{"id":"aca5d992.82c6e","type":"comment","z":"c0fd550e.bc4408","name":"-> RS232 ->","info":"","x":595.5454711914062,"y":519.2728271484375,"wires":[]},{"id":"d49622ec.830b38","type":"debug","z":"c0fd550e.bc4408","name":"payload1","active":true,"console":"false","complete":"payload","x":1958.1817626953125,"y":335.363525390625,"wires":[]},{"id":"ffb875e1.cd852","type":"comment","z":"c0fd550e.bc4408","name":"-> RS232 ->","info":"","x":800.4774169921875,"y":432.95428466796875,"wires":[]},{"id":"a0af9205.b12b58","type":"function","z":"c0fd550e.bc4408","name":"set \"tpc_payload\" as topic","func":"msg.topic=\"tpc_payload\";\nreturn msg;","outputs":1,"noerr":0,"x":1418.6591796875,"y":432.0452880859375,"wires":[["f2f46f57.f3dad","2ddedd29.3aed2a","4c754bb5.d52e2c","d4d507b4.3db338"]]},{"id":"aff3b4f2.5610a","type":"debug","z":"c0fd550e.bc4408","name":"topic1","active":true,"console":"false","complete":"topic","x":1948.9998168945312,"y":287.9090576171875,"wires":[]},{"id":"2ddedd29.3aed2a","type":"debug","z":"c0fd550e.bc4408","name":"payload2","active":true,"console":"false","complete":"payload","x":1697.1817016601562,"y":459.818115234375,"wires":[]},{"id":"f2f46f57.f3dad","type":"debug","z":"c0fd550e.bc4408","name":"topic2","active":true,"console":"false","complete":"topic","x":1695.4544067382812,"y":415.272705078125,"wires":[]},{"id":"169d14f9.cb8993","type":"debug","z":"c0fd550e.bc4408","name":"topic6","active":true,"console":"false","complete":"topic","x":1927.3635864257812,"y":582,"wires":[]},{"id":"cacf6a73.e12a48","type":"debug","z":"c0fd550e.bc4408","name":"payload6","active":true,"console":"false","complete":"payload","x":1932.3635864257812,"y":625,"wires":[]},{"id":"3734cf6a.e7d3b8","type":"debug","z":"c0fd550e.bc4408","name":"CURR TRIGGER","active":false,"console":"false","complete":"payload","x":662.3635864257812,"y":615,"wires":[]},{"id":"36de8282.50a74e","type":"debug","z":"c0fd550e.bc4408","name":"VOLT TRIGGER","active":false,"console":"false","complete":"payload","x":643.3635864257812,"y":272,"wires":[]},{"id":"b94d49ef.94302","type":"function","z":"c0fd550e.bc4408","name":"RS232 Response simulator","func":"msg.payload = new Date().toString();\nmsg.topic= null;\nreturn msg;","outputs":1,"noerr":0,"x":1007,"y":431,"wires":[["c1ef25c2.12f49"]]},{"id":"c1ef25c2.12f49","type":"delay","z":"c0fd550e.bc4408","name":"delay","pauseType":"delay","timeout":"5","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"","rateUnits":"second","randomFirst":"1","randomLast":"100","randomUnits":"milliseconds","drop":false,"x":1197,"y":431,"wires":[["a0af9205.b12b58"]]},{"id":"4c754bb5.d52e2c","type":"function","z":"c0fd550e.bc4408","name":"Combinator v5","func":"context.data = context.data || {};\nswitch (msg.topic) {\n case \"tpc_payload\":\n \n if(context.data.tpc_flag==1) {\n context.data.tpc_payload = msg.payload;\n }\n msg = null;\n break;\n case \"tpc_topic\":\n context.data.tpc_flag = 1;\n context.data.tpc_topic = msg.payload;\n //context.data.time2 = new Date().toString();\n msg = null;\n break;\n default:\n msg = null;\n break;\n}\n\nif(context.data.tpc_payload != null && context.data.tpc_topic != null ) {\n var output_msg = context.data.tpc_payload;\n var output_tpc = context.data.tpc_topic;\n context.data=null;\n \n return {payload:output_msg,topic:output_tpc};\n} else return msg;","outputs":"1","noerr":0,"x":1691,"y":560,"wires":[["169d14f9.cb8993","cacf6a73.e12a48"]]},{"id":"d4d507b4.3db338","type":"function","z":"c0fd550e.bc4408","name":"Combinator v5","func":"context.data = context.data || {};\nswitch (msg.topic) {\n case \"tpc_payload\":\n \n if(context.data.tpc_flag==1) {\n context.data.tpc_payload = msg.payload;\n }\n msg = null;\n break;\n case \"tpc_topic\":\n context.data.tpc_flag = 1;\n context.data.tpc_topic = msg.payload;\n //context.data.time2 = new Date().toString();\n msg = null;\n break;\n default:\n msg = null;\n break;\n}\n\nif(context.data.tpc_payload != null && context.data.tpc_topic != null ) {\n var output_msg = context.data.tpc_payload;\n var output_tpc = context.data.tpc_topic;\n context.data=null;\n \n return {payload:output_msg,topic:output_tpc};\n} else return msg;","outputs":"1","noerr":0,"x":1690,"y":333,"wires":[["aff3b4f2.5610a","d49622ec.830b38"]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment