9.1. Ejemplo de comunicaciones RS232
Unit de comunicaciones RS232
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "RS232Unit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Comm"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
//Establecer los valores por defecto de comunicaciones
ComboBox1->ItemIndex = br9600; //Bits por segundo
/* 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; //Bits de datos
/* 0: da4
1: da5
2: da6
3: da7
4: da8 */
ComboBox3->ItemIndex = paNone; //Paridad
/* 0: paNone
1: paOdd (Par)
2: paEven (impar)
3: paMark (Marca)
4: paSpace (Espacio) */
ComboBox4->ItemIndex = sb10; //Bits de parada
/* 0: sb10
1: sb15
2: sb20 */
ComboBox5->ItemIndex = fcNone; //Control de flujo
/* 0: fcNone
1: fcCTS
2: fcDTR
3: fcSottware
4: fcDefault */
ComboBox6->ItemIndex = 0; //Puerto
}
//---------------------------------------------------------------------------
//Verde Salidas OFF clGreen ON clLime
//Azul Entradas OFF 0x00804000 ON 0x00FF8000
void __fastcall TForm1::Comm1RxChar(TObject *Sender, DWORD Count)
{
int Estado;
int NumRec;
String Dat;
char BufRec[101];
Shape3->Brush->Color = clLime;
NumRec=Comm1->InQueCount(); //Número de caracteres recibidos
Estado = Comm1->Read(BufRec, Count); //Leer el Buffer
if(Estado==-1)
{
ShowMessage("Error en la recepción");
}
else
{
for(int n=0;n<NumRec;n++)
{
if(!CheckBox1->Checked)
{
Dat=Dat+IntToStr(BufRec[n])+',';
}
else
{
Dat=Dat+BufRec[n];
}
}
Edit2->Text=Edit2->Text+Dat;
}
Shape2->Brush->Color = 0x00FF8000;
Timer1->Enabled=true;
}
//---------------------------------------------------------------------------
void TForm1::EstadoLineas()
{
if(Comm1->CTS)
{
Shape8->Brush->Color = 0x00FF8000;
}
else
{
Shape8->Brush->Color = 0x00804000;
}
if(Comm1->DSR)
{
Shape6->Brush->Color = 0x00FF8000;
}
else
{
Shape6->Brush->Color = 0x00804000;
}
if(Comm1->RING)
{
Shape9->Brush->Color = 0x00FF8000;
}
else
{
Shape9->Brush->Color = 0x00804000;
}
if(Comm1->RLSD)
{
Shape1->Brush->Color = 0x00FF8000;
}
else
{
Shape1->Brush->Color = 0x00804000;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Comm1Cts(TObject *Sender)
{
//Mirar el estado de la línes CTS y DSR
EstadoLineas();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Comm1Dsr(TObject *Sender)
{
//Mirar el estado de la línes CTS y DSR
EstadoLineas();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
if(!Comm1->Enabled())
{
//Establecer los parámetros
Comm1->DeviceName = ComboBox6->Items->Strings[ComboBox6->ItemIndex]; //Puerto
Comm1->BaudRate = ComboBox1->ItemIndex; //Bits por segundo
Comm1->DataBits = ComboBox2->ItemIndex; //Bits de datos
Comm1->Parity = ComboBox3->ItemIndex; //Paridad
Comm1->StopBits = ComboBox4->ItemIndex; //Bits de parada
Comm1->FlowControl = ComboBox5->ItemIndex; //Control de flujo
//Abrir el puerto de comunicaciones
Comm1->Open();
Comm1->SetRTSState(true); //Activar la línea RTS
Shape7->Brush->Color = clLime;
Comm1->SetDTRState(true); //Activar la línea DTR
Shape4->Brush->Color = clLime;
//Activar los indicadores del estado del puerto abierto
Label7->Caption = "Puerto abierto";
Shape10->Brush->Color = clLime;
//Mirar el estado de la línes CTS y DSR
EstadoLineas();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
if(Comm1->Enabled())
{
//Cerrar el puerto de comunicaciones
Comm1->Close();
//Activar los indicadores del estado del puerto abierto
Label7->Caption = "Puerto cerrado";
Shape10->Brush->Color = clRed;
Shape7->Brush->Color = clGreen;
Shape4->Brush->Color = clGreen;
//Mirar el estado de la línes CTS y DSR
EstadoLineas();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
Edit2->Text="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
Edit1->Text="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Comm1Ring(TObject *Sender)
{
//Mirar el estado de la línes CTS y DSR
EstadoLineas();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Comm1Rlsd(TObject *Sender)
{
//Mirar el estado de la línes CTS y DSR
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;
Shape2->Brush->Color = 0x00804000;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
Timer2->Enabled=false;
Shape3->Brush->Color = clGreen;
}
//---------------------------------------------------------------------------

Ejemplo del programa test de comunicaciones RS-232