Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Comunicacion Por El Puerto Serial C++Builder
Comunicacion Por El Puerto Serial C++Builder
N2.Septiembrede2008
AlterNEXT
SoftwareEngineering
Ahorasedeberatener2units,unaUnit1.cppparaelForm1,yuna
Unit2.cppparaelthread.
3.Aquhayunalistadeloseventosaimplementar:
Form1 OnCreate
Form1 OnClose
Memo1 OnKeyPress
#pragmaresource"*.dfm"
TForm1*Form1;
//
__fastcallTForm1::TForm1(TComponent*Owner)
:TForm(Owner)
{
}
//
void__fastcallTForm1::FormCreate(TObject*Sender)
{
Memo1>Clear();
DCBdcbCommPort;
//ABREELPUERTO
//REEMPLAZAR"COM2"POR"COM1","COM3",ETC.PARAABRIR
//OTROPUERTO
hComm=CreateFile("COM2",
GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
//SIELPUERTONOPUEDEABRIRSE...
if(hComm==INVALID_HANDLE_VALUE)
Application>Terminate();
//ESTABLECEMOSLOSTIMEOUTS
GetCommTimeouts(hComm,&ctmoOld);
ctmoNew.ReadTotalTimeoutConstant=100;
ctmoNew.ReadTotalTimeoutMultiplier=0;
ctmoNew.WriteTotalTimeoutMultiplier=0;
ctmoNew.WriteTotalTimeoutConstant=0;
SetCommTimeouts(hComm,&ctmoNew);
//ESTABLECEMOSBAUDRATE,PARITY,WORDSIZE,YSTOPBITS.
//HAYOTROSMETODOSDEHACERESTO,PEROESTEESELMASFACIL
dcbCommPort.DCBlength=sizeof(DCB);
GetCommState(hComm,&dcbCommPort);
BuildCommDCB("9600,N,8,1",&dcbCommPort);
SetCommState(hComm,&dcbCommPort);
//ACTIVAMOSELTHREAD
ReadThread=newTRead(false);
}
lvaroPintoTorrejn
apinto@alternext.com.bo
N2.Septiembrede2008
COMUNICACINPORELPUERTOSERIALUTILIZANDOC++BUILDER
AlterNEXT
SoftwareEngineering
//
void__fastcallTForm1::FormClose(TObject*Sender,TCloseAction&Action)
{
//TERMINAMOSELTHREAD.
ReadThread>Terminate();
//ESPERAMOSQUETERMINEYCERRAMOSELPUERTO
Sleep(250);
PurgeComm(hComm,PURGE_RXABORT);
SetCommTimeouts(hComm,&ctmoOld);
CloseHandle(hComm);
}
//
void__fastcallTForm1::Memo1KeyPress(TObject*Sender,char&Key)
{
//TRANSMITECUALQUIERCOSAESCRITAENELMEMO
TransmitCommChar(hComm,Key);
//ESTOPREVEELAVISUALIZACIONDECARACTERESBASURAENLAPANTALLA
if(Key!=13&&(Key<''||Key>'z'))
Key=0;
}
//
6.ElarchivocabeceraUnit2.h...
//
#ifndefUnit2H
#defineUnit2H
//
#include<Classes.hpp>
//
classTRead:publicTThread
{
private:
protected:
void__fastcallDisplayIt(void);
void__fastcallExecute();
public:
__fastcallTRead(boolCreateSuspended);
};
//
#endif
5.ElcdigofuentedelaUnit2.cpp...
//
#include<vcl.h>
#pragmahdrstop
//SEDEBEINCLUIRLACABECERADELAUNIT1
#include"Unit1.h"
#include"Unit2.h"
externHANDLEhComm;
charInBuff[100];
#pragmapackage(smart_init)
//
//Important:MethodsandpropertiesofobjectsinVCLcanonlybe
//usedinamethodcalledusingSynchronize,forexample:
//
//Synchronize(UpdateCaption);
//
//whereUpdateCaptioncouldlooklike:
//
//void__fastcallTRead::UpdateCaption()
//{
//Form1>Caption="Updatedinathread";
//}
//
__fastcallTRead::TRead(boolCreateSuspended)
:TThread(CreateSuspended)
{
}
//
void__fastcallTRead::DisplayIt()
{
//ENESTEEJEMPLONOSETIENEENCUENTACUANTOTEXTOHAENTRADOENEL
//Memo1QUEPUEDESERSOLODE32K.
//TAMPOCOSEHACENADACONLOSCARACTERESNOIMPRIMIBLES
//MUESTRAELTEXTORECIBIDO
Form1>Memo1>SetSelTextBuf(InBuff);
}
//
void__fastcallTRead::Execute()
{
//Placethreadcodehere
DWORDdwBytesRead;
//HACEQUEELOBJETOTRHEADSEADESTRUIDOCUANDOELTHREADTERMINA
FreeOnTerminate=true;
while(1){
//INTENTALEERDELPUERTOSERIE
//YSIHAYALGOLOVISUALIZA
ReadFile(hComm,InBuff,50,&dwBytesRead,NULL);
if(dwBytesRead){
InBuff[dwBytesRead]=0;//ACABALACADENAEN
NULO
Synchronize(DisplayIt);
}
}
}
//
Pgina2
http://www.alternext.com.bo
lvaroPintoTorrejn
apinto@alternext.com.bo