将此 PHP RS232 代码移植到 Node.js
Porting this PHP RS232 code to Node.js
我正在尝试将这个简单的 PHP 脚本移植到 node.js。
电视使用RS232,指令为PON为开; POF 关闭。
这个例子成功打开了我的电视:
<?php
$rs232_sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($rs232_sock, '10.0.1.155', '4999');
$rs232_in = pack("H*" ,'02'.bin2hex('PON').'03');
socket_write($rs232_sock, $rs232_in, strlen($rs232_in));
?>
我已经为 NodeJS 启动了这个:
var net = require('net');
var jspack = require('jspack/jspack.js').jspack;
client.connect('4999','10.0.1.155', function(){
console.log('CONNECTED');
// Send the RS232 command
client.write(jspack.Pack("H",'02'+bin2hex(command)+'03'));
}).on('data', function(data) {
// log the response
console.log('DATA: ' + data);
// Close the connection
client.destroy();
});
这导致:
net.js:618
throw new TypeError('invalid data');
^
TypeError: invalid data
at Socket.write (net.js:618:11)
at Socket.<anonymous> (/Users/paul/Sites/homebridge-globalcache-gc100/test.js:79:10)
at Socket.g (events.js:261:16)
at emitNone (events.js:73:20)
at Socket.emit (events.js:167:7)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1051:10)
您不需要额外的库来发送二进制数据。这样的东西应该足够了:
var net = require('net');
var PON_MSG = new Buffer('\x02PON\x03', 'binary');
var POF_MSG = new Buffer('\x02POF\x03', 'binary');
client.connect('4999','10.0.1.155', function() {
console.log('CONNECTED');
// Replace `PON_MSG` with `POF_MSG` to do POF instead
client.write(PON_MSG);
}).on('data', function(data) {
console.log('DATA: %j', data);
});
另请注意,data
事件可以触发多次,因此您何时可以安全地结束连接(如果远程端没有自动这样做)取决于协议(以确保您已收到完整回复)。
我正在尝试将这个简单的 PHP 脚本移植到 node.js。 电视使用RS232,指令为PON为开; POF 关闭。
这个例子成功打开了我的电视:
<?php
$rs232_sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($rs232_sock, '10.0.1.155', '4999');
$rs232_in = pack("H*" ,'02'.bin2hex('PON').'03');
socket_write($rs232_sock, $rs232_in, strlen($rs232_in));
?>
我已经为 NodeJS 启动了这个:
var net = require('net');
var jspack = require('jspack/jspack.js').jspack;
client.connect('4999','10.0.1.155', function(){
console.log('CONNECTED');
// Send the RS232 command
client.write(jspack.Pack("H",'02'+bin2hex(command)+'03'));
}).on('data', function(data) {
// log the response
console.log('DATA: ' + data);
// Close the connection
client.destroy();
});
这导致:
net.js:618
throw new TypeError('invalid data');
^
TypeError: invalid data
at Socket.write (net.js:618:11)
at Socket.<anonymous> (/Users/paul/Sites/homebridge-globalcache-gc100/test.js:79:10)
at Socket.g (events.js:261:16)
at emitNone (events.js:73:20)
at Socket.emit (events.js:167:7)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1051:10)
您不需要额外的库来发送二进制数据。这样的东西应该足够了:
var net = require('net');
var PON_MSG = new Buffer('\x02PON\x03', 'binary');
var POF_MSG = new Buffer('\x02POF\x03', 'binary');
client.connect('4999','10.0.1.155', function() {
console.log('CONNECTED');
// Replace `PON_MSG` with `POF_MSG` to do POF instead
client.write(PON_MSG);
}).on('data', function(data) {
console.log('DATA: %j', data);
});
另请注意,data
事件可以触发多次,因此您何时可以安全地结束连接(如果远程端没有自动这样做)取决于协议(以确保您已收到完整回复)。