Skip to content

Commit 1ad5dc6

Browse files
committed
Fix DataViewItem with 'String'-format
1 parent f221919 commit 1ad5dc6

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/core/sdk/mbcore.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,15 +635,23 @@ QByteArray toByteArray(const QVariant &value, Format format, Modbus::MemoryType
635635
break;
636636
case String:
637637
{
638-
const int cBytes = variableLength*2;
638+
const int cBytes = variableLength;
639639
QTextCodec *codec = QTextCodec::codecForName(stringEncoding);
640640
QString s = fromEscapeSequence(value.toString());
641641
if (stringLengthType == ZerroEnded)
642-
s = s.section(QChar('\0'), 0);
642+
{
643+
int index = s.indexOf(QChar('\0'));
644+
if (index < 0)
645+
s.append(QChar('\0'));
646+
else
647+
s = s.left(index+1);
648+
}
643649
data = codec->fromUnicode(s);
644-
if (data.length() > cBytes)
650+
if ((stringLengthType == FullLength) && data.length() < cBytes)
651+
data.append(cBytes-data.length(), '\0');
652+
else if (data.length() > cBytes)
645653
data = data.left(cBytes);
646-
if ((data.length() & 1) && (data.length() < cBytes)) // data len is odd number
654+
if (data.length() & 1) // data len is odd number
647655
data.append(1, '\0');
648656
}
649657
break;

0 commit comments

Comments
 (0)