【基于CAPL进行TXT文件读取】—2.使用指令将读取的文件内容发送到trace
文章目录
- 1.概述
- 2.具体脚本
1.概述
上一篇文章讲到怎么从TXT文件中读取具体内容,这篇文章主要讲解读取到的内容 怎么使用通过使用诊断报文的形式发送到trace窗口中。提供一个实例仅供参考使用。
2.具体脚本
TXT内容:
/@!Encoding:936/
includes
{
}
variables
{
message 0x6F4 meg;
}
On Start
//On Start
{
int j;
char content[100]; // 定义字符数组,用于存储读取的内容(缓冲区)
dword fileHandle; // 定义文件句柄变量
long i;
long idx;
long val;
byte txtBuf[8];
byte byteCnt;
byte low;
byte high;
char tok[3];
long tokLen;
char c;
// 1. 打开文件
fileHandle = openFileRead("E:\T\11.txt", 0);
// 判断文件是否成功打开(非零表示成功)
if (fileHandle != 0)
{
// 2. 读取一行内容(使用fileGetsStringz去除换行符)
if (fileGetStringSZ(content, elcount(content), fileHandle) == 1)
{write("不保留换行符,读取到的内容为: %s", content); // 打印读取的内容/* 1. 按空格拆十六进制字符串 */
idx = 0; // 当前在 content[] 的位置
byteCnt = 0; // 已解析出的字节数
while (byteCnt < 8 && content[idx] != '\0')
{/* 跳过空格 */while (content[idx] == ' ') ++idx;/* 高 4 位 */c = content[idx];if (c == '\0') break;if (c >= '0' && c <= '9') high = c - '0';else if (c >= 'A' && c <= 'F') high = c - 'A' + 10;else if (c >= 'a' && c <= 'f') high = c - 'a' + 10;else break; // 非法字符直接结束++idx;/* 低 4 位 */c = content[idx];if (c >= '0' && c <= '9') low = c - '0';else if (c >= 'A' && c <= 'F') low = c - 'A' + 10;else if (c >= 'a' && c <= 'f') low = c - 'a' + 10;else break;++idx;txtBuf[byteCnt] = (high << 4) | low;++byteCnt;}}// 3. 关闭文件,释放资源
fileClose(fileHandle);/======================使用诊断报文进行内容发送=====================================/meg.dlc=8;meg.byte(0) = byteCnt; // 长度for (idx = 0; idx < byteCnt; ++idx)meg.byte(idx + 1) = txtBuf[idx]; // 数据for (idx = byteCnt + 1; idx < 8; ++idx)meg.byte(idx) = 0; // 补 0output(meg);}
else
{
write("文件打开失败!"); // 处理打开失败的情况
}
}
#3.运行结果