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)