Write Modbus TCP strings

Flow with modbus string writes that allow editing the string (deleting characters)

[{"id":"b9582923.cba97","type":"ui_text_input","z":"2a13cd58.b779ba","name":"","label":"Batch Name","tooltip":"","group":"948397c1.059f58","order":20,"width":0,"height":0,"passthru":false,"mode":"text","delay":"0","topic":"topic","topicType":"msg","x":670,"y":3540,"wires":[["79e29628.cfdbc8","6970cb42.9a404c","9485f33f.bfce68"]]},{"id":"6970cb42.9a404c","type":"delay","z":"2a13cd58.b779ba","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":720,"y":3620,"wires":[["651fa960.15ea88"]]},{"id":"79e29628.cfdbc8","type":"function","z":"2a13cd58.b779ba","name":"ASCII to Number","func":"\nvar numArray = [];\nvar len = flow.get('arrlength');\nnumArray.length = len;\nvar charLength = numArray.length;\nvar temp = 8;\nfor (var i = 0; i < charLength; i++) {\n    numArray[i] = temp;\n}\n\nmsg.payload = numArray;\n\nflow.set('clearlength', charLength);\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":890,"y":3600,"wires":[["c2205495.1e756","30020a00.b58ece"]]},{"id":"651fa960.15ea88","type":"function","z":"2a13cd58.b779ba","name":"ASCII to Number","func":"char = msg.payload;\nvar numArray = [];\ncharArray = char.split(\"\");\nvar charLength = charArray.length;\n\nfor (var i = 0; i < charLength; i++) {\n   var temp;\n   temp = charArray[i];\n    numArray[i] = temp.charCodeAt(0);\n}\n\nmsg.payload = numArray;\n\nflow.set('arrlength', charLength);\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":870,"y":3680,"wires":[["7228c35e.5db18c","6d68afe0.30977"]]},{"id":"c2205495.1e756","type":"function","z":"2a13cd58.b779ba","name":"context and address","func":"flow.set('address', 30727);\n//msg.payload = msg.payload * 100;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1220,"y":3500,"wires":[["2c822ef1.56bc1a"]]},{"id":"6d68afe0.30977","type":"function","z":"2a13cd58.b779ba","name":"context and address","func":"flow.set('address', 30727);\n//msg.payload = msg.payload * 100;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1240,"y":3580,"wires":[["940da113.6ad1"]]},{"id":"2c822ef1.56bc1a","type":"function","z":"2a13cd58.b779ba","name":"write ","func":"var address = flow.get('address');\nvar quantity = flow.get('clearlength');\nmsg.payload = { value: msg.payload, 'fc': 16, 'unitid': 1, 'address': address, 'quantity': quantity};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1390,"y":3140,"wires":[["7f9913eb.342dac"]]},{"id":"940da113.6ad1","type":"function","z":"2a13cd58.b779ba","name":"write ","func":"var address = flow.get('address');\nvar quantity = flow.get('arrlength');\nmsg.payload = { value: msg.payload, 'fc': 16, 'unitid': 1, 'address': address, 'quantity': quantity};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1470,"y":3180,"wires":[["7f9913eb.342dac"]]},{"id":"7f9913eb.342dac","type":"modbus-flex-write","z":"2a13cd58.b779ba","name":"flex writer","showStatusActivities":true,"showErrors":true,"server":"14e54849.e1c328","emptyMsgOnFail":false,"keepMsgProperties":false,"x":1410,"y":2460,"wires":[[],[]]},{"id":"948397c1.059f58","type":"ui_group","name":"Batch","tab":"13616c77.bce65c","order":1,"disp":true,"width":"6","collapse":false},{"id":"14e54849.e1c328","type":"modbus-client","name":"nanodac","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"10.0.0.2","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":1,"commandDelay":20,"clientTimeout":4000,"reconnectOnTimeout":false,"reconnectTimeout":2000,"parallelUnitIdsAllowed":false},{"id":"13616c77.bce65c","type":"ui_tab","name":"Batch","icon":"dashboard","disabled":false,"hidden":false}]

This seems to work for reading strings. I used, flow.get/set to track my array length so when I read I don't display garbage at the end of the array. Might be a smarter way to do this, but since I'm inputing the characters I will always know the length. I'm rusty on the javascript.

var num = [];
num = msg.payload;
var charArray = [];
var len = flow.get('arrlength');
var numLength = len;

for (var i = 0; i < numLength; i++) {
   var temp;
   temp = num[i];
    charArray[i] = String.fromCharCode(temp);
    
}

msg.payload = charArray.join("");
return msg;

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