【发布时间】:2017-02-22 07:28:25
【问题描述】:
我有 ReadBinFile () //从bin文件中读取2048字节 {
transferlength = fileGetBinaryBlock(buffer, 2048 , fileHandle); }
现在我想读取 .hex 和 .mhx 扩展文件我找不到内置函数在 capl 脚本中执行此操作的选项是什么。
【问题讨论】:
标签: capl
我有 ReadBinFile () //从bin文件中读取2048字节 {
transferlength = fileGetBinaryBlock(buffer, 2048 , fileHandle); }
现在我想读取 .hex 和 .mhx 扩展文件我找不到内置函数在 capl 脚本中执行此操作的选项是什么。
【问题讨论】:
标签: capl
dword openFileRead(char filename[], dword mode);
此函数打开名为 filename 的文件以进行读取访问。
如果 mode=0 文件以 ASCII 模式打开; 如果 mode=1 则以二进制模式打开文件。
To open hexfile
dword HexFileHandle;
HexFileHandle = openFileRead(HEX_File_Path, 0);
返回值是读操作必须使用的文件句柄。
如果发生错误,返回值为0。
【讨论】:
*.hex 或 *.mhx 格式的文件可以在 OpenFileRead 函数的帮助下读取。 下面的代码将有助于读取 .hex 文件。
void readhexfile(void)
{
dword i;
char LineBuffer[0xFF];
int ByteCount;
char CountAscii[5];
int Data;
char Ascii[5];
dword readaccess = 0;
dword bufferpointer = 0;
byte buffer[1*1024*1024]; //1MB hex file size
if ((-1) != strstr_regex(FILENAME, ".hex")) //FILENAME-> Sysvariable
{
readaccess = OpenFileRead (FILENAME,0);
/* --> identified as IntelHEX-Input */
if (readaccess != 0)
{
/* read line until cr+lf */
while (fileGetString(LineBuffer, elcount(LineBuffer), readaccess) != 0)
{
// check for Record Type 00 (Data Record)
if (LineBuffer[0] == ':'
&& LineBuffer[7] == '0'
&& LineBuffer[8] == '0'
)
{
// extract ByteCount parameter
strncpy(CountAscii, "0x", elcount(CountAscii));
substr_cpy_off(CountAscii, 2, LineBuffer, 1, 2, elcount(CountAscii));
ByteCount = atol(CountAscii);
// extract Data parameter
for (i = 0; i < ByteCount; i++)
{
strncpy(Ascii, "0x", elcount(Ascii));
substr_cpy_off(Ascii, 2, LineBuffer, 9+i*2, 2, elcount(Ascii));
Data = atol(Ascii);
buffer[bufferpointer++] = Data;
};
}
}
fileClose(readaccess);
}
}
}
类似的方法可以用于读取.mhx文件格式。
【讨论】: