9.1. RS232 Kommunikation zB
RS232 Communications Unit
//------------------------------------------------ ---------------------------
# Include
# Pragma hdrstop
# Include "RS232Unit.h"
//------------------------------------------------ ---------------------------
# Pragma Paket (smart_init)
# Pragma link "Comm"
# Pragma resource "*. dfm"
TForm1 * Form1;
//------------------------------------------------ ---------------------------
__fastcall TForm1:: TForm1 (TComponent * Owner)
: TForm (Eigentümer)
{
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: FormCreate (TObject * Sender)
{
/ / Festlegen der Standard-Kommunikation
ComboBox1-> ItemIndex = br9600 / / Bits pro Sekunde
/ * 0: br110
1: BR300
2: br600
3: BR1200
4: br2400
5: br4800
6: br9600
7: br14400
8: br19200
9: br38400
10: br56000
11: br57600
12: br115200 * /
ComboBox2-> ItemIndex = DA8 / / Daten-Bits
/ * 0: da4
1: Da5
2: DA6
3: Da7
4: DA8 * /
ComboBox3-> ItemIndex = Panone / / Parity
/ * 0: Panone
1: paOdd (Par)
2: paEven (ungerade)
3: paMark (Brand)
4: PASPAC (Space) * /
ComboBox4-> ItemIndex = SB10 / / Stopbits
/ * 0: SB10
1: SB 15
2: SB20 * /
ComboBox5-> ItemIndex = fcNone / / Flow Control
/ * 0: fcNone
1: fcCTS
2: fcDTR
3: fcSottware
4: fcDefault * /
ComboBox6-> ItemIndex = 0, / / Port
}
//------------------------------------------------ ---------------------------
/ / Green Check OFF ON clLime clGreen
/ / Blue Check OFF ON 0x00FF8000 0x00804000
void __fastcall TForm1:: Comm1RxChar (TObject * Sender, DWORD Count)
{
int Staat;
int NumRec;
Dat String;
BufRec char [101];
Shape3-> Brush-> Color = clLime;
NumRec = COMM1-> InQueCount (), / / Anzahl der empfangenen Zeichen
State = COMM1-> Read (BufRec, Graf) / / Read Buffer
if (Stand ==- 1)
{
ShowMessage ("Fehler bei der Aufnahme");
}
sonst
{
for (int n = 0, n <NumRec, n + +)
{
if (! CheckBox1-> Häkchen setzen)
{
Dat dat = + IntToStr (BufRec [n ])+',';
}
sonst
{
Dat dat = + BufRec [n];
}
}
Edit2-> Text = Edit2-> Text + Dat;
}
Form2-> Brush-> Color = 0x00FF8000;
Timer1-> Enabled = true;
}
//------------------------------------------------ ---------------------------
Leere TForm1:: EstadoLineas ()
{
if (COMM1 "> CTS)
{
Shape8-> Brush-> Color = 0x00FF8000;
}
sonst
{
Shape8-> Brush-> Color = 0x00804000;
}
if (COMM1-> DSR)
{
Shape6-> Brush-> Color = 0x00FF8000;
}
sonst
{
Shape6-> Brush-> Color = 0x00804000;
}
if (COMM1-> RING)
{
Shape9-> Brush-> Color = 0x00FF8000;
}
sonst
{
Shape9-> Brush-> Color = 0x00804000;
}
if (COMM1-> RLSD)
{
Form1-> Brush-> Color = 0x00FF8000;
}
sonst
{
Form1-> Brush-> Color = 0x00804000;
}
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: Comm1Cts (TObject * Sender)
{
/ / Prüfen des Status der CTS und DSR LINES
EstadoLineas ();
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: Comm1Dsr (TObject * Sender)
{
/ / Prüfen des Status der CTS und DSR LINES
EstadoLineas ();
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: BitBtn1Click (TObject * Sender)
{
if (! COMM1-> Enabled ())
{
/ / Die Parameter
Comm1-> DeviceName = ComboBox6-> Items-> Strings [ComboBox6-> ItemIndex] / / Port
Comm1-> Baudrate = ComboBox1-> ItemIndex, / / Bits pro Sekunde
Comm1-> DataBits = ComboBox2-> ItemIndex / / Daten-Bits
Comm1-> Parität = ComboBox3-> ItemIndex / / Parity
Comm1-> StopBits = ComboBox4-> ItemIndex / / Stopbits
Comm1-> FlowControl = ComboBox5-> ItemIndex / / Flow Control
/ / Offene Kommunikation Hafen
Comm1-> Open ();
Comm1-> SetRTSState (true) / / aktiviere RTS
Shape7-> Brush-> Color = clLime;
Comm1-> SetDTRState (true) / / aktiviere DTR-Leitung
Shape4-> Brush-> Color = clLime;
/ / Schaltet die Statusanzeigen offenen Port
Label7-> Caption = "Open Port";
Shape10-> Brush-> Color = clLime;
/ / Prüfen des Status der CTS und DSR LINES
EstadoLineas ();
}
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: BitBtn2Click (TObject * Sender)
{
if (COMM1-> Enabled ())
{
/ / Schließen der Kommunikations-Port
Comm1-> Close ();
/ / Schaltet die Statusanzeigen offenen Port
Label7-> Caption = "Port geschlossen";
Shape10-> Brush-> Color = clRed;
Shape7-> Brush-> Color = clGreen;
Shape4-> Brush-> Color = clGreen;
/ / Prüfen des Status der CTS und DSR LINES
EstadoLineas ();
}
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: BitBtn4Click (TObject * Sender)
{
Edit2-> Text = "";
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: BitBtn3Click (TObject * Sender)
{
Edit1-> Text = "";
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: Comm1Ring (TObject * Sender)
{
/ / Prüfen des Status der CTS und DSR LINES
EstadoLineas ();
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: Comm1Rlsd (TObject * Sender)
{
/ / Prüfen des Status der CTS und DSR LINES
EstadoLineas ();
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: Edit1KeyPress (TObject * Sender, char & Key)
{
char dat [2];
Dat [0] = Key;
Comm1-> Write (dat, 1);
Shape3-> Brush-> Color = clLime;
Timer2-> Enabled = true;
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: Timer1Timer (TObject * Sender)
{
Timer1-> Enabled = false;
Form2-> Brush-> Color = 0x00804000;
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: Timer2Timer (TObject * Sender)
{
Timer2-> Enabled = false;
Shape3-> Brush-> Color = clGreen;
}
//------------------------------------------------ ---------------------------

Beispiel Testprogramm RS-232