Skip to content

Getting different result based on where I dequeue #1

@nirajbhatt7

Description

@nirajbhatt7

Depending on where I dequeue, I am getting different result. Can you please help.
To test the following code, uncomment TestDequeue() in setup first and check the result which is as expected. Then comment out TestDequeue() in setup and uncomment the same function in the loop() where I see different result.

#include <QueueArray.h>

// create a queue of characters.
QueueArray <char *> queue;

typedef struct {
  char magicID;
  uint8_t deviceID;
  uint8_t commandID;
  uint8_t sensorType;
} protocolHeader;


void setup() {
  // put your setup code here, to run once:
   // start serial communication.
  Serial.begin (115200);

  // set the printer of the queue.
  queue.setPrinter (Serial);

  protocolHeader pRequest;
  pRequest.magicID = 'N';
  pRequest.deviceID = 132;
  pRequest.commandID = 5;
  pRequest.sensorType = 22;

  char b[sizeof(pRequest)+1];
  memcpy(b, &pRequest, sizeof(pRequest)+1);

  queue.enqueue(b);

  //TestDequeue(); 
}

void loop() {
  // put your main code here, to run repeatedly:
  if(!queue.isEmpty()){
    //TestDequeue();    
  }
}

void TestDequeue(){
    Serial.println("Count: " + (String)queue.count());

    protocolHeader p2; 
    char * bufferCopy = queue.dequeue();
    memcpy(&p2, bufferCopy, sizeof(p2));
    Serial.println(p2.magicID);
    Serial.println(p2.deviceID);
    Serial.println(p2.commandID);
    Serial.println(p2.sensorType);

    Serial.println("Count: " + (String)queue.count());
    Serial.println("////////////////////");

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions