我正在尝试通过 UART 从 Matlab 将数据从包含十六进制字符的文件复制到嵌入式设备的 SRAM 内存。问题是我不知道如何使程序停止将任何收到的字符视为特殊命令。
例如:根据 ASCII 表,符号 '' 的十六进制相当于 0x20。然而,我的数据可能在某个地方有这个 0x20。因此,我不能使用它作为我的程序的分隔符。
请建议我一种方法,可以读取十六进制文件中的所有数据,而不会导致任何此类问题。
这是我的代码的一部分。
memcpy(((uint8_t*)(SRAM_BASE+i)),&cThisChar,1);
UARTCharPut(UART0_BASE, cThisChar);
i++;
//
// Stay in the loop until either a CR or LF is received.
//
}
while((cThisChar != '\n') && (cThisChar != '\r')); // this is where the problem happens!
那么我应该为 while 循环设置什么条件才能接受所有字符?
谢谢!
最佳答案
不要尝试通过文件内容获取文件结尾。 获取其大小并使用计数器代替。
FILE *fp = NULL;
long int fsize = 0;
long int fptr = 0;
/* Open file. */
/* todo: Open file here. */
/* Get file size. */
fseek(fp, 0L, SEEK_END);
fsize = ftell(fp);
fseek(fp, 0L, SEEK_SET);
/* Process file data. */
while (fptr < fsize) {
/* todo: Do your stuff here. */
++fptr;
}
/* Close file. */
/* todo: Close file here. */
关于c - 使用 fread 从 Matlab 读取所有字符到嵌入式设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24289184/