源代码如下:
串口通信 获取串口数据后转为qbytearray类型
在程序运行时报段错误
-
unsigned char ucbuff[256]; //temp QByteArray qbbuff; pAmmeterSerialPort->Tcflush(); ilen = pAmmeterSerialPort->Read(ucbuff,MaxSerialRecvBuffLen,SerialRecvBuffTImeOut_ms); qbbuff = QByteArray::fromRawData((const char*)ucbuff,ilen); if(ilen > 0 ) { emit sigSendDataFromSerial(qbbuff, false); }
查看qt帮助文档 fromRawData的注意事项
Warning: A byte array created with fromRawData() is not null-terminated, unless the raw data contains a 0 character at position size. While that does not matter for QDataStream or functions like indexOf(), passing the byte array to a function accepting a const char * expected to be '\0'-terminated will fail.
警告:使用fromRawData()创建的字节数组不能以null结尾,除非原始数据包