创build节点红色的二进制有效载荷

新的节点红色和JavaScript。 我需要使用TCPinput连接到继电器控制器的状态。 我正在使用函数节点来生成一个两字节的请求,将stream向TCPinput节点和控制器,但不知道如何格式化在Java中。 我可以设置

msg.payload = "hello"; 

发送一个string,但我需要发送2个字节:0xEF 0xAA。 在C#中,我只是创buildstring

 msg.payload = "\xEF\xAA"; 

或者其他的东西。 如何在java / node-red中做到这一点?

二进制有效载荷是NodeJS 缓冲区对象,因此可以像这样创build:

 msg.payload = new Buffer([0xEF,0xAA]); 

从今天开始( nodered 0.17.5 ),可以通过以下操作来实现:参见文档 :

 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.