Queue write to SD Card Twice

Hi all,
I am a beginner in RTOS.
I want to write a file on the SD card in ESP32 with FreeRTOS.
One of the tasks is from the serial port to send data to the queue and receive the data from the queue to write the SD Card in another task.

TaskHandle_t Task1,Task2,Task3;
QueueHandle_t queue;
#define length            11
char array_b[length],array_s[length],array_sd[length-1];
int counter;

void Task1_Serial(void* pvParam)
{
	Serial.begin(115200);							        					
	Serial.setTimeout(10);
	while(1)
	{
		if(Serial.available()>0)
		{
			recData_s = Serial.readStringUntil('\n');
			recData_s += '\n';
			recData_s.toCharArray(array_s,length);

			array_s[0] = counter++;
			xQueueSend(queue,&array_s,portMAX_DELAY);

		}
		vTaskDelay(5 / portTICK_PERIOD_MS);
	}
}

void Task2_BT(void* pvParam)
{
	while(1)
	{
		vTaskDelay(5 / portTICK_PERIOD_MS);
	}
}

void Task3_SD_Card(void* pvParam)
{
	recDataFile = SD_MMC.open(binfilePath, FILE_WRITE);

	while(1)
	{
		if(xQueueReceive(queue,array_sd,portMAX_DELAY) == pdPASS)
		{
			recDataFile.print(array_sd);
			recDataFile.flush();
		}
		vTaskDelay(5 / portTICK_PERIOD_MS);
	}
}
void setup()
{
	BT.begin("BT_laptop");
  	queue = xQueueCreate( 5,length-1);
  	if(queue == NULL){
   		BT.println("Error creating the queue");
	xTaskCreatePinnedToCore(Task1_Serial,"Task1",10000,NULL,2,&Task1,tskNO_AFFINITY);
	xTaskCreatePinnedToCore(Task2_BT,"Task2",10000,NULL,2,&Task2,tskNO_AFFINITY);
	xTaskCreatePinnedToCore(Task3_SD_Card,"Task3",10000,NULL,2,&Task3,tskNO_AFFINITY);
}
void loop()
{
	
}

I continued to send 10 bytes of sample data like below

AT+FTP=0/r/n

But it seems to write the same data twice on the SD card.
I can not figure out why.
擷取

actually, this may be due to a defect SD card. Do yourself a favor and use industry quality SD cards only. My customers have wasted many many hours hunting after inexplicable behavior that at the end of the day could be attributed to poor quality consumer SD cards.

I see your point, but I use a Sandisk sd card bought over the counter.

yes, that is a consumer card. Try another one. It MAY save you a lot of time. A “brand name” is no guarantee whatsoever for quality. Query the net a little, you will be surprised how much trash is out there, even with a good name.

So you already verified that the write is done twice using e.g. a simple counter value ? Just to avoid being trapped by your current test code.
If it turns out that the write/flush is broken in your application try to track the write and flush call with a debugger to see where the problem is. And maybe post what versions (FreeRTOS + FAT-FS) you’re using. If there was a bug it might be fixed in recent versions.
Although I don’t think that a SDK was released with such an obvious problem.
Is the low-level SD-Card driver known to work properly ?

They are built-in FreeRTOS v8.2 and sd_mmc API.
I try to increase the length of array_sd and queue.
It seems to work fine.
Can anybody explain this question?

TaskHandle_t Task1,Task2,Task3;
QueueHandle_t queue;
#define length            11
char array_b[length],array_s[length],array_sd[length];
int counter;

void Task1_Serial(void* pvParam)
{
	Serial.begin(115200);							        					
	Serial.setTimeout(10);
	while(1)
	{
		if(Serial.available()>0)
		{
			recData_s = Serial.readStringUntil('\n');
			recData_s += '\n';
			recData_s.toCharArray(array_s,length);

			array_s[0] = counter++;
			xQueueSend(queue,&array_s,portMAX_DELAY);

		}
		vTaskDelay(5 / portTICK_PERIOD_MS);
	}
}

void Task2_BT(void* pvParam)
{
	while(1)
	{
		vTaskDelay(5 / portTICK_PERIOD_MS);
	}
}

void Task3_SD_Card(void* pvParam)
{
	recDataFile = SD_MMC.open(binfilePath, FILE_WRITE);

	while(1)
	{
		if(xQueueReceive(queue,array_sd,portMAX_DELAY) == pdPASS)
		{
			recDataFile.print(array_sd);
			recDataFile.flush();
		}
		vTaskDelay(5 / portTICK_PERIOD_MS);
	}
}
void setup()
{
	BT.begin("BT_laptop");
  	queue = xQueueCreate( 5,length);
  	if(queue == NULL){
   		BT.println("Error creating the queue");
	xTaskCreatePinnedToCore(Task1_Serial,"Task1",10000,NULL,2,&Task1,tskNO_AFFINITY);
	xTaskCreatePinnedToCore(Task2_BT,"Task2",10000,NULL,2,&Task2,tskNO_AFFINITY);
	xTaskCreatePinnedToCore(Task3_SD_Card,"Task3",10000,NULL,2,&Task3,tskNO_AFFINITY);
}
void loop()
{
	
}

擷取1999

Without knowing anything about the Serial- and recData_s functions I guess there was or is an issue with a 0 terminator of the strings assumed or not. I’d re-read the docs where it’s needed or automatically appended (maybe overwriting the reserved buffer).