Try to convert msg.payload string of number into ascii

Hello,
I have to translate a string of delimited numbers into ascii string.
This is working fine:
String.fromCharCode(65,66,67); // returns 'ABC'
...
I have a msg.payload containing by example: 45,42,45,42,45,42,45,42,22,42,22,42,6,42,6,28,28

I need to translate that string into ascii like this: X0017EXXU5 (from the above example payload)
I try:
msg.payload = String.fromCharCode(msg.payload); return msg;
...in a function node

but I receive a strange aswer:
""
Strange ???

If someone have an idea???
Many thanks,
PhilLu

Welcome to the forum @PhilLu

So we can see exactly what you have, can you feed the incoming data into a debug node and show us a screenshot of what you see please.

Thank you for your answer :wink:
The first screenshot is the initial payload to use.
image

This is the code

msg.payload = String.fromCharCode(msg.payload);
return msg;

The last is the debug.
image

Many thanks for your assistance :slight_smile:
Phil.

The string 45,42,45,42,45,42,45,42,22,42,22,42,6,42,6,28,28 represents `

-*-*-*-****

not X0017EXXU5 which would be '88,48,48,49,55,69,88,88,85,53',

However, a quick and dirty method

let input = ' 45,42,45,42,45,42,45,42,22,42,22,42,6,42,6,28,28'

let numbers = input.split(',')
let output = ''

for (const element of numbers) {
    output = output + String.fromCharCode(parseInt(element, 10))
}

msg.payload = output

return msg

Maybe these are hexadecimal values?

image

One thing is certain, they do not equal what the OP states

Hello, many thanks for your answer :wink:
Strange about the collected string.
I use an usb barcode scanner on my RPI.
When I am on a simple text and I scan the barcode, I receive the correct text:
X0017EXXU5
Then, to collect that string in Node-red, I use the node Pi Keyboard.
When I scan the barcode, I receive several separated numbers.
To have a comma separated string I use JOIN with comma separator, giving:

42,45,42,45,42,11,42,11,42,11,42,11,42,2,42,2,42,8,42,8,42,18,42,18,42,45,42,45,42,45,42,45,42,22,42,22,42,6,42,6,28,28

Is it the wrong method to collect simple string from barcode?
Manythnks for your help :wink:
Phil.

Probably need to check what the scanner is actually sending (from the manual) because it certainly isn't a keystroke copy of the text.

Hello,
Many thanks for your answer :wink:
The strange think is when I use the scanner in a simple editor on the same system without any modification, it works perfectly:
image
How is it possible to get that kind of message i-as payload into Node-Red?
Thanks for your suggestions :ok_hand:
Phil.

Often, barcode scanners have 2 modes - keyboard mode and data mode (serial / ethernet for example).

If your device supports a different mode, you can bypass the whole keyboard shenanigans and use a serial node to read the raw data that it sends. This is often faster and more reliable than a keyboard mode.

The pi keyboard node returns key scan codes as it grabs the keys at the hardware level. So it is not ascii codes

Many thanks for all your answers :wink:
Now I use Node-red Dashbord input text node and perfectly get barcode when scanned :ok_hand:
Is it possible to hide dashbord (work without screen) and focus that input text at boot?
Also, is it possible to clear that field after 1sec by example?

Have a nice Sunday,
Many thanks, :pray:
Phil.

Not sure since it needs to "type" into a focused input, in keyboard mode.

However, if you can switch to data (serial? Ethernet?) You won't need dashboard at all.

Impossible to install node-red-contrib-usbhid in palette :no_mouth:
Don't now how to bypass that or use alternate solution...

Search the forum

Have you read the manual?

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