文章目录
- 生成数据帧
- 参考
生成数据帧
代码如下,代码很简单,有几点要注意,
- 较高版本的MATLAB中支持0x的写法
- 使用bitand进行位运算
- 使用strcat函数进行字符串拼接时,如果需要插入空格,要使用双引号
cmd_ay(1) = 0x33;
cmd_ay(2) = 0x44;
cmd_ay(3) = 0x08;
cmd_ay(4) = 0x33;
cmd_ay(5) = 0xB3;
cmd_ay(6) = 0x02;
cmd_ay(7) = 0x00;
cmd_ay(8) = 0x01;
cmd_ay(9) = 0x02;
cmd_ay(10) = 0x03;
cmd_ay(11) = 0x04;
chk_sum = uint16(sum(cmd_ay(3:11),1)); % 计算校验和
% cmd_ay(12) = mod(chk_sum,2^8);
cmd_ay(12) = bitand(chk_sum,uint16(255));str_line = '';
for m=1:12str_line = strcat(str_line,dec2hex(cmd_ay(m),2)," ");
end
display(str_line);