|
컴도 한 대고, Serial cable도 어디있는지 찾을 수도 없고 (있기나 한건가 ? -_-;;)..
해서 비스므리하게 만들어 봤습니다.
사용된 Component들은
Edit1 : TEdit
Button1 : TButton
Memo1 : TMemo
이며 Event 처리부는
TForm1::Edit1KeyPress
TForm1::Button1Click
입니다.
행복하세요.
피 에쓰. 테스트는 "123@4567*89..." 이런 식으로 입력해서 하심 됩니다.
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString asStrBuf;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
asStrBuf = "";
}
//---------------------------------------------------------------------------
// 기능 : Thread의 Execure()에 해당됩니다.
// Source 중간 중간에 주석문을 달아 두었으니 읽어 보세요.
// 글구.... 문자열 Buffer를 사용하지 않고 AnsiString을 쓴 이유는
// 처리속도가 많이 걸림에도 AnsiString이 사용하기 편해서 입니다.
// 더 빠른 처리를 원하신다면 직접.... ^^;
// 주의 : CR = '@', LF = '*'로 처리했습니다.
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
if( Key == '\r' ) {
AnsiString asText = Edit1->Text;
int nPosCR;
int nPosLF;
int nLen;
// Edit1의 기 입력된 자료를 지우기 쉽도록 함 (경우에 따라 Edit1->Text = ""보다 쓸모가 있죠)
Edit1->SelStart = 0;
Edit1->SelLength = asText.Length();
if( asText != "" ) {
while( true ) {
nPosCR = asText.Pos( "@" ); // Serial port에서 읽으려면 조금 번거로워서
nPosLF = asText.Pos( "*" ); // "@", "*"을 CR, LF로 처리하도록 하였습니다.
nLen = asText.Length();
if( (nPosCR >= 1) & (nPosLF >= 1) ) { // CR, LF가 모두 있으면 앞에 것 부터 처리하게 함.
if( nPosCR > nPosLF ) { // 만약 CR LF가 붙어 있는 경우를 하나로 처리할 경우엔
nPosCR = nPosLF; // 조금 처리가 달라 지겠죠 -_-;;
}
}
else if( nPosLF >= 1 ) {
nPosCR = nPosLF;
}
if( nPosCR >= 1 ) { // CR, LF가 있는 경우 문자를 출력함.
asStrBuf += asText.SubString( 1, nPosCR - 1 );
Memo1->Lines->Add( asStrBuf ); // Synchronize() 부분에 해당 되겠죠!
asStrBuf = "";
asText = asText.SubString( nPosCR + 1, nLen );
}
else { // CR, LF가 더이상 없으므로 나머진 asStrBuf에 추가합니다.
asStrBuf += asText;
break;
}
}
}
}
}
//---------------------------------------------------------------------------
// 기능 : Button1을 눌렀을 경우 asStrBuf에 보관된 문자가 있으면 강제 출력하게 함.
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if( asStrBuf != "" ) {
Memo1->Lines->Add( asStrBuf );
asStrBuf = "";
}
}
-------------------------------------------------
초보 님이 쓰신 글 :
: 말 뜻을 잘 알겠습니다..
: 그런데.
: char InBuff[InBuffSize],StrBuf[InBuffSize];
: void __fastcall TSerialComm::Execute()
: {
: while(1)
: {
: int i,rr = 0;
: if(Terminated) return;
: ReadFile(hComm, InBuff, 300 , &dwBytesRead, NULL);
:
: if(dwBytesRead) {
: for(i = 0; i < dwBytesRead; i++) {
: if((InBuff[i] == '\n') || (InBuff[i] == '\r')) {
: rr = i;
: StrBuf[i] = 0;
: Synchronize(ReadDataDisplay);
: }
: else {
: StrBuf[i-rr] = InBuff[i];
: }
: }
: InBuff[dwBytesRead] = 0;
: Synchronize(ReadDataDisplay);
: }
: }
: void __fastcall TSerialComm::ReadDataDisplay()
: {
: FormMain->MemoSerial->Lines->Append(InBuff);
: }
:
: 님이 적으신 대로 한번 해봤습니다..그런데
: 어디가 잘못 됐는지 정말 모르겠네여..
: 한번 봐 주시겠어여?
|