Seial communication UART sensor not response

Hello,

I'm working on a serial communication project with sensor cairsens. My code is based on the datasheet (in attachment page 9-10-11). But I didn't get a result from the sensor, I got Zero as response frame.

Here what I got in Serial Monitor:

the Request frames : 0xFF, 0x02, 0x13, 0x30, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x12, 0xAF, 0x88, 0x03,
TIMEOUT
the Response frames : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0

Firstly, I wrote the Request Frame in the array (requestvalFrame) and by SerialNPM.write I sent the request then I read the response by SerialNPM.readBytes (in array responseArray)when I will get the response of the sensor but I got Zero. (Using SerialNPM or Serial1 gives the same result)
Did I miss something? If you can help me I will appreciate that.

Thank you in advance.

My code:

#include <Arduino.h>
#include "wiring_private.h"

Uart SerialNPM (&sercom3, 0, 1, SERCOM_RX_PAD_1, UART_TX_PAD_0); //https://www.arduino.cc/en/Tutorial/SamdSercom

/** Length of LG field */
static const uint8_t LG_LENGTH = 0x01;
/** Length of Measure field */
static const uint8_t MEAS_LENGTH = 0x01;
/** Length of END field */
static const uint8_t END_LENGTH = 0x02;
/** Generic address allowing to communicate with any product without knowing its references */
static const uint8_t REF_LENGTH = 0x08;
//  static const uint8_t ANY_REF[];
static const uint8_t CRC_LENGTH = 0x02;
static const uint8_t CMD_TYPE_LENGTH = 0x01;
//  static const uint8_t GET_INSTANT_VAL_RSP[];

typedef enum
{
  GET_INSTANT_VAL = 0x12,
} ECmd;



const uint8_t SYNC[] = {0xFF};
/** Start Frame */
const uint8_t STX[]  = {0x02};
/** End Frame */
const uint8_t ETX[]  = {0x03};
/** Header UART end bytes */
const uint8_t HEADER_UART_END_BYTES[] = {0x30, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
/** Generic address allowing to communicate with any product without knowing its references */
const uint8_t ANY_REF[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
const uint8_t HEADER_UART_LENGTH = sizeof(SYNC) + sizeof(STX) + LG_LENGTH + sizeof(HEADER_UART_END_BYTES);
const uint8_t GET_INSTANT_VAL_RSP[] = {0x13};

const byte MaxByteArraySizeResponse = 25;
byte responseArray[MaxByteArraySizeResponse] = {};


void getInstantValue()
{

  uint8_t requestvalFrame[HEADER_UART_LENGTH + sizeof(ANY_REF) + sizeof(GET_INSTANT_VAL_RSP) + MEAS_LENGTH + END_LENGTH + CRC_LENGTH + sizeof(ETX)];
  uint8_t loc_u8_id = 0;
  static const uint8_t GET_INST_VAL_TIMEOUT = 30;
  uint8_t arg_u8_instVal;

  memcpy(&requestvalFrame[loc_u8_id], SYNC, sizeof(SYNC));
  loc_u8_id += sizeof(SYNC);
  memcpy(&requestvalFrame[loc_u8_id], STX, sizeof(STX));
  loc_u8_id += sizeof(STX);
  /** Length */
  requestvalFrame[loc_u8_id++] = 0x13;
  memcpy(&requestvalFrame[loc_u8_id], HEADER_UART_END_BYTES, sizeof(HEADER_UART_END_BYTES));
  loc_u8_id += sizeof(HEADER_UART_END_BYTES);
  memcpy(&requestvalFrame[loc_u8_id], ANY_REF, sizeof(ANY_REF));
  loc_u8_id += sizeof(ANY_REF);
  /** GetInstantVal cmd */
  requestvalFrame[loc_u8_id++] = GET_INSTANT_VAL;

  /** TODO CRC */
  requestvalFrame[loc_u8_id++] = 0xAF;
  requestvalFrame[loc_u8_id++] = 0x88;


  memcpy(&requestvalFrame[loc_u8_id], ETX, sizeof(ETX));
  loc_u8_id += sizeof(ETX);


  Serial.print("the Request frames : ");

 for (int i = 0; i < HEADER_UART_LENGTH + sizeof(ANY_REF) + CMD_TYPE_LENGTH + CRC_LENGTH + sizeof(ETX); i++)
  {
    Serial.print("0x");
    if (requestvalFrame[i] < 0x10)
      Serial.print("0");
    Serial.print(requestvalFrame[i], HEX);
    Serial.print(", ");
  }
  Serial.println();

  
  /** Write GetValueFrame buffer to UART */

  SerialNPM.write(requestvalFrame, HEADER_UART_LENGTH + sizeof(ANY_REF) + CMD_TYPE_LENGTH + CRC_LENGTH + sizeof(ETX)); //*************************

//  /** get response */

SerialNPM.setTimeout(30);   

  loc_u8_id = SerialNPM.readBytes(responseArray, sizeof(MaxByteArraySizeResponse)); //*************************************************

  if (loc_u8_id == 0)
  {
    Serial.println("TIMEOUT");
  }
  Serial.print("the Response frames : ");

 for (int i = 0; i < sizeof(responseArray); i++)
  {
    Serial.print("0x");
    if (responseArray[i] < 0x10)
      Serial.print("0");
    Serial.print(responseArray[i], HEX);
    Serial.print(", ");
  }

}



void setup() {
  Serial.begin(9600);

  ////////////////////////////////////////////////////////////////////////////////////////////////////
  SerialNPM.begin(9600, SERIAL_8E1);
  pinPeripheral(0, PIO_SERCOM); //Assign RX function to pin 0
  pinPeripheral(1, PIO_SERCOM);
  while (!Serial);
  Serial.println("Concentrations readings is");

}


void loop() { 
    getInstantValue();
  delay (3000);
}
void SERCOM3_Handler()
{
  SerialNPM.IrqHandler();
}

CairClipUart-Interface-Protocole-downloadDataOnly.211116.pdf (1.08 MB)

Since you never told us, we always have to assume an Arduino UNO. That device uses pins 0 and 1 as part of the USB connection to your PC, and is where your Serial.print() is sent.

Paul

Hello Paul, thank you for your reply and I'm sorry for the late I was trying with another code.
I'm sorry also because I forgot to mention my carte Arduino. I'm using Arduino MKRWiFi 1010 so there is no problem with the pins 0 and 1, I created a serial communication with SamdSercom.

I found this library for the same sensor I'm using on Github: https://github.com/Lahorde/airbeam/blob/master/firmware/arduino/libraries/cairsens/cairsens.cpphttps://github.com/Lahorde/airbeam/blob/master/firmware/arduino/libraries/cairsens/cairsens.cpp
but I'm trying to write the code in Sketch instead using the library because I failed with the library.

here the library's .cpp:

#include "cairsens.h"

#include <Arduino.h>
#include <logger_config.h>
/**************************************************************************
 * Manifest Constants
 **************************************************************************/
const uint8_t CairsensUART::SYNC[] = {0xFF};
/** Start Frame */  
const uint8_t CairsensUART::STX[]  = {0x02};
/** End Frame */  
const uint8_t CairsensUART::ETX[]  = {0x03};
/** Header UART end bytes */  
const uint8_t CairsensUART::HEADER_UART_END_BYTES[] = {0x30, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
/** Generic address allowing to communicate with any product without knowing its references */
const uint8_t CairsensUART::ANY_REF[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 
const uint8_t CairsensUART::HEADER_UART_LENGTH = sizeof(CairsensUART::SYNC) + sizeof(CairsensUART::STX) + CairsensUART::LG_LENGTH + sizeof(CairsensUART::HEADER_UART_END_BYTES); 
const uint8_t CairsensUART::GET_INSTANT_VAL_RSP[] = {0x13}; 
/**************************************************************************
 * Type Definitions
 **************************************************************************/

/**************************************************************************
 * Public Methods Definitions
 **************************************************************************/
CairsensUART::CairsensUART(Stream* arg_p_uartStream):
  _p_uartStream(arg_p_uartStream)
{
   
} 

CairsensUART::EError CairsensUART::getInstantValue(uint8_t& arg_u8_instVal)
{
  /** same buffer used for Request and response, give it response length (> request length) */ 
  uint8_t loc_au8_getInstValFrame[HEADER_UART_LENGTH + sizeof(ANY_REF) + sizeof(GET_INSTANT_VAL_RSP) + MEAS_LENGTH + END_LENGTH + CRC_LENGTH + sizeof(ETX)]; 
  uint8_t loc_u8_id = 0; 
  static const uint8_t GET_INST_VAL_TIMEOUT = 30; 

  memcpy(&loc_au8_getInstValFrame[loc_u8_id], SYNC, sizeof(SYNC));
  loc_u8_id += sizeof(SYNC);
  memcpy(&loc_au8_getInstValFrame[loc_u8_id], STX, sizeof(STX));
  loc_u8_id += sizeof(STX);
  /** Length */ 
  loc_au8_getInstValFrame[loc_u8_id++] = 0x13;
  memcpy(&loc_au8_getInstValFrame[loc_u8_id], HEADER_UART_END_BYTES, sizeof(HEADER_UART_END_BYTES));
  loc_u8_id += sizeof(HEADER_UART_END_BYTES);
  memcpy(&loc_au8_getInstValFrame[loc_u8_id], ANY_REF, sizeof(ANY_REF));
  loc_u8_id += sizeof(ANY_REF);
  /** GetInstantVal cmd */ 
  loc_au8_getInstValFrame[loc_u8_id++] = GET_INSTANT_VAL;
  
  /** TODO CRC */ 
  loc_au8_getInstValFrame[loc_u8_id++] = 0xAF;
  loc_au8_getInstValFrame[loc_u8_id++] = 0x88;
  
  
  memcpy(&loc_au8_getInstValFrame[loc_u8_id], ETX, sizeof(ETX));
  loc_u8_id += sizeof(ETX);
  
  /** Write GetValueFrame buffer to UART */
  LOG_DEBUG("Send frame : ");
  for(loc_u8_id = 0; loc_u8_id < HEADER_UART_LENGTH + sizeof(ANY_REF) + CMD_TYPE_LENGTH + CRC_LENGTH + sizeof(ETX); loc_u8_id++)
  {
    LOG_DEBUG("%x ", loc_au8_getInstValFrame[loc_u8_id]);
  }
  LOG_DEBUG_LN("");
  _p_uartStream->write(loc_au8_getInstValFrame, HEADER_UART_LENGTH + sizeof(ANY_REF) + CMD_TYPE_LENGTH + CRC_LENGTH + sizeof(ETX));
  
  /** get response */ 
  _p_uartStream->setTimeout(GET_INST_VAL_TIMEOUT);
   
  loc_u8_id = _p_uartStream->readBytes(loc_au8_getInstValFrame, sizeof(loc_au8_getInstValFrame)); 

  if(loc_u8_id == 0)
  {
    LOG_ERROR("Timeout when getting GetInstantValue RESP");
    return TIMEOUT;
  }
  LOG_DEBUG("received frame : ");
  for(loc_u8_id = 0; loc_u8_id < sizeof(loc_au8_getInstValFrame); loc_u8_id++)
  {
    LOG_DEBUG("%x ", loc_au8_getInstValFrame[loc_u8_id]);
  }
  LOG_DEBUG_LN("");
  
  /** TODO check RSP */
  /** TODO check CRC */
  /** TODO check Life */
  
  arg_u8_instVal = loc_au8_getInstValFrame[HEADER_UART_LENGTH + sizeof(ANY_REF) + sizeof(GET_INSTANT_VAL_RSP)];
  return NO_ERROR; 
} 
 /**************************************************************************
 * Private Methods Definitions
 **************************************************************************/