Arduino Mega 2560示波器PC端代码fprintf写入文件异常排查
Arduino示波器PC端数据写入文件异常问题分析
我使用Arduino Mega 2560开发小型示波器,PC端接收采样数据的处理函数中,终端显示数据正常,但写入文本文件时却变成从1开始的递增大数。以下是出现问题的代码:
void receive_data(int fd, const char *filename, int num_channels, int mode) { FILE *file = fopen(filename, "w"); if (!file) { perror("Errore nell'apertura del file"); return; } uint8_t buffer[MAX_BUFFER_SIZE]; int bytes_per_sample = num_channels * 3; // 1 byte di sincronizzazione + 2 bytes per canale int buffer_offset = 0; time_t start_time = time(NULL); //unsigned int sample_count = 0; // Contatore dei campioni printf("Campionamento in corso per 10 secondi...\n"); while (time(NULL) - start_time < 10) { int bytes_read = read(fd, buffer + buffer_offset, sizeof(buffer) - buffer_offset); if (bytes_read > 0) { bytes_read += buffer_offset; int i = 0; while (i <= bytes_read - bytes_per_sample) { // For each channel, look for a sync byte int sync_found = 1; for (int ch = 0; ch < num_channels; ch++) { if (buffer[i++] != 0xAA) { sync_found = 0; break; } // Read high and low bytes for the channel if (bytes_read - i < 2) { // Not enough data, handle buffer offset break; } uint8_t high_byte = buffer[i++]; uint8_t low_byte = buffer[i++]; uint16_t value = ((uint16_t)high_byte << 8) | low_byte; printf("%u ", value); fprintf(file, "%u", value); if (ch < num_channels - 1) { fprintf(file, " "); } } if (!sync_found) { // Realign buffer if sync byte not found while (i < bytes_read && buffer[i] != 0xAA) { i++; } buffer_offset = bytes_read - i; memmove(buffer, &buffer[i], buffer_offset); break; } printf("\n"); fprintf(file, "\n"); fflush(file); } // Gestisci eventuali bytes rimasti nel buffer if (i < bytes_read) { buffer_offset = bytes_read - i; memmove(buffer, &buffer[i], buffer_offset); } else { buffer_offset = 0; } } else if (bytes_read == 0) { usleep(1000); // Attendi 1ms } else { perror("Errore nella lettura dalla porta seriale"); break; } } fclose(file); }
问题根源
代码边解析边写入文件的逻辑存在漏洞:
- 当某个通道的同步字节不是
0xAA时,sync_found被置为0并跳出循环,但此时前面已经有部分通道的解析值被写入了文件——这些值是基于错误的字节位置解析出来的垃圾数据,表现为无意义的递增大数。 - 当剩余字节不足2个时直接
break,同样会留下已写入的部分错误数据,没有回退或清理。
修复方案
先完成完整的同步校验和数据解析,确认无误后再统一执行打印和写入操作,避免错误数据流入文件。修改后的代码如下:
void receive_data(int fd, const char *filename, int num_channels, int mode) { FILE *file = fopen(filename, "w"); if (!file) { perror("Errore nell'apertura del file"); return; } uint8_t buffer[MAX_BUFFER_SIZE]; int bytes_per_sample = num_channels * 3; // 1 byte di sincronizzazione + 2 bytes per canale int buffer_offset = 0; time_t start_time = time(NULL); printf("Campionamento in corso per 10 secondi...\n"); while (time(NULL) - start_time < 10) { int bytes_read = read(fd, buffer + buffer_offset, sizeof(buffer) - buffer_offset); if (bytes_read > 0) { bytes_read += buffer_offset; int i = 0; while (i <= bytes_read - bytes_per_sample) { int sync_found = 1; uint16_t values[num_channels]; // 临时存储一组完整的采样值 // 先完成所有通道的同步校验与数据解析 for (int ch = 0; ch < num_channels; ch++) { if (buffer[i] != 0xAA) { sync_found = 0; break; } i++; // 跳过同步字节 if (bytes_read - i < 2) { sync_found = 0; break; } uint8_t high_byte = buffer[i++]; uint8_t low_byte = buffer[i++]; values[ch] = ((uint16_t)high_byte << 8) | low_byte; } if (!sync_found) { // 重新定位同步字节,放弃当前不完整的解析内容 while (i < bytes_read && buffer[i] != 0xAA) { i++; } buffer_offset = bytes_read - i; memmove(buffer, &buffer[i], buffer_offset); break; } // 校验成功后,统一输出到终端和文件 for (int ch = 0; ch < num_channels; ch++) { printf("%u ", values[ch]); fprintf(file, "%u", values[ch]); if (ch < num_channels - 1) { fprintf(file, " "); } } printf("\n"); fprintf(file, "\n"); fflush(file); } // 处理缓冲区剩余的未解析字节 if (i < bytes_read) { buffer_offset = bytes_read - i; memmove(buffer, &buffer[i], buffer_offset); } else { buffer_offset = 0; } } else if (bytes_read == 0) { usleep(1000); // Attendi 1ms } else { perror("Errore nella lettura dalla porta seriale"); break; } } fclose(file); }
关键改动
- 新增
uint16_t values[num_channels]临时数组,先存储所有通道的解析结果,确保同步完全成功后再输出。 - 将打印和写入逻辑移到同步校验成功之后,彻底避免错误数据写入文件。
- 优化了解析过程中的判断逻辑,确保只有完整、合法的采样组才会被输出。
内容的提问来源于stack exchange,提问作者Federico Frontera
相关产品推荐
相关产品推荐

