node-red和javascript新手。我需要使用TCP输入连接到继电器控制器以获取状态。我正在使用一个函数节点生成一个双字节请求,该请求将流向TCP输入节点并传递给控制器,但不知道如何在java中格式化它。我可以设置
msg.payload = "hello";
发送一个字符串,但我需要发送2个字节:0xEF 0xAA。在C#中我只想创建字符串
msg.payload = "\xEF\xAA";
或者其他什么。如何在java / node-red中执行此操作?
答案 0 :(得分:1)
二进制有效负载是NodeJS buffer对象,因此可以像这样创建:
msg.payload = new Buffer([0xEF,0xAA]);
答案 1 :(得分:0)
截至今天( nodered 0.17.5 ),可以通过以下方式实现这一目标,请参阅documentation:
msg.payload = Buffer.from("\xEF\xAA")
或
msg.payload = Buffer.from('hello world', 'ascii');
如您所见,您还可以指定encoding
参数:
Node.js当前支持的字符编码包括:
'ascii' - for 7-bit ASCII data only. This encoding is fast and will strip the high bit if set. 'utf8' - Multibyte encoded Unicode characters. Many web pages and other document formats use UTF-8. 'utf16le' - 2 or 4 bytes, little-endian encoded Unicode characters. Surrogate pairs (U+10000 to U+10FFFF) are supported. 'ucs2' - Alias of 'utf16le'. 'base64' - Base64 encoding. When creating a Buffer from a string, this encoding will also correctly accept "URL and Filename Safe Alphabet" as specified in RFC4648, Section 5. 'latin1' - A way of encoding the Buffer into a one-byte encoded string (as defined by the IANA in RFC1345, page 63, to be the Latin-1 supplement block and C0/C1 control codes). 'binary' - Alias for 'latin1'. 'hex' - Encode each byte as two hexadecimal characters.