# Converting Modbus registers to ASCII string

**URL:** <https://discourse.nodered.org/t/converting-modbus-registers-to-ascii-string/1010>\
**Category:** General\
**Created:** [22 June 2018 22:21 UTC](https://discourse.nodered.org/t/converting-modbus-registers-to-ascii-string/1010 "2018-06-22T22:21:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![arriflex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/arriflex/32/26643_2.png) [@arriflex](https://discourse.nodered.org/u/arriflex)\
**Post date:** [22 June 2018 22:21 UTC](https://discourse.nodered.org/t/converting-modbus-registers-to-ascii-string/1010/1 "2018-06-22T22:21:50Z")

</div>

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.?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 June 2018 22:23 UTC](https://discourse.nodered.org/t/converting-modbus-registers-to-ascii-string/1010/2 "2018-06-22T22:23:49Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 June 2018 22:34 UTC](https://discourse.nodered.org/t/converting-modbus-registers-to-ascii-string/1010/4 "2018-06-22T22:34:15Z")

</div>

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;
```

---

<div class="post-metadata">

**Author:** ![arriflex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/arriflex/32/26643_2.png) [@arriflex](https://discourse.nodered.org/u/arriflex)\
**Post date:** [22 June 2018 22:40 UTC](https://discourse.nodered.org/t/converting-modbus-registers-to-ascii-string/1010/5 "2018-06-22T22:40:23Z")

</div>

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

```auto
msg.payload = msg.responseBuffer.buffer.toString();
return msg;
```

---

<div class="post-metadata">

**Author:** ![arriflex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/arriflex/32/26643_2.png) [@arriflex](https://discourse.nodered.org/u/arriflex)\
**Post date:** [28 June 2018 04:24 UTC](https://discourse.nodered.org/t/converting-modbus-registers-to-ascii-string/1010/6 "2018-06-28T04:24:40Z")

</div>

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:

```auto
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:

 ![11](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/735608314a323373918f4cc1ecc738686ffef7b2.png)

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.

---

<div class="post-metadata">

**Author:** ![arriflex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/arriflex/32/26643_2.png) [@arriflex](https://discourse.nodered.org/u/arriflex)\
**Post date:** [28 June 2018 05:18 UTC](https://discourse.nodered.org/t/converting-modbus-registers-to-ascii-string/1010/7 "2018-06-28T05:18:58Z")

</div>

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,'');`

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 June 2018 07:27 UTC](https://discourse.nodered.org/t/converting-modbus-registers-to-ascii-string/1010/8 "2018-06-28T07:27:12Z")

</div>

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