The first time I came across Delphi was because the company needed to control the equipment using Delphi.
나는 맨 처음 델파이를 접하게 된 계기가 회사에서 델파이를 이용해서 장비를 제어하는 것이 필요해서였다.
I'm not a program major, so I had to do a lot of trial and error, and I often used the wrong development method, but "I do it until I can." "We will improve it to the end.I have developed the program with that mindset.
나는 프로그램 전공자가 아니여서 정말 많은 시행착오를 격어야 했고, 자주 잘못된 개발 방법을 사용하기도 했지만, "될때까지 한다." "끝까지 개선한다."라는 마음가짐을 가지고 프로그램을 개발해 왔다.
The picture below is one of the programs I made that uses serial communication.
아래 그림은 내가 만든 프로그램 중 시리얼 통신이 사용되는 프로그램 중 하나이다.
1. Serial communication program using Comport components.
1. Comport 컴포넌트를 이용한 시리얼 통신 프로그램.
- Double-click and insert the comport component into the form.
- Form에 comport 컴포넌트를 마우스로 더블클릭하여 삽입한다.
- Specify each value for the Object Inspector in the Comfort.(baudrate, Databits, Port, ...)
- It can be specified within the code (see below).
- Comport의 Object Inspector에 각 값을 지정한다.(baudrate, Databits, Port, ...)
- 코드 내에서 지정할 수 있다.(아래 참조)
procedure TfMain.COMPortSetting(nPortNo : integer; nBaudrate, nParity, nStopBits:string);
var i : integer;
strString : string;
begin
COMPORT.Port := 'COM' + intTostr(nPortNo);
case strTointDef(nBaudrate, 115200) of
110: i :=Integer(br110);
300: i :=Integer(br300);
600: i :=Integer(br600);
1200: i :=Integer(br1200);
2400: i :=Integer(br2400);
4800: i :=Integer(br4800);
9600: i :=Integer(br9600);
14400: i :=Integer(br14400);
19200: i :=Integer(br19200);
38400: i :=Integer(br38400);
56000: i :=Integer(br56000);
57600: i :=Integer(br57600);
115200: i :=Integer(br115200);
128000: i :=Integer(br128000);
256000: i :=Integer(br256000);
end;
COMPORT.BaudRate := TBaudRate(i);
if Pos('None',UpperCase(nParity)) > 0 then //(prNone, prOdd, prEven, prMark, prSpace);
COMPORT.Parity.Bits := prNone
else if Pos('ODD',UpperCase(nParity)) > 0 then //(prNone, prOdd, prEven, prMark, prSpace);
COMPORT.Parity.Bits := prOdd
else if Pos('EVEN',UpperCase(nParity)) > 0 then //(prNone, prOdd, prEven, prMark, prSpace);
COMPORT.Parity.Bits := prEven
else if Pos('MARK',UpperCase(nParity)) > 0 then //(prNone, prOdd, prEven, prMark, prSpace);
COMPORT.Parity.Bits := prMark
else if Pos('SPACE',UpperCase(nParity)) > 0 then //(prNone, prOdd, prEven, prMark, prSpace);
COMPORT.Parity.Bits := prSpace;
COMPORT.StopBits := sbOneStopBit; //
// COMPORT.DataBits := dbEight;
end;
- Below is an example of a basic program.
- 아래는 기본 프로그램 예이다.
unit SerialComm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ComPort;
type
TForm1 = class(TForm)
ComPort1: TComPort;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ComPort1RxChar(Sender: TObject; Count: Integer);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject); // comport open
begin
if not ComPort1.Connected then
ComPort1.Open;
end;
procedure TForm1.Button2Click(Sender: TObject); // comport close
begin
if ComPort1.Connected then
ComPort1.Close;
end;
procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer); // comport recieve event
var
Str: string;
begin
SetLength(Str, Count);
ComPort1.Read(Str[1], Count);
Memo1.Lines.Add(Str);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ComPort1.Device := 'COM1';
ComPort1.BaudRate := br9600;
ComPort1.Parity.Bits := prNone;
ComPort1.StopBits := sbOne;
ComPort1.DataBits := dbEight;
ComPort1.Connected := False;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); //
begin
if ComPort1.Connected then
ComPort1.Close;
end;
end.
- A method of transmitting a string is as follows.
- 문자열 string 전송하는 방법은 다음과 같다.
ComPort1.WriteString('Hello, World!');
- Translate into an array of strings and transfer
- 문자열 배열로 변환하여 전송하기
var
Str: string;
Bytes: TBytes;
begin
Str := 'Hello, World!';
Bytes := TEncoding.ASCII.GetBytes(Str);
ComPort1.WriteBuffer(Bytes[0], Length(Bytes));
end;
P.s The next article will cover the communication format of various methods (Can communication, Hex transmission, etc.)
P.s 다음 글에는 다양항 방법의 통신 포멧을 다뤄야 겠다.(Can통신, Hex전송 등)