Esempio n. 1
0
void __fastcall TForm1::Button2Click(TObject *Sender)
{
	if(this->OpenDialog1->Execute())
        {
        	AnsiString FileName = OpenDialog1->FileName ;
                int File =   FileOpen(FileName,fmOpenRead);
                //获取文件内容真正的大小
		int length =  GetCompressedFileSize(FileName.c_str(),NULL);
                byte *buf = (byte *)malloc(length);
                char *content = (char *)malloc(length);
                //read file to buf
                FileRead(File,buf,length);
                FileSeek(File,0,0);
                FileRead(File,content,length);
                FileClose(File);
                this->MemoLeftData->Text = AnsiString(content);
//store global data
                embeddata = BinaryToString(buf,length);
                embedSize = length * 8 ;
                free(content);
                free(buf);

               

        }
}
/* processes character read from serial port of gsm module */
void SimcomResponseParser::FeedChar(char c)
{	
	if (_state != ParserState::WaitingForEcho)
	{
		if (_currentCommand == AtCommand::CipSend)
		{
			if (_parserContext.CipsendState == CipsendStateType::WaitingForPrompt)
			{
				if (_promptSequenceDetector.NextChar(c))
				{
					_parserContext.CipsendState = CipsendStateType::WaitingForDataEcho;
					_response.clear();
					_logger.Log(F("Writing %d b of data"), _parserContext.CipsendDataLength);

					auto dataPtr = _parserContext.CipsendBuffer->c_str() + _parserContext.CipsendDataIndex;
					auto dataLength = _parserContext.CipsendDataLength;

					_serial.write(dataPtr, dataLength);
					_parserContext.CipsendDataEchoDetector.SetSequence(dataPtr, dataLength);
					return;
				}
			}
			if (_parserContext.CipsendState == CipsendStateType::WaitingForDataEcho)
			{
				if (_parserContext.CipsendDataEchoDetector.NextChar(c))
				{
					_parserContext.CipsendState = CipsendStateType::WaitingForDataAccept;
				}
				return;
			}
		}
	}
	if (_parserContext.CiprxGetLeftBytesToRead > 0)
	{
		_parserContext.CipRxGetBuffer->append(c);
		_parserContext.CiprxGetLeftBytesToRead--;
		return;
	}	
	int prevState = lineParserState;

	lineParserState = StateTransition(c);

	if(prevState  == PARSER_INITIAL || prevState == PARSER_LF || prevState == PARSER_LINE)
	{		
		if (lineParserState == PARSER_LINE)
		{
			if (_response.freeBytes() > 0)
			{
				_response.append(c);
			}
		}
	}
	// line -> delimiter
	if ((prevState == PARSER_LINE || prevState == PARSER_CR) && (lineParserState == PARSER_LF))
	{
		if (_response.length() == 0)
		{
			return;
		}

		_logger.LogAt(F("    <= %s"), (char*)_response.c_str());

		auto isUnsolicited = ParseUnsolicited(_response);

		if (isUnsolicited)
		{
			_response.clear();
			return;
		}
		ParserState parseResult = ParseLine();

		// if error or or success
		if (parseResult == ParserState::Success || parseResult == ParserState::Error)
		{
			commandReady = true;
			_state = parseResult;
		}
		
		// if command not parsed yet
		else if (parseResult == ParserState::None)
		{
			if (ParsingHelpers::CheckIfLineContainsGarbage(_response))
			{
				if (IsGarbageDetectionActive)
				{
					_garbageOnSerialDetected = true;
					_logger.Log(F(" Garbage detected(%d b): "), _response.length());
				}

			}
			else
			{
				_logger.Log(F( "Unknown response (%d b): "), _response.length());
			}

			FixedString200 printableLine;
			BinaryToString(_response, printableLine);
			_logger.Log(F(" '%s'"), printableLine.c_str());
			// do nothing, do not change _state to none
		}
		else
		{
			_state = parseResult;
		}
		
		_response.clear();
	}

}