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

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