代码之家  ›  专栏  ›  技术社区  ›  Chico3001

没有复制nodejs数组?

  •  0
  • Chico3001  · 技术社区  · 7 年前

    我使用nodejs制作了一个状态机,使用串行端口,我需要接收和发送一系列命令来验证通信是否正确,在我的第二个状态下,我接收到31字节的数据,需要在第四个状态下重新发送,因为我只需复制接收到的数组来发送它。稍后,但在我的第四个状态控制台上,日志没有显示任何内容,会发生什么?

    这是我的代码:

    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;
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   SAGAR RAVAL    7 年前

    您不应该分配一个与登录javascript相等的对象。

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

    https://lodash.com/docs/4.17.10#cloneDeep

    因为它是一个对象而不是引用类型。