Sensor::readMessage()



  • When I ported sensor.cpp to C# i notice:

    boolean Sensor::readMessage() {
    uint8_t len = RF24::getDynamicPayloadSize();
    RF24::read(&msg, len);

    if (!(msg.header.messageType==M_INTERNAL && msg.header.type == I_PING_ACK)) {
    	delay(ACK_SEND_DELAY); // Small delay here to let other side switch to reading mode
    	RF24::stopListening();
    	RF24::openWritingPipe(TO_ADDR(msg.header.last));
    	RF24::write(&radioId, sizeof(uint8_t));
    	RF24::closeReadingPipe(WRITE_PIPE); // Stop listening to write-pipe after transmit
    	RF24::startListening();
    	debug(PSTR("Sent ack msg to %d\n"), msg.header.last);
    }
    uint8_t valid = validate(len-sizeof(header_s));
    boolean ok = valid == VALIDATE_OK;
    
    // Make sure string gets terminated ok for full sized messages.
    msg.data[len - sizeof(header_s) ] = '\0';
    debug(PSTR("Rx: fr=%d,to=%d,la=%d,ci=%d,mt=%d,t=%d,cr=%d(%s): %s\n"),
    		msg.header.from,msg.header.to, msg.header.last, msg.header.childId, msg.header.messageType, msg.header.type, msg.header.crc, valid==0?"ok":valid==1?"ec":"ev", msg.data);
    return ok;
    

    }

    Is this correct? Isn't better to first validante message and then send confirmation if it is ok. Like this:

    boolean Sensor::readMessage() {
    uint8_t len = RF24::getDynamicPayloadSize();
    RF24::read(&msg, len);

    uint8_t valid = validate(len-sizeof(header_s));
    boolean ok = valid == VALIDATE_OK;**
    **
    if (ok && !(msg.header.messageType==M_INTERNAL && msg.header.type == I_PING_ACK)) {
    delay(ACK_SEND_DELAY); // Small delay here to let other side switch to reading mode
    RF24::stopListening();
    RF24::openWritingPipe(TO_ADDR(msg.header.last));
    RF24::write(&radioId, sizeof(uint8_t));
    RF24::closeReadingPipe(WRITE_PIPE); // Stop listening to write-pipe after transmit
    RF24::startListening();
    debug(PSTR("Sent ack msg to %d\n"), msg.header.last);
    }

    // Make sure string gets terminated ok for full sized messages.
    msg.data[len - sizeof(header_s) ] = '\0';
    debug(PSTR("Rx: fr=%d,to=%d,la=%d,ci=%d,mt=%d,t=%d,cr=%d(%s): %s\n"),
    		msg.header.from,msg.header.to, msg.header.last, msg.header.childId, msg.header.messageType, msg.header.type, msg.header.crc, valid==0?"ok":valid==1?"ec":"ev", msg.data);
    return ok;
    

    }


  • Admin

    Yes, you're right!

    I will change this.



  • Also in RelayActuator.ino

    void setRelayStatus(message_s message) {
    if (message.header.messageType=M_SET_VARIABLE &&
    message.header.type==V_LIGHT) {

    is better:

    void setRelayStatus(message_s message) {
    if ((message.header.messageType==M_SET_VARIABLE || message.header.messageType==M_ACK_VARIABLE ) &&
    message.header.type==V_LIGHT) {

    Be careful message.header.messageType=M_SET_VARIABLE and message.header.messageType==M_SET_VARIABLE is not the same.


Log in to reply
 

Suggested Topics

  • 33
  • 5
  • 5
  • 6
  • 8
  • 2

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts