# Node Red socketcan output frame received problem

**URL:** <https://discourse.nodered.org/t/node-red-socketcan-output-frame-received-problem/73297>\
**Category:** General\
**Created:** [6 January 2023 13:35 UTC](https://discourse.nodered.org/t/node-red-socketcan-output-frame-received-problem/73297 "2023-01-06T13:35:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![David321](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@David321](https://discourse.nodered.org/u/David321)\
**Post date:** [6 January 2023 13:35 UTC](https://discourse.nodered.org/t/node-red-socketcan-output-frame-received-problem/73297/1 "2023-01-06T13:35:16Z")

</div>

Hello,  
I am working on a node red project that uses sockectcan out.  
I have a problem when receiving a frame.  
I receive a data frame containing 2 separate bytes, example "0x1004". When I receive it, I get 0x104, which is annoying because the zero of the byte "0x04" is deleted.  
How can I receive the correct data frame without the zero being erased?  
Thanks a lot.

---

<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:** [6 January 2023 13:48 UTC](https://discourse.nodered.org/t/node-red-socketcan-output-frame-received-problem/73297/2 "2023-01-06T13:48:28Z")

</div>

Welcome to the forum @David321

Please feed the data you are receiving into a Debug node and show us what you see. Then we can be sure exactly what form the data is in. You can paste a screenshot here.  
Also tell us what you want to do with that data.

---

<div class="post-metadata">

**Author:** ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)\
**Post date:** [6 January 2023 14:40 UTC](https://discourse.nodered.org/t/node-red-socketcan-output-frame-received-problem/73297/3 "2023-01-06T14:40:06Z")

</div>

I believe you're working with the array `msg.payload.data`?

We are doing something like `msg.payload.data[i].toString(16).padStart(2, '0');` to get leading zeros.

---

<div class="post-metadata">

**Author:** ![David321](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@David321](https://discourse.nodered.org/u/David321)\
**Post date:** [6 January 2023 14:53 UTC](https://discourse.nodered.org/t/node-red-socketcan-output-frame-received-problem/73297/4 "2023-01-06T14:53:11Z")

</div>

the frame represents a voltage value.  
This is what i put in sockecan in :  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dcadb19748f73fa06a4831fc87a26ba6c49661ce.png)

When I send the frame 0x0410, which corresponds to 41V, in the socketcan in,  
I receive the frame 0x410 at the output of the socketcan out, which causes a problem because the voltage value is not the same. So I would like to receive 0x0410 at the output of the socketcan out  
This is what i received in socketcan out :  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ff5c7f1e5eab61034009e624c13fc871f492d85d.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 January 2023 15:18 UTC](https://discourse.nodered.org/t/node-red-socketcan-output-frame-received-problem/73297/5 "2023-01-06T15:18:32Z")

</div>

You havent shown us how you are extracting the data (though I can guess where you are going wrong)

You can use `buffer-parser` for these kind of conversions.

I have also added a DIY function for comparison...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/f/5f9cd98f06ab891c6bfba01f39947c337c97a066.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/8247e26b19a83b4bda5fcfeda6435897f73683f8.png)

Demo flow (use `CTRL+I` to import)

```auto
[{"id":"5cc7cc13ae146210","type":"inject","z":"3ce7b2dfd0389010","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"000C0300#20010410","payloadType":"str","x":1560,"y":580,"wires":[["62f29b073ef65185"]]},{"id":"62f29b073ef65185","type":"function","z":"3ce7b2dfd0389010","name":"remove data before #","func":"const parts = msg.payload.split('#')\nmsg.payload = parts[1]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1800,"y":580,"wires":[["85240c9841bd2da2","7e296ef1a5b95560"]]},{"id":"85240c9841bd2da2","type":"function","z":"3ce7b2dfd0389010","name":"get value","func":"const buf = Buffer.from(msg.payload,\"hex\")\nmsg.payload = {\n item1: buf.readUint16LE(0) / 100,\n voltage: buf.readUint16LE(2) / 100,\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2000,"y":580,"wires":[["0a5ca2d32ff6c518"]]},{"id":"7e296ef1a5b95560","type":"buffer-parser","z":"3ce7b2dfd0389010","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"uint16le","name":"item1","offset":0,"length":1,"offsetbit":0,"scale":"/ 100","mask":""},{"type":"uint16le","name":"voltage","offset":2,"length":1,"offsetbit":0,"scale":"/ 100","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":2010,"y":640,"wires":[["8a662f13e58752d0"]]},{"id":"0a5ca2d32ff6c518","type":"debug","z":"3ce7b2dfd0389010","name":"debug 157","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2160,"y":580,"wires":[]},{"id":"8a662f13e58752d0","type":"debug","z":"3ce7b2dfd0389010","name":"debug 158","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2180,"y":640,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![David321](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@David321](https://discourse.nodered.org/u/David321)\
**Post date:** [9 January 2023 08:51 UTC](https://discourse.nodered.org/t/node-red-socketcan-output-frame-received-problem/73297/6 "2023-01-09T08:51:41Z")

</div>

I solved the problem thank to you.  
Thank you a lot.  
Have a nice day !

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 January 2023 08:52 UTC](https://discourse.nodered.org/t/node-red-socketcan-output-frame-received-problem/73297/7 "2023-01-23T08:52:40Z")

</div>

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