9.1. Communications RS232 exemple
Unité des communications RS232
//------------------------------------------------ ---------------------------
# Include
# Hdrstop Pragma
# Include "RS232Unit.h"
//------------------------------------------------ ---------------------------
# Paquet Pragma (smart_init)
# Pragma lien "Comm"
# Ressources Pragma "*. dfm"
TForm1 * Form1;
//------------------------------------------------ ---------------------------
__fastcall TForm1:: TForm1 (Owner * TComponent)
: TForm (Owner)
{
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: FormCreate (TObject * Sender)
{
/ / Définit les communications par défaut
ComboBox1-> ItemIndex = br9600; / / Bits par seconde
/ * 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 * /
ItemIndex ComboBox2-> = DA8 / / bits de données
/ * 0: da4
1: Da5
2: DA6
3: DA7
4: DA8 * /
ItemIndex ComboBox3-> = Panone / / Parité
/ * 0: Panone
1: paOdd (Par)
2: paEven (impair)
3: paMark (Marque)
4: PASPAC (Space) * /
Bits / / Stop; ItemIndex ComboBox4-> = SB10
/ * 0: SB10
1: SB 15
2: SB20 * /
ComboBox5-> ItemIndex = fcNone / / Flow Control
/ * 0: fcNone
1: fcCTS
2: fcDTR
3: fcSottware
4: * fcDefault /
ItemIndex ComboBox6-> = 0, / / Port
}
//------------------------------------------------ ---------------------------
/ / Green cocher sur clGreen clLime
/ / Check Blue OFF ON 0x00FF8000 0x00804000
void __fastcall TForm1:: Comm1RxChar (TObject * Sender, DWORD comte)
{
Etat int;
NumRec int;
String Dat;
Omble [101] BufRec;
Shape3-> Brush-> Color = clLime;
NumRec = comm1-> InQueCount (), / / Nombre de caractères reçus
Etat = comm1-> Lire (BufRec, comte) / / Lire tampon
if (état ==- 1)
{
ShowMessage ("Erreur dans la réception»);
}
d'autre
{
pour (int n = 0, n NumRec <, n + +)
{
if (! CheckBox1-> Checked)
{
Dat dat = + IntToStr (BufRec ])+','; n [
}
d'autre
{
Dat dat = + BufRec [n];
}
}
Texte Edit2-> = Texte Edit2-> + Dat;
}
Shape2-> Color Brush-> = 0x00FF8000;
Timer1-> Enabled = true;
}
//------------------------------------------------ ---------------------------
void TForm1:: EstadoLineas ()
{
if (CTS comm1->)
{
Shape8-> Color Brush-> = 0x00FF8000;
}
d'autre
{
Shape8-> Color Brush-> = 0x00804000;
}
if (comm1-> DSR)
{
Shape6-> Color Brush-> = 0x00FF8000;
}
d'autre
{
Shape6-> Color Brush-> = 0x00804000;
}
if (RING comm1->)
{
Shape9-> Color Brush-> = 0x00FF8000;
}
d'autre
{
Shape9-> Color Brush-> = 0x00804000;
}
if (comm1-> RLSD)
{
Shape1-> Color Brush-> = 0x00FF8000;
}
d'autre
{
Shape1-> Color Brush-> = 0x00804000;
}
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: Comm1Cts (TObject * Sender)
{
/ / Vérifier l'état des lignes CTS et DSR
EstadoLineas ();
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: Comm1Dsr (TObject * Sender)
{
/ / Vérifier l'état des lignes CTS et DSR
EstadoLineas ();
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: BitBtn1Click (TObject * Sender)
{
if (! comm1-> Activé ())
{
/ / Définition des paramètres
COMM1-> DeviceName = ComboBox6-> Strings articles-> [ItemIndex ComboBox6->] / / Port
COMM1-> baudrate = ComboBox1-> ItemIndex, / / Bits par seconde
DataBits COMM1-> = ItemIndex ComboBox2-> / / bits de données
Parité COMM1-> = ItemIndex ComboBox3-> / / Parité
StopBits COMM1-> = ItemIndex ComboBox4-> Bits / / Stop
FlowControl COMM1-> = ItemIndex ComboBox5-> / Contrôle de flux /
/ / Ouvrir un port de communication
Open COMM1-> ();
COMM1-> SetRTSState (true) / / Activer RTS
Shape7-> Brush-> Color = clLime;
COMM1-> SetDTRState (true) / / ligne DTR
Shape4-> Brush-> Color = clLime;
/ / Modifier l'état des indicateurs de port ouvert
Légende Label7-> = "Port Ouvert";
Shape10-> Brush-> Color = clLime;
/ / Vérifier l'état des lignes CTS et DSR
EstadoLineas ();
}
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: BitBtn2Click (TObject * Sender)
{
if (comm1-> Activé ())
{
/ / Fermer le port de communication
Fermer COMM1-> ();
/ / Modifier l'état des indicateurs de port ouvert
Label7-> Caption = "Port fermé";
Shape10-> Brush-> Color = clRed;
Shape7-> Brush-> Color = clGreen;
Shape4-> Brush-> Color = clGreen;
/ / Vérifier l'état des lignes CTS et DSR
EstadoLineas ();
}
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: BitBtn4Click (TObject * Sender)
{
Texte Edit2-> = "";
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: BitBtn3Click (TObject * Sender)
{
Texte Edit1-> = "";
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: Comm1Ring (TObject * Sender)
{
/ / Vérifier l'état des lignes CTS et DSR
EstadoLineas ();
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: Comm1Rlsd (TObject * Sender)
{
/ / Vérifier l'état des lignes CTS et DSR
EstadoLineas ();
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: Edit1KeyPress (TObject * Sender, char & Key)
{
dat char [2];
Dat [0] = Key;
COMM1-> Write (dat, 1);
Shape3-> Brush-> Color = clLime;
Timer2-> Enabled = true;
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: Timer1Timer (TObject * Sender)
{
Timer1-> Enabled = false;
Shape2-> Color Brush-> = 0x00804000;
}
//------------------------------------------------ ---------------------------
void __fastcall TForm1:: Timer2Timer (TObject * Sender)
{
Timer2-> Enabled = false;
Shape3-> Brush-> Color = clGreen;
}
//------------------------------------------------ ---------------------------

Exemple de test du programme RS-232 communications