0

少し問題があります。イベント処理は理解できたと思っていましたが、今は理解できません。

Chrome を作成しましたEvent():

this.onReadLine = new chrome.Event();

このイベントは関数でディスパッチされます。

this.onReadLine.dispatch(line);

ディスパッチ命令の前に、ディスパッチ命令の引数である「行」をログに記録しようとしました。問題ありません。「行」は存在します。

コードをまっすぐ下に進むと、次の部分が見つかります。

connection.onReadLine.addListener(function(line) {                  
    logJSON(line);                                                  
}); 

onReadLineこれは、イベントがディスパッチされるたびに発生する必要があるものです。

onReadLine問題は、コードの最後に定義されているボタン '#dimmer1_Chrome_Input' を押したり離したりしたときにのみ、イベントがディスパッチされることです。どこが間違っていますか?

ここに私の完全なコード。問題に関連する部分は ////\///\/\///\\ 行で強調表示されます。

// Serial used from Arduino board
const Arduino_COM = 'COM3'; // PC

var SerialConnection = function() {                                 
  this.connectionId = -1;                                           
  this.lineBuffer = "";
  this.boundOnDataReceiving = this.onDataReceiving.bind(this);                  
  this.boundOnDataReceivingError = this.onDataReceivingError.bind(this);
  this.onConnect = new chrome.Event();

///////////////////////////\\\\\\\\\\\\\\\/////////////\\\\\\\\\\////////\\\\\\\\\\\\////////\\\\\\\\\\//////PROBLEM

  this.onReadLine = new chrome.Event();

///////////////////////////\\\\\\\\\\\\\\\/////////////\\\\\\\\\\////////\\\\\\\\\\\\////////\\\\\\\\\\//////PROBLEM

  this.onError = new chrome.Event();
};

SerialConnection.prototype.connect = function(Serial_COM_Port) {
  chrome.serial.connect(Serial_COM_Port, this.onConnectComplete.bind(this));
};

SerialConnection.prototype.onConnectComplete = function(connectionInfo) {
  if (!connectionInfo) {
    log("Connection failed.");
    return;
  }
  this.connectionId = connectionInfo.connectionId;
  chrome.serial.onReceive.addListener(this.boundOnDataReceiving);
  chrome.serial.onReceiveError.addListener(this.boundOnDataReceivingError);
  this.onConnect.dispatch();
};

SerialConnection.prototype.send = function(msg) {
  if (this.connectionId < 0) {
    throw 'Invalid connection';
  }
  chrome.serial.send(this.connectionId, String_to_ArrayBuffer(msg), function() {});     
};

SerialConnection.prototype.onDataReceiving = function(receiveInfo) {
  if (receiveInfo.connectionId !== this.connectionId) {
    return;
  }
  this.lineBuffer += ArrayBuffer_to_String(receiveInfo.data);

  var index;
  while ((index = this.lineBuffer.indexOf('\n')) >= 0) {    
    var line = this.lineBuffer.substr(0, index + 1);        

    console.log(line);
    ///////////////////////////\\\\\\\\\\\\\\\/////////////\\\\\\\\\\////////\\\\\\\\\\\\////////\\\\\\\\\\//////PROBLEM

    this.onReadLine.dispatch(line);
///////////////////////////\\\\\\\\\\\\\\\/////////////\\\\\\\\\\////////\\\\\\\\\\\\////////\\\\\\\\\\//////PROBLEM

    this.lineBuffer = this.lineBuffer.substr(index + 1);    
  }
};

SerialConnection.prototype.onDataReceivingError = function(errorInfo) {
  if (errorInfo.connectionId === this.connectionId) {
    this.onError.dispatch(errorInfo.error);
  }
};

SerialConnection.prototype.disconnect = function() {
  if (this.connectionId < 0) {
    throw 'Invalid connection';
  }
  chrome.serial.disconnect(this.connectionId, function() {});
};

var connection = new SerialConnection();    
connection.onConnect.addListener(function() {                       
    log('connected to: ' + Arduino_COM);                            
});                                                                 

///////////////////////////\\\\\\\\\\\\\\\/////////////\\\\\\\\\\////////\\\\\\\\\\\\////////\\\\\\\\\\//////PROBLEM

connection.onReadLine.addListener(function(line) {                  
    logJSON(line);                                                  
});                                                                     
///////////////////////////\\\\\\\\\\\\\\\/////////////\\\\\\\\\\////////\\\\\\\\\\\\////////\\\\\\\\\\//////PROBLEM

connection.connect(Arduino_COM);

function logJSON(result) {
    
    var response = jQuery.parseJSON( result );
    
    dimmer1_state = response.dimmer1_state;
    dimmer1_value = response.dimmer1_value;
    SerialIn = response.SerialIn;
    dimmer1_Chrome_Input = response.dimmer1_Chrome_Input;
    temperature1_value = response.temperature1_value;

    s=Math.round(dimmer1_value * 80 / 255 + 20);
    hsl='hsl(115,'+s+'%,60%)';
    
    if (dimmer1_state == 0)
    {
        $('#statusCircle').css('fill','hsl(115,20%,60%)');
    }
    else
    {
        $('#statusCircle').css('fill', hsl);
    };

    // Print led Status to HTML buffer area
    messaggio = "dimmer1 state: " + dimmer1_state 
                + "<br />dimmer1 value: " + dimmer1_value   
                + "<br />SerialIn: " + SerialIn 
                + "<br />dimmer1_Chrome_Input: " + dimmer1_Chrome_Input 
                + "<br />temperature1_value: " + temperature1_value + " °C";
    log(messaggio);
};


function log(msg) {
    $('#buffer').html(msg);
};

$(function(){
    $('#dimmer1_Chrome_Input')  .button()
                                .mousedown(function() {
                                    connection.send("101");
                                })
                                .mouseup(function() {
                                    connection.send("100");
                                });
});
4

1 に答える 1