Barcode Scanner Byte Array Conversion Node

#1

Has anyone created a node that would convert a byte array to a string value? I created several nodes that do the job but I was wondering if anyone has created a single node that would do it. Seems like it would be useful to a lot of people utilizing barcode scanners and USB HID. My nodes are based on this
https://github.com/riklaunim/pyusb-keyboard-alike/blob/master/keyboard_alike/mapping.py

Here is my code but the credit goes here: https://medium.com/coinmonks/iot-tutorial-read-tags-from-a-usb-rfid-reader-with-raspberry-pi-and-node-red-from-scratch-4554836be127

[{"id":"7721ca37.ab72e4","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"5a1924e9.8d800c","type":"getHIDdevices","z":"7721ca37.ab72e4","name":"","x":306.1000061035156,"y":60.200008392333984,"wires":[["6104d99b.ed2db8"]]},{"id":"4d47a1f5.91878","type":"inject","z":"7721ca37.ab72e4","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":146.10000610351562,"y":52.000000953674316,"wires":[["5a1924e9.8d800c"]]},{"id":"6104d99b.ed2db8","type":"debug","z":"7721ca37.ab72e4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":551.1000366210938,"y":86.80000305175781,"wires":[]},{"id":"42295d4d.140a74","type":"HIDdevice","z":"7721ca37.ab72e4","connection":"c00b186a.28f3f8","name":"","x":93.10000991821289,"y":119.60000514984131,"wires":[["af4b685d.f367a8"],[]]},{"id":"5684121.d9a17ec","type":"debug","z":"7721ca37.ab72e4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":543.1000862121582,"y":196.20001554489136,"wires":[]},{"id":"5d6954c9.5d582c","type":"function","z":"7721ca37.ab72e4","name":"Translate","func":"keymap = [\n '', '', '', '',\n 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',\n 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',\n '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\n', '^]', '^H',\n '^I', ' ', '-', '=', '[', ']', '\\', '>', ';', "'", '`', ',', '.',\n '/', 'CapsLock', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', 'F11', 'F12',\n 'PS', 'SL', 'Pause', 'Ins', 'Home', 'PU', '^D', 'End', 'PD', '->', '<-', '-v', '-^', 'NL',\n 'KP/', 'KP*', 'KP-', 'KP+', 'KPE', 'KP1', 'KP2', 'KP3', 'KP4', 'KP5', 'KP6', 'KP7', 'KP8',\n 'KP9', 'KP0', '\\', 'App', 'Pow', 'KP=', 'F13', 'F14'\n]\nvar replacechars = function(c){\n return keymap[c] || c;\n};\nmsg.payload = replacechars(msg.payload)\n\nreturn msg;","outputs":1,"noerr":0,"x":234.10008239746094,"y":297.2000102996826,"wires":[["e3b57a1b.be25b8"]]},{"id":"af4b685d.f367a8","type":"function","z":"7721ca37.ab72e4","name":"FilterValueOnly","func":"msg.payload = msg.payload[2]\nreturn msg;","outputs":1,"noerr":0,"x":276.1000061035156,"y":146.20000648498535,"wires":[["43118f5d.73029"]]},{"id":"e3b57a1b.be25b8","type":"join","z":"7721ca37.ab72e4","name":"JoinDigits","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"","joinerType":"str","accumulate":false,"timeout":".2","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":433.1000633239746,"y":268.200008392334,"wires":[["5684121.d9a17ec"]]},{"id":"43118f5d.73029","type":"switch","z":"7721ca37.ab72e4","name":"removeblanks","property":"payload","propertyType":"msg","rules":[{"t":"btwn","v":"5","vt":"num","v2":"39","v2t":"num"}],"checkall":"true","repair":false,"outputs":1,"x":242.10000610351562,"y":217.40000808238983,"wires":[["5d6954c9.5d582c"]]},{"id":"c00b186a.28f3f8","type":"HIDConfig","z":"","vid":"1534","pid":"4112","name":""}]

0 Likes

#2
    function stringFromArray(data)
  {
    var count = data.length;
    var str = "";
    
    for(var index = 0; index < count; index += 1)
      str += String.fromCharCode(data[index]);
    
    return str;
  }

Found this in google search. Would this help?

0 Likes

#3

I don't think so -- looks like the code is using a keyboard mapping, not byte->char mapping.

0 Likes

#4

Something interesting happened to me today when testing a barcode scanner.

I had set the serial input node to output as byte array. Conveniently, node-red seemed to interpret the byte array as a string and processed it as such through the rest of my nodes.

0 Likes