Converting Modbus registers to ASCII string

#1

I’m sure there must be an easy way to convert contiguous Modbus registers to a string since the debug window is showing me exactly what I want! Background: Using a Modbus-Flex-Getter to read 8 contiguous registers which are described as being an ASCII format (it’s a serial number). The debug node produces a path to an array[8] and buffer[16] as payload.buffer which displays the exact string I want to extract.

How do I get that string as displayed in the debug window’s payload.buffer to something I can log, write to databases, display, etc.?

#2

It would help if you showed what you are seeing in the Debug sidebar to give us the full context - can you share a screenshot?

#4

I suspect you can just do msg.responseBuffer.buffer.toString() to get the String version of the buffer’s contents. You’ll have to do that in a Function node and assign the result to another part of the msg object. For example:

msg.responseBuffer.string = msg.responseBuffer.buffer.toString();
return msg;
#5

Thanks! Your suggestion put me on the right track, this is what worked:

msg.payload = msg.responseBuffer.buffer.toString();
return msg;
#6

So to further my notes and need for help as I continue to refine the string extraction, I have discovered an issue getting the data out of Node-Red .
Something is getting corrupted somewhere with the string I'm extracting from a Modbus buffer and transmitting to InfluxDB. The string looks right everywhere I debug it with message windows, but it's clear from InfluxDB queries that there is an issue with the string it records.
Here's my function as it sends to the db:

gwSerialString = "GTYA002018";
gwSerial = msg.responseBuffer.buffer.toString('utf8',0,16);
gwPower  = msg.responseBuffer.buffer.readInt32BE(28)/10;

msg.payload = [
    {
        measurement: "test",
        fields: {
            power: gwPower
        },
        tags:{
            serialString: gwSerialString,
            serial: gwSerial
        },
        timestamp: new Date()
    }
];
return msg;

Which appears in the debug window to return the same string as follows:

However, the InfluxDB does not see them the same way because trying to use the hard coded string works fine but using the one passed from the buffer.toString gives the following error when used in queries:
found GTYA002018, expected identifier, string, number, bool

Which interestingly when pasted into some text windows and previewed I have witnessed an an extra diamond shaped character I haven't seen before preceding the expected string. This may be the clue as to where the issue is coming from although it's not appearing in this window.

I don't think anything is wrong with the batch uploader, but I cannot figure out where this string is getting messed up or how to "cleanse" it appropriately.

#7

Still not sure why it is getting mucked up, but I am “cleansing” the string with a regex successfully:
gwSerial = gwSerial.replace(/[^a-zA-Z 0-9]+/g,'');

#8

If you displayed the buffer as a hex codes I imagine you would see an unexpected character on the front.