Steve-Mcl / node-red-contrib-omron-fins

A Node-RED node to directly interface with OMRON PLCs over FINS Ethernet protocol
MIT License
13 stars 4 forks source link

writing string data to PLC #11

Closed dapadk closed 3 years ago

dapadk commented 3 years ago

Hi. I'm very new to node and PLC programing in general, the Omron fins node has been very helpful and I've successfully managed to establish a connection read data out and write data in, but I've only been able to write INT and BOOL data. I'm having trouble understanding how to write string data into the plc, i understand that i need to use the buffers but ill be completely honest and how no idea how its supposed to be set up.

i would like to be able to send the text from a payload as a string to the PLC (I'm currently using an NX1p2 to learn and test node red but will eventually be using an NX1).

any help or suggestions would be greatly appreciated . #

Steve-Mcl commented 3 years ago

Did you read the readme. It states the is a helper node called node-red-contrib-buffer-parser.

That lib has a node that helps you build a buffer from a string.

As for bool values, I don't support single bit writes. Your options include, writing 16 bits as a word or using a 16 but value of 0 or 1 to represent true/false.

Again the buffer parser node can assist in setting bits in a buffer.

I will post an example in a short while.

Steve-Mcl commented 3 years ago

ok, so as you are new to PLC programming I can safely assume you are very new to FINS protocol.

FINS is a protocol that reads and write 16 bit values to physical addresses in the PLCs memory banks (not TAGS). With that in mind, reading and writing strings can be problematic & need you to convert your strings into WD values when you want to transmit to the PLC and convert back from WD values to a string when reading back.

My personal golden rule is dont write strings to the PLC - however it is not impossible. Here is an example...

3NA3GSljJo

[{"id":"dfd55d3a.9e638","type":"FINS Write","z":"bec69dbd.8d622","name":"","connection":"d297db77.373458","addressType":"msg","address":"topic","dataType":"msg","data":"payload","msgPropertyType":"msg","msgProperty":"payload","x":1710,"y":140,"wires":[["2741dbbe.7fb424"]]},{"id":"8ba4930a.0ab86","type":"inject","z":"bec69dbd.8d622","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"D0","payload":"bye","payloadType":"str","x":1150,"y":140,"wires":[["3225b0bb.6833d"]]},{"id":"2741dbbe.7fb424","type":"debug","z":"bec69dbd.8d622","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1890,"y":140,"wires":[]},{"id":"8217ebe6.ad4958","type":"inject","z":"bec69dbd.8d622","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"D0","payload":"10","payloadType":"num","x":1150,"y":200,"wires":[["af2f1172.ff2cd"]]},{"id":"af2f1172.ff2cd","type":"FINS Read","z":"bec69dbd.8d622","name":"","connection":"d297db77.373458","addressType":"msg","address":"topic","countType":"msg","count":"payload","msgPropertyType":"msg","msgProperty":"payload","outputFormatType":"list","outputFormat":"buffer","x":1330,"y":200,"wires":[["2c8109e5.5ae2b6"]]},{"id":"2c8109e5.5ae2b6","type":"buffer-parser","z":"bec69dbd.8d622","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"string","name":"item1","offset":0,"length":10,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"output","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":1510,"y":200,"wires":[["dc071ef0.315a9"]]},{"id":"dc071ef0.315a9","type":"debug","z":"bec69dbd.8d622","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1710,"y":200,"wires":[]},{"id":"468efb1e.dd98f4","type":"inject","z":"bec69dbd.8d622","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"D0","payload":"hello","payloadType":"str","x":1160,"y":100,"wires":[["3225b0bb.6833d"]]},{"id":"fa39ad3e.fff1c","type":"buffer-parser","z":"bec69dbd.8d622","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"uint16le","name":"item1","offset":0,"length":-1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":false,"setTopic":false,"outputs":1,"x":1510,"y":140,"wires":[["dfd55d3a.9e638"]]},{"id":"3225b0bb.6833d","type":"function","z":"bec69dbd.8d622","name":"string[10] to buffer","func":"var b = Buffer.alloc(20); //10WDs\nb.write(msg.payload)\nmsg.payload = b;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1330,"y":140,"wires":[["fa39ad3e.fff1c"]]},{"id":"d297db77.373458","type":"FINS Connection","name":"","host":"127.0.0.1","port":"9600","MODE":"CSCJ","ICF":"128","DNA":"","DA1":"","DA2":"","SNA":"","SA1":"","SA2":""}]
dapadk commented 3 years ago

this is excellent, it took me a little while to realise that i was using the wrong data type in my PLC variables, and that the data was in HEX format, but now i have something i can work with.

string test

thank you very much for the help @Steve-Mcl

Steve-Mcl commented 3 years ago

Pleased you are making progress.

Try the demo again with tag datatest4 (D300) - since you have set that to a string.

If necessary, byteswap16 or use the be (big endian) versions of the conversion operators in buffer-parser if the string looks wrong.

dapadk commented 3 years ago

sending the data to datatest4 gave me a mixed up string, but as soon as i changed the buffer-paraser to big endian I'm getting perfect results.

string test 2

I'm just an automation apprentice and have been pulling my hair out trying to figure this out. This is going to help me a lot with upcoming projects so thank you so much for your time.