|
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
|