Ver Mensaje Individual
  #4  
Antiguo 01-10-2010
bactering bactering is offline
Miembro
 
Registrado: jul 2006
Posts: 19
Reputación: 0
bactering Va por buen camino
A mi me pasaba lo mismo y pego los codigos tal cual los he usado que van bien. cada uno que se los adapte.
//---------------------------------------------------------------------------
void __fastcall TForm2::FormCreate(TObject *Sender)
{
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE) {
ShowMessage ("Puerto ocupado o fallo al abrir");
Application->Terminate ();
}
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess) {
ShowMessage ("GetCommState a dado error");
Application->Terminate ();
}
dcb.BaudRate = 115200; // 9600 set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
//___________________________________________
dcb.fBinary = TRUE; // Binary mode; no EOF check
dcb.fParity = TRUE; // Enable parity checking
dcb.fOutxCtsFlow = FALSE; // No CTS output flow control
dcb.fOutxDsrFlow = FALSE; // No DSR output flow control
dcb.fDtrControl = DTR_CONTROL_DISABLE ; // ENABLE
// DTR flow control type
dcb.fDsrSensitivity = FALSE; // DSR sensitivity
dcb.fTXContinueOnXoff = TRUE; // XOFF continues Tx
dcb.fOutX = FALSE; // No XON/XOFF out flow control
dcb.fInX = FALSE; // No XON/XOFF in flow control
dcb.fErrorChar = FALSE; // Disable error replacement
dcb.fNull = FALSE; // Disable null stripping
dcb.fRtsControl = RTS_CONTROL_DISABLE; // ENABLE
// RTS flow control
dcb.fAbortOnError = FALSE; // Do not abort reads/writes on
// error
//____________________________________________
Config1();
//________________________________________________

//---------------------------------------------------------------------------
void __fastcall TForm2::Config1() {

fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess) {
ShowMessage ("SetCommState a dado error");
Application->Terminate ();
}
CommTimeouts.ReadIntervalTimeout = 100;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 250;
CommTimeouts.WriteTotalTimeoutMultiplier = 1;
if(!SetCommTimeouts(hCom, &CommTimeouts)) {
CloseHandle(hCom);
hCom = NULL;
//Memo1->Lines->Add("No se puede establecer comunicación tiempos de espera.");
return;
}
}
//____________________________________________________


y para transmitir:

//---------------------------------------------------------------------------
//Transmisión de datos
void __fastcall TForm2::Transmite(byte Proces,byte SalB) {
char StringA50[9] = {Sincro,BitsTx,Master,Propio,Proces,SalB,0xAA,0xAA,0xAA};
WriteFile(hCom, // Handle
&StringA50, // Address of Outgoing data
9, // Number of bytes to write
&bytes_written, // Returned number of bytes written
NULL);
// ShowMessage ("Han sido enviados " + char (bytes_written));
Label20->Caption = bytes_written ;
Label20->Color = clAqua;
Timer5->Enabled = true;
}
//Transmisión de datos
void __fastcall TForm2::Transmite(byte Proces,byte SalB,byte SalB2) {
char StringA50[9] = {Sincro,BitsTx,Master,Propio,Proces,SalB,SalB2,0xAA,0xAA};
WriteFile(hCom, // Handle
&StringA50, // Address of Outgoing data
9, // Number of bytes to write
&bytes_written, // Returned number of bytes written
NULL);
// ShowMessage ("Han sido enviados " + char (bytes_written));
Label20->Caption = bytes_written ;
Label20->Color = clAqua;
Timer5->Enabled = true;
}
//Transmisión de datos
void __fastcall TForm2::Transmite(byte Proces1,byte Proces2,byte Proces3,byte Proces4,byte Proces5,byte Proces6,byte Proces7,byte Proces8) {
char StringA50[9] = {Proces1,Proces2,Proces3,Proces4,Proces5,Proces6,Proces7,Proces8,0xAA};
WriteFile(hCom, // Handle
&StringA50, // Address of Outgoing data
9, // Number of bytes to write
&bytes_written, // Returned number of bytes written
NULL);
// ShowMessage ("Han sido enviados " + char (bytes_written));
Label20->Caption = bytes_written ;
Label20->Color = clAqua;
Timer5->Enabled = true;
}

//--------------------------------------------------------

Je! esque envio de diferentes.
y para recibirlos:
//---------------------------------------------------------------------------
void __fastcall TForm2::Timer1Timer(TObject *Sender)
{
DWORD dwBytesRead,dwValor;
COMSTAT Sta;
if (ClearCommError (hCom, &dwValor,&Sta))
{ // 2
if (0< Sta.cbInQue)
{ // 1 Pues si que hay que leer
Memo1->Text = '>';
ReadFile(hCom, InBuff, sizeof (InBuff), &dwBytesRead, NULL);
for (DWORD i = 0; i <= dwBytesRead ; i++) {
Memo1->Text = Memo1->Text + (InBuff[i])+ " ";
} //1 Si ha saltado aquí es que no habia nada en el puerto
// Memo1->Text = Memo1->Text + '\r' + '\n';
Label18->Caption = dwBytesRead;
Label18->Color = clAqua;
Timer4->Enabled = true;
Procesallegada();
} //2
}
}
//---------------------------------------------------------------------------
Lo tengo en marcha y pirulando a las mil maravillas con los ceros y todo.
Saludos
Responder Con Cita