Arrays 是否未复制节点JS阵列?
我用NodeJS做一个状态机,使用串口我需要接收和发送一系列命令来验证通信是否正确,在我的第二个状态我收到31字节的数据,需要在我的第四个状态重新发送它们,为此,我只需复制接收到的数组,以便稍后发送,但在我的第四个state console.log上没有显示任何内容,会发生什么 这是我的密码:Arrays 是否未复制节点JS阵列?,arrays,node.js,serial-port,Arrays,Node.js,Serial Port,我用NodeJS做一个状态机,使用串口我需要接收和发送一系列命令来验证通信是否正确,在我的第二个状态我收到31字节的数据,需要在我的第四个状态重新发送它们,为此,我只需复制接收到的数组,以便稍后发送,但在我的第四个state console.log上没有显示任何内容,会发生什么 这是我的密码: const SerialPort = require('serialport'); const ByteLength = SerialPort.parsers.ByteLength; const port
const SerialPort = require('serialport');
const ByteLength = SerialPort.parsers.ByteLength;
const port = new SerialPort("COM6");
const parser = new ByteLength({length: 1});
port.pipe(parser);
var state = 0;
var cache = [];
var history;
parser.on('data', function (data) {
cache.push(data[0]);
flowcontrol();
});
function porterr() {
console.log('error');
process.exit(1);
}
function flowcontrol() {
switch (state) {
case 0:
// ---------------------------------------------
// State 0 -> Recives ENQ, Sends ACK
// ---------------------------------------------
// Receives 1 byte
if (cache.length !== 1) {
return;
}
// Verify answer received
if (cache[0] !== 5) {
// Wrong answer received
porterr();
}
console.log('state ' + state);
// Sends Answer, clears cache, and waits for next state
port.write(Buffer.from([6]));
state++;
cache.length = 0;
break;
case 1:
// ---------------------------------------------
// State 1 -> Recives DATA, Sends ACK
// ---------------------------------------------
// Receives 31 bytes
if (cache.length !== 31) {
return;
}
console.log('state ' + state);
history = cache; // <--- data is saved for later use
// Sends Answer, clears cache, and waits for next state
port.write(Buffer.from([6]));
state++;
cache.length = 0;
break;
case 2:
// ---------------------------------------------
// State 1 -> Recives EOT, Sends ENQ
// ---------------------------------------------
// Receives 1 byte
if (cache.length !== 1) {
return;
}
// Verify answer received
if (cache[0] !== 4) {
// Wrong answer received
porterr();
}
console.log('state ' + state);
// Sends Answer, clears cache, and waits for next state
port.write(Buffer.from([5]));
state++;
cache.length = 0;
break;
case 3:
// ---------------------------------------------
// State 3 -> Recives ACK, Sends DATA
// ---------------------------------------------
// Receives 1 byte
if (cache.length !== 1) {
return;
}
// Verify answer received
if (cache[0] !== 6) {
// Wrong answer received
porterr();
}
console.log('state ' + state);
console.log[history]; // <-- Nothing is shown here!!!
// Sends Answer, clears cache, and waits for next state
//port.write(Buffer.from(history));
//state++;
//cache.length = 0;
break;
default:
console.log('not yet completed');
console.log(cache.length);
break;
}
}
const SerialPort=require('SerialPort');
const ByteLength=SerialPort.parsers.ByteLength;
常量端口=新串行端口(“COM6”);
const parser=newbytelength({length:1});
端口管道(解析器);
var状态=0;
var缓存=[];
var历史;
parser.on('data',函数(data){
cache.push(数据[0]);
流量控制();
});
函数porterr(){
console.log('error');
过程。退出(1);
}
函数flowcontrol(){
开关(状态){
案例0:
// ---------------------------------------------
//状态0->接收ENQ,发送ACK
// ---------------------------------------------
//接收1字节
如果(cache.length!==1){
返回;
}
//核实收到的答复
如果(缓存[0]!==5){
//接获错误答覆
porterr();
}
console.log('state'+state);
//发送应答,清除缓存,并等待下一个状态
端口写入(缓冲区从([6]));
状态++;
cache.length=0;
打破
案例1:
// ---------------------------------------------
//状态1->接收数据,发送确认
// ---------------------------------------------
//接收31个字节
如果(cache.length!==31){
返回;
}
console.log('state'+state);
history=cache;//接收EOT,发送ENQ
// ---------------------------------------------
//接收1字节
如果(cache.length!==1){
返回;
}
//核实收到的答复
如果(缓存[0]!==4){
//接获错误答覆
porterr();
}
console.log('state'+state);
//发送应答,清除缓存,并等待下一个状态
port.write(Buffer.from([5]));
状态++;
cache.length=0;
打破
案例3:
// ---------------------------------------------
//状态3->接收确认,发送数据
// ---------------------------------------------
//接收1字节
如果(cache.length!==1){
返回;
}
//核实收到的答复
如果(缓存[0]!==6){
//接获错误答覆
porterr();
}
console.log('state'+state);
console.log[history];//您不应该在javascript中为对象分配等于登录的值
为此使用Object.assign或lodash
代码中发生的事情是当您更改cache.length=0的值时;它也会更改历史记录的值。
因为它是一个对象而不是引用类型。你是说console.log(历史记录)?是的。历史记录不会显示,因为你写了console.log[历史记录]相反,Javascript中对象或数组的赋值不会生成数据的单独副本。它只是复制指向同一对象/数组的指针。因此,有两个变量指向同一对象。更改其中一个变量,它们都会看到更改,因为它们都指向同一对象。如果要保存数据副本,必须显式复制一份。@universeigacy duh!…你完全正确。。。