Q&A

  • 감사님 무슨 말씀인지 이해가...~~~~
저는 delphi에서 시리얼 통신 초보입니다.

소스를 올려 보겠습니다.

물론 읽은 데아타는 다 들어오는데 읽은 건수보다 많다는게 문제 입니다.

component => AsyncFree입니다.

=====DataReceived==============================================================

부분입니다. 보십시요.

procedure TForm1.AfComPort1DataRecived(Sender: TObject; Count: Integer);

Var

SendBody : Array[0..1024] of Char;

Offset : Integer;



Label EndFrame;



Label TryAgain;

begin



If bStxStarted = True Then

Begin

StrPLCopy(@RecvData[StrLen(RecvData)], AfComPort1.ReadString, Count);

If (RecvData[StrLen(RecvData)-1] = _ETX) Then

Begin

Memo1.Lines.Add('HANDSHAKE ETX를받았습니다');

Sleep(100);

goto EndFrame;

End

Else

Begin

Goto TryAgain;

End;



End

Else If bStxStarted = False Then

Begin

i := i+1;

FillChar(RecvData, SizeOf(RecvData), 0);

StrPLCopy(@RecvData[0], AfComPort1.ReadString, Count);



If RecvData[0] = _STX Then

Begin

bStxStarted := True;

goto TryAgain;

End;

End;





EndFrame:



bStxStarted := False;

offset := 0;



If(RecvData[1] = _EOT)then

Begin

FillChar(SendBody, Sizeof(SendBody), 0);

Offset := 0;

SendBody[offset] := _STX;

Inc(offset, 1);

SendBody[offset] := _ACK;

Inc(offset, 1);

SendBody[offset] := _ETX;

Inc(offset, 1);

AfComPort1.WriteString(SendBody);

Exit;

End;



If (REcvData[1] = _ENQ)then

Begin

FillChar(sendBody, sizeof(sendBody), 0);

Offset := 0;

SendBody[offset] := _STX;

Inc(offset, 1);

SendBody[offset] := _ACK;

Inc(offset, 1);

SendBody[offset] := _ETX;

Inc(offset, 1);

AfComPort1.WriteString(SendBody);

exit;

end;





FillChar(sendBody, sizeof(sendBody), 0);

offset := 0;

SendBody[offset] := _STX;

Inc(offset, 1);

SendBody[offset] := _ACK;

Inc(offset, 1);

SendBody[offset] := _ETX;

Inc(offset, 1);

AfComPort1.WriteString(SendBody);

Memo1.Lines.Add(Format('[%d]',[i]));





Memo1.Lines.Add(RecvData);

FillChar(RecvData, sizeof(RecvData), 0);





TryAgain:



end;

===========================================================================

감사합니다. 저는 이걸 꼭 해결해야 하거든요. 부탁드립니다.





0  COMMENTS