| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- #include "rtuhelper.h"
- #ifdef _MSC_VER
- #pragma execution_character_set("utf-8")
- #endif
- namespace UtilsFun {
- RTUHelper::RTUHelper():
- readType(SubAddress),toReadlen(0),_checkSubAddress(false)
- {
- }
- RTUHelper::~RTUHelper(){}
- void RTUHelper::readed(const QByteArray &data)
- {
- int index = 0;
- while (index < data.length())
- {
- const char ch = data.at(index);
- // qDebug()<<"ch"<<ch;
- // qDebug()<<data.at(index);
- switch (readType)
- {
- case SubAddress:
- {
- ++ index;
- if (!_checkSubAddress || _subAddress.contains(ch))
- {
- if(!buffer.isEmpty()) buffer.clear();
- buffer.append(ch);
- readType = GetType;
- }
- continue;
- }
- case GetType:
- {
- buffer.append(ch);
- ++ index;
- readType = NumLength;
- continue;
- }
- case NumLength: //需要读数据
- {
- ++ index;
- toReadlen = static_cast<uchar>(ch);
- buffer.append(ch);
- readType = ReadData;
- continue;
- }
- case ReadData: //需要读数据
- {
- const int canread = data.length() - index;
- if (canread >= toReadlen)
- {
- buffer.append(data.mid(index, toReadlen));
- index += toReadlen;
- toReadlen = 0;
- readType = CRCUL;
- }
- else
- {
- buffer.append(data.mid(index, canread));
- index += canread;
- toReadlen -= canread;
- }
- continue;
- }
- case CRCUL: //需要读数据
- {
- buffer.append(ch);
- index ++;
- readType = CRCUH;
- continue;
- }
- case CRCUH: //需要读数据
- {
- buffer.append(ch);
- index ++;
- readType = SubAddress;
- doHandle(this->buffer);
- buffer.clear();
- }
- break;
- default:
- {
- readType = SubAddress;
- continue;
- }
- }
- }
- }
- }
|