Have writing to a usb to RS-485 adapter working but how do I read back the response?

In Labview, Im used to basically drawing a line from the write to the input node of the USB read. There is not an input node on the USB read to control the flow. How do you read the response from the packet I sent?

Here is my code:

[{"id":"9a21f667.8fc0c8","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"3b8d164f.baeb12","type":"serial out","z":"9a21f667.8fc0c8","name":"","serial":"6316eb55.80e3a4","x":690,"y":360,"wires":[]},{"id":"fd418c64.ca7508","type":"inject","z":"9a21f667.8fc0c8","name":"Set to EOL","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x08\",\"0x41\",\"0x1E\",\"0x90\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x82\",\"0x03\",\"0x45\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":440,"y":360,"wires":[["3b8d164f.baeb12"]]},{"id":"475ce64b.f69e5","type":"inject","z":"9a21f667.8fc0c8","name":"Set Normal","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x08\",\"0x41\",\"0x1E\",\"0x17\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x83\",\"0x03\",\"0xC3\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":430,"y":400,"wires":[["3b8d164f.baeb12"]]},{"id":"4a6c0228.8aa92c","type":"inject","z":"9a21f667.8fc0c8","name":"MainBoard-Relay1Off","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4A\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":500,"wires":[["3b8d164f.baeb12"]]},{"id":"9e59f466.668f48","type":"inject","z":"9a21f667.8fc0c8","name":"MainBoard-Relay2On","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x00\",\"0x00\",\"0x01\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4B\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":540,"wires":[["3b8d164f.baeb12"]]},{"id":"438754f0.729e24","type":"inject","z":"9a21f667.8fc0c8","name":"MainBoard-Relay1On","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x01\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4B\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":460,"wires":[["3b8d164f.baeb12"]]},{"id":"8850d8be.bc3f88","type":"inject","z":"9a21f667.8fc0c8","name":"fake mqtt Set to end of line mode","topic":"","payload":"0201 0141 4000 0841 1E90 0000 0000 8203 45","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":580,"wires":[["b54fa00f.5ecc9"]]},{"id":"b54fa00f.5ecc9","type":"function","z":"9a21f667.8fc0c8","name":"Hex string to Hex buffer","func":"msg.oldPayload = msg.payload; //save it for later debugging\nvar noSpaces = msg.payload.replace(/ /g, '');// remove spaces\nmsg.payload = Buffer.from(noSpaces, \"hex\"); //convert to buffer\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":600,"wires":[["3b8d164f.baeb12"]]},{"id":"e47641e4.d18d38","type":"inject","z":"9a21f667.8fc0c8","name":"fake mqtt MainBoard-Relay1On","topic":"","payload":"0201 0141 4000 2220 1200 5A00 0100 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0003 4B","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":620,"wires":[["b54fa00f.5ecc9"]]},{"id":"484dec1a.a6fd5c","type":"serial in","z":"9a21f667.8fc0c8","name":"","serial":"6316eb55.80e3a4","x":870,"y":360,"wires":[["57476dc8.786784"]]},{"id":"57476dc8.786784","type":"debug","z":"9a21f667.8fc0c8","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","targetType":"msg","x":1050,"y":360,"wires":[]},{"id":"6316eb55.80e3a4","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"10000"}]

Did you not see my response on your other thread?

Our circuit board responds to the command and turns a relay on so I know from running the labview that it gets the command and sends a response. In Node Red the relay is turning on so I know our part is working correctly to write. In labview you set the bits back to read after you write and then just read next in your program flow.
I dont get any response. I dont get anything in the debug or console.

Trying the serial request gave me a time out error. Somehow i need to tell i the number of bits to read. I think that is what is timing out. Not sure how to do that yet tho...

[{"id":"f042e293.4bd948","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"be2f3e94.08e27","type":"serial out","z":"f042e293.4bd948","name":"","serial":"6316eb55.80e3a4","x":690,"y":280,"wires":[]},{"id":"2d54136f.d45514","type":"inject","z":"f042e293.4bd948","name":"Set to EOL","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x08\",\"0x41\",\"0x1E\",\"0x90\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x82\",\"0x03\",\"0x45\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":440,"y":280,"wires":[["be2f3e94.08e27"]]},{"id":"b9937022.701d4","type":"inject","z":"f042e293.4bd948","name":"Set Normal","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x08\",\"0x41\",\"0x1E\",\"0x17\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x83\",\"0x03\",\"0xC3\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":430,"y":320,"wires":[["be2f3e94.08e27"]]},{"id":"43164026.d98908","type":"inject","z":"f042e293.4bd948","name":"MainBoard-Relay1Off","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4A\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":420,"wires":[["be2f3e94.08e27"]]},{"id":"9f0a0b28.d830b","type":"inject","z":"f042e293.4bd948","name":"MainBoard-Relay2On","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x00\",\"0x00\",\"0x01\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4B\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":460,"wires":[["be2f3e94.08e27"]]},{"id":"47cfc409.596604","type":"inject","z":"f042e293.4bd948","name":"MainBoard-Relay1On","topic":"","payload":"[\"0x02\",\"0x01\",\"0x01\",\"0x41\",\"0x40\",\"0x00\",\"0x22\",\"0x20\",\"0x12\",\"0x00\",\"0x5A\",\"0x00\",\"0x01\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x00\",\"0x03\",\"0x4B\"]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":380,"wires":[["52f778b2.1c696"]]},{"id":"13c53829.be0b88","type":"inject","z":"f042e293.4bd948","name":"fake mqtt Set to end of line mode","topic":"","payload":"0201 0141 4000 0841 1E90 0000 0000 8203 45","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":500,"wires":[["d12565c9.4d6cf8"]]},{"id":"d12565c9.4d6cf8","type":"function","z":"f042e293.4bd948","name":"Hex string to Hex buffer","func":"msg.oldPayload = msg.payload; //save it for later debugging\nvar noSpaces = msg.payload.replace(/ /g, '');// remove spaces\nmsg.payload = Buffer.from(noSpaces, \"hex\"); //convert to buffer\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":520,"wires":[["be2f3e94.08e27"]]},{"id":"c8b308cb.efb1b","type":"inject","z":"f042e293.4bd948","name":"fake mqtt MainBoard-Relay1On","topic":"","payload":"0201 0141 4000 2220 1200 5A00 0100 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0003 4B","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":540,"wires":[["d12565c9.4d6cf8"]]},{"id":"52f778b2.1c696","type":"serial request","z":"f042e293.4bd948","name":"","serial":"6316eb55.80e3a4","x":690,"y":360,"wires":[["c03cb9b3.b9d49"]]},{"id":"c03cb9b3.b9d49","type":"debug","z":"f042e293.4bd948","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","x":870,"y":360,"wires":[]},{"id":"6316eb55.80e3a4","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"10000"}]

https://discourse.nodered.org/t/ascii-convert-control-character/26140

I think this is close. Somehow I need to read what has been sent back and calculate a crc and then set that for the end of the incoming message... ( kind of like how they were using /r to return )....

Hey something else i wrote :slight_smile:

The value at the end of your strings and buffer examples look to be simple 8bit XOR BCC of everything proceeding it.

BCC calculation...

[{"id":"51480b90.640c84","type":"inject","z":"40758d1c.696554","name":"fake mqtt Set to end of line mode","topic":"","payload":"0201 0141 4000 0841 1E90 0000 0000 8203 45","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":390,"y":520,"wires":[["471f1a31.2f05d4"]]},{"id":"471f1a31.2f05d4","type":"function","z":"40758d1c.696554","name":"Hex string to Hex buffer","func":"msg.oldPayload = msg.payload; //save it for later debugging\nvar noSpaces = msg.payload.replace(/ /g, '');// remove spaces\nmsg.payload = Buffer.from(noSpaces, \"hex\"); //convert to buffer\nreturn msg;","outputs":1,"noerr":0,"x":670,"y":540,"wires":[["4d1f98ce.f22738"]]},{"id":"5ed8a091.c09b8","type":"inject","z":"40758d1c.696554","name":"fake mqtt MainBoard-Relay1On","topic":"","payload":"0201 0141 4000 2220 1200 5A00 0100 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0003 4B","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":390,"y":560,"wires":[["471f1a31.2f05d4"]]},{"id":"4d1f98ce.f22738","type":"function","z":"40758d1c.696554","name":"BCC Calc","func":"msg.last = msg.payload[msg.payload.length-1]; //capture last element\nvar data = msg.payload.slice(0,-1); //lose last element\n\nlet cs = 0;//the checksum\nfor (let i = data.length - 1; i >= 0; i--) {\n    cs ^= data[i]; //XOR\n}\n\nmsg.checksum = cs;\n\nreturn msg;","outputs":1,"noerr":0,"x":880,"y":540,"wires":[["d188f30b.31816"]]},{"id":"d188f30b.31816","type":"debug","z":"40758d1c.696554","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","x":1010,"y":540,"wires":[]}]

image

It takes your fake COMM strings, strips the last HEX value off off and re-calculates the BCC (look in msg.checksum - you will see it matches your two samples which I put into msg.last for convenience)


moving forward...

I suspect you need to use the Serial Request node, send a correctly formatted buffer & then you'll get a response.

1 Like

Still get the buffer error thing when added the usb part to your code. Somehow we need to read and then use the read stuff to calcualte the checksum and set that as the end of the time out. Setting how many to read would work also but not sure how to do that

[{"id":"e016160.58e2068","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"8e2e41da.6793f8","type":"inject","z":"e016160.58e2068","name":"fake mqtt Set to end of line mode","topic":"","payload":"0201 0141 4000 0841 1E90 0000 0000 8203 45","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":260,"wires":[["ff792e63.ecf2e8"]]},{"id":"ff792e63.ecf2e8","type":"function","z":"e016160.58e2068","name":"Hex string to Hex buffer","func":"msg.oldPayload = msg.payload; //save it for later debugging\nvar noSpaces = msg.payload.replace(/ /g, '');// remove spaces\nmsg.payload = Buffer.from(noSpaces, \"hex\"); //convert to buffer\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":280,"wires":[["5bcbd601.5aa87"]]},{"id":"88411c82.9f73d","type":"inject","z":"e016160.58e2068","name":"fake mqtt MainBoard-Relay1On","topic":"","payload":"0201 0141 4000 2220 1200 5A00 0100 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0003 4B","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":300,"wires":[["ff792e63.ecf2e8"]]},{"id":"5bcbd601.5aa87","type":"function","z":"e016160.58e2068","name":"BCC Calc","func":"msg.last = msg.payload[msg.payload.length-1]; //capture last element\nvar data = msg.payload.slice(0,-1); //lose last element\n\nlet cs = 0;//the checksum\nfor (let i = data.length - 1; i >= 0; i--) {\n    cs ^= data[i]; //XOR\n}\n\nmsg.checksum = cs;\n\nreturn msg;","outputs":1,"noerr":0,"x":700,"y":280,"wires":[["51d5acde.ada794"]]},{"id":"51d5acde.ada794","type":"serial request","z":"e016160.58e2068","name":"","serial":"6316eb55.80e3a4","x":870,"y":280,"wires":[["b7e52a33.9cb5b"]]},{"id":"b7e52a33.9cb5b","type":"debug","z":"e016160.58e2068","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","x":1030,"y":280,"wires":[]},{"id":"6316eb55.80e3a4","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"10000"}]

What is it you are communicating with? Is there a documented protocol? And documented comm settings?

Its a dumb proprietary wattmaster usb-link 2, usb to 485 converter. There isn't any documentation but we use the expected bit read back to read it in labview. You are the man at this by the way. Thank you for help.

I have this working now. it says ok and im getting something back but the return buffer should of been 0241 4001 4000 0841 1E90 0000 0000 8203 45.

[{"id":"e016160.58e2068","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"8e2e41da.6793f8","type":"inject","z":"e016160.58e2068","name":"fake mqtt Set to end of line mode","topic":"","payload":"0201 0141 4000 0841 1E90 0000 0000 8203 45","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":260,"wires":[["ff792e63.ecf2e8"]]},{"id":"ff792e63.ecf2e8","type":"function","z":"e016160.58e2068","name":"Hex string to Hex buffer","func":"msg.oldPayload = msg.payload; //save it for later debugging\nvar noSpaces = msg.payload.replace(/ /g, '');// remove spaces\nmsg.payload = Buffer.from(noSpaces, \"hex\"); //convert to buffer\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":280,"wires":[["5bcbd601.5aa87"]]},{"id":"88411c82.9f73d","type":"inject","z":"e016160.58e2068","name":"fake mqtt MainBoard-Relay1On","topic":"","payload":"0201 0141 4000 2220 1200 5A00 0100 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0003 4B","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":300,"wires":[["ff792e63.ecf2e8"]]},{"id":"5bcbd601.5aa87","type":"function","z":"e016160.58e2068","name":"BCC Calc","func":"msg.last = msg.payload[msg.payload.length-1]; //capture last element\nvar data = msg.payload.slice(0,-1); //lose last element\n\nlet cs = 0;//the checksum\nfor (let i = data.length - 1; i >= 0; i--) {\n    cs ^= data[i]; //XOR\n}\n\nmsg.checksum = cs;\n\nreturn msg;","outputs":1,"noerr":0,"x":700,"y":280,"wires":[["51d5acde.ada794"]]},{"id":"51d5acde.ada794","type":"serial request","z":"e016160.58e2068","name":"","serial":"6316eb55.80e3a4","x":870,"y":280,"wires":[["b7e52a33.9cb5b"]]},{"id":"b7e52a33.9cb5b","type":"debug","z":"e016160.58e2068","name":"","active":false,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","x":1030,"y":280,"wires":[]},{"id":"759e889.7a5c978","type":"serial in","z":"e016160.58e2068","name":"","serial":"6316eb55.80e3a4","x":850,"y":440,"wires":[["c2018e1b.f4a5f"]]},{"id":"c2018e1b.f4a5f","type":"debug","z":"e016160.58e2068","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","x":1050,"y":440,"wires":[]},{"id":"6316eb55.80e3a4","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"","bin":"false","out":"char","addchar":"","responsetimeout":"10000"}]

Actually I think all we need to do is take those payloads and turn them into a hex buffer and see if it matches. It's close. There are 9 payload debug outputs and i count 10 bytes. I have to read the buffer to say see what relay outputs are on and then set the bit in the byte for the one I want to turn on and than write it back. I think we are close

Which debug is giving you replies? suspect it's the top one attached to the serial request & the 2nd serial in node isn't required.

And now you are getting data back, can you do the following?...

Its on complete message. Its like it is returning each byte read in a new payload. So the first one is

{"payload":"A","port":"/dev/ttyUSB0","_msgid":"c001b66b.a96578"}

Second is

{"payload":"@","port":"/dev/ttyUSB0","_msgid":"8630c990.484ac8"}

Is A = to 02 in hex and @ = 41 in hex? The string we should be getting back is 0241 4001 4000 0841 1E90 0000 0000 8203 45

Here are all the values of the payloads returned.

Try setting the serial config node to split on 0x03 & return a buffer

image

See if that give a more sensible response

EDIT...
And send a copy of the results once more & a description of what you believe the reply means (same as I asked before please)

1 Like

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