我是编码的初学者。 请原谅我。
试图从Qt示例终端学习并发送原始的十六进制字节。
最初发送0 x 57,0 x 04,0 x02也没有问题。 我喜欢这个
语音主窗口:3360写入数据(constqbytearraydata ) )。
{
串行-写入(data;
}
void main window :3360 my function1()
{
qbytearrayba(((x57 ) x04 (x02 ) );
写数据(ba );
}
然后,随着我的进步,有必要发送0x00s的选项数据字符串。 上面的代码不发送 x00,也不删除后面的任何内容,所以#RawData&; #39; 停止特殊字符的转换。
语音主窗口:3360我的函数2 (int option ) )。
{
QByteArray hexString;
sitch(option ) (
case 1:
hex string.append (q bytearray :3360 from raw data ((x00 (x01 ) x02 ),3 );
黑;
case 2:
hex string.append (q bytearray :3360 from raw data ((xff (x00 ),2 );
黑;
}
witedata(hexstring );
}
上面的代码可以发送两个常量字符串。 但现在我面临着真正的挑战,我需要发送非固定的十六进制字节。 我创建了一个包含Hex字符串的unsigned char数组,并更改了它的值。 然后转换为const char数组。 但是,这个演员的大小不合适。
语音主窗口:3360我的函数3 (int option ) )。
{
unsignedchardiff [ ]={0x 45,0x 00,0x 01,0x 02 };
.
Diff[2]=0x08;//运行时间更改
constchar*cDiff=(constchar* ) diff; //casting
intsize_diff=sizeof(diff; //q调试shows 4
intsize_cdiff=sizeof(cdiff ); //q调试shows 8!
QByteArray hexString;
sitch(option ) (
case 1:
hex string.append (q bytearray :3360 from raw data ((x00 (x01 ) x02 ),3 );
黑;
case 2:
hex string.append (q bytearray :3360 from raw data ((xff (x00 ),2 );
黑;
case 3:
hex string=q bytearray : from raw data (cdiff,sizeof ) c diff );
黑;
}
witedata(hexstring );
}
在第三种情况下,我的MCU板总共有8个字节:0x450x00、0x08、0x02、0x01、0x00、0x00、0x00、0x00! 然后,将Diff []的大小更改为10,并再次设置为sizeof(cdiff )=8。 最后两个字节将被截断。
我的第一个问题是,有更好的方法发送不同数组大小的无符号字符吗? 取值范围为0x00到0xFF,是否在运行时使用QSerialPort确定? 第二个问题是,为什么演员给了我错误的数组大小?
谢谢您的提醒
DLIU