C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[57675] [질문] Cport->ReadStr()에서 데이터를 다 받질 못하고 있네요.???
아~자~~! [minonnet] 3120 읽음    2009-07-20 15:30
ComExample.rar 5.4MB TComPort4Beta의 ComExample 예제파일
Cport를 사용하여 시리얼 통신(RS232)을 하는데요.
일단 TComPort4Beta의 예제파일들로 실행시키고 있습니다.
예제파일명은 ComExample입니다.
문제점은 다음과 같습니다.

받는 데이터는 "02 01 C4 C7 03" 입니다.

1. EventChar가 #0 일때에,
   Cport->ReadStr()에서 데이터를 1개만 받습니다.
   받는 데이터의 중간에 NULL은 없습니다.

2. EventChar가 #3 일때에, (데이터 패킷을 마지막은 0x03)으로 끝납니다.)
   Cport->ReadStr()에서 데이터를 2개만 받습니다.
   받는 데이터의 중간에 NULL은 없습니다.

3. Cport->ReadStr()은 동기식이고,
    Cport->ReadStrAsync()은 비동기식이니까.
    따라서 RS232는 비동기식이므로  "Cport->ReadStrAsync()"을 사용해야 되지 않나요?

4. 다음은 ComMainForm.Cpp파일과 Form의 텍스트 파일입니다.
//**************************//
//***  ComMainForm.cpp   ***//
//**************************//
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "ComMainForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button_OpenClick(TObject *Sender)
{
  if (ComPort->Connected)
    ComPort->Close();
  else
    ComPort->Open();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button_SettingsClick(TObject *Sender)
{
  ComPort->ShowSetupDialog();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button_SendClick(TObject *Sender)
{
  AnsiString Str;

  Str = Edit_Data->Text;
  if (NewLine_CB->Checked)
    Str = Str + "\r\n";
  ComPort->WriteStr(Str);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ComPortOpen(TObject *Sender)
{
  Button_Open->Caption = "Close";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ComPortClose(TObject *Sender)
{
  Button_Open->Caption = "Open";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ComPortRxChar(TObject *Sender, int Count)
{
  AnsiString Str;

  ComPort->ReadStr(Str, Count);
  Memo->Text = Memo->Text + Str;
}
//---------------------------------------------------------------------------







//*************************************//
//***  다음은 Form의 텍스트 파일입니다  ***//
//*************************************//
object Form1: TForm1
  Left = 191
  Top = 107
  Caption = 'ComPort Library example'
  ClientHeight = 363
  ClientWidth = 558
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  PixelsPerInch = 96
  TextHeight = 13
  object Memo: TMemo
    Left = 1
    Top = 1
    Width = 433
    Height = 297
    ImeName = 'Microsoft Office IME 2007'
    ReadOnly = True
    ScrollBars = ssBoth
    TabOrder = 0
  end
  object Button_Open: TButton
    Left = 456
    Top = 8
    Width = 75
    Height = 25
    Caption = 'Open'
    TabOrder = 1
    OnClick = Button_OpenClick
  end
  object Button_Settings: TButton
    Left = 456
    Top = 40
    Width = 75
    Height = 25
    Caption = 'Settings'
    TabOrder = 2
    OnClick = Button_SettingsClick
  end
  object Edit_Data: TEdit
    Left = 440
    Top = 80
    Width = 113
    Height = 21
    ImeName = 'Microsoft Office IME 2007'
    TabOrder = 3
  end
  object Button_Send: TButton
    Left = 456
    Top = 104
    Width = 75
    Height = 25
    Caption = 'Send'
    Default = True
    TabOrder = 4
    OnClick = Button_SendClick
  end
  object NewLine_CB: TCheckBox
    Left = 448
    Top = 136
    Width = 89
    Height = 17
    Caption = 'Send new line'
    Checked = True
    State = cbChecked
    TabOrder = 5
  end
  object Panel1: TPanel
    Left = 0
    Top = 304
    Width = 433
    Height = 57
    BevelInner = bvRaised
    BevelOuter = bvLowered
    TabOrder = 6
    object ComLed1: TComLed
      Left = 112
      Top = 8
      Width = 25
      Height = 25
      ComPort = ComPort
      LedSignal = lsCTS
      Kind = lkPurpleLight
      RingDuration = 0
    end
    object ComLed2: TComLed
      Left = 144
      Top = 8
      Width = 25
      Height = 25
      ComPort = ComPort
      LedSignal = lsDSR
      Kind = lkPurpleLight
      RingDuration = 0
    end
    object ComLed3: TComLed
      Left = 176
      Top = 8
      Width = 25
      Height = 25
      ComPort = ComPort
      LedSignal = lsRLSD
      Kind = lkPurpleLight
      RingDuration = 0
    end
    object ComLed4: TComLed
      Left = 256
      Top = 8
      Width = 25
      Height = 25
      ComPort = ComPort
      LedSignal = lsRing
      Kind = lkYellowLight
      RingDuration = 0
    end
    object Label2: TLabel
      Left = 112
      Top = 32
      Width = 21
      Height = 13
      Caption = 'CTS'
    end
    object Label3: TLabel
      Left = 144
      Top = 32
      Width = 23
      Height = 13
      Caption = 'DSR'
    end
    object Label4: TLabel
      Left = 176
      Top = 32
      Width = 29
      Height = 13
      Caption = 'RLSD'
    end
    object Label5: TLabel
      Left = 256
      Top = 32
      Width = 22
      Height = 13
      Caption = 'Ring'
    end
    object ComLed5: TComLed
      Left = 344
      Top = 8
      Width = 25
      Height = 25
      ComPort = ComPort
      LedSignal = lsTx
      Kind = lkRedLight
      RingDuration = 0
    end
    object ComLed6: TComLed
      Left = 376
      Top = 8
      Width = 25
      Height = 25
      ComPort = ComPort
      LedSignal = lsRx
      Kind = lkRedLight
      RingDuration = 0
    end
    object Label1: TLabel
      Left = 350
      Top = 32
      Width = 12
      Height = 13
      Caption = 'Tx'
    end
    object Label6: TLabel
      Left = 382
      Top = 32
      Width = 13
      Height = 13
      Caption = 'Rx'
    end
  end
  object ComPort: TComPort
    BaudRate = br9600
    Port = 'COM1'
    Parity.Bits = prNone
    StopBits = sbOneStopBit
    DataBits = dbEight
    DiscardNull = True
    Events = [evRxChar, evTxEmpty, evRxFlag, evRing, evBreak, evCTS, evDSR, evError, evRLSD, evRx80Full]
    FlowControl.OutCTSFlow = False
    FlowControl.OutDSRFlow = False
    FlowControl.ControlDTR = dtrEnable
    FlowControl.ControlRTS = rtsDisable
    FlowControl.XonXoffOut = False
    FlowControl.XonXoffIn = False
    OnAfterOpen = ComPortOpen
    OnAfterClose = ComPortClose
    OnRxChar = ComPortRxChar
    Left = 384
    Top = 8
  end
end

+ -

관련 글 리스트
57675 [질문] Cport->ReadStr()에서 데이터를 다 받질 못하고 있네요.??? 아~자~~! 3120 2009/07/20
57682     Re:일정부분 자답... 의문점들 아직도 있음. 아~자~~! 2353 2009/07/20
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.