PDA

Ver la Versión Completa : uso del componente tcomport


astrocar
06-06-2010, 14:11:03
Hola buenos dias, mi problema es el siguiente estoy haciendo una aplicacion en delphi 7 y estoy usando el componente Tcomport, el asunto es elvia via rs232 los siguientes byte:

25 9B AD 1B A6 AA CD 3F 02 AE 18 CD 3F 00 AE 19
CD 3F 00 AD 0A AE 00 D6 FF FF CD 3F 02 20 EA AE
10 5A 26 FD 81 00

para tal caso me creo un buffer de manera areglo

buffer: arreglo[0..37] of byte

ahora para enviar mi buffer lo hago de la manera siguiente:

var
i;data:byte;
for i:=0 to $25 do
begin
comport1.WriteStr(char(buffer[i]);
comport1.Read(data,i);
mphexeditorex1.Data[i]:=data;
mphexeditorex1.Refresh;
end;

lo que obtengo en mi componente editor que lo estoy usando para ver lo que envio de manera de prueba es esto:

00 25 9B AD 1B A6 AA CD 3F 02 AE 18 CD 3F 00 AE
19 CD 3F 00 AD 0A AE 00 D6 FF FF CD 3F 02 20 EA AE
10 5A 26 FD 81

logico no me muestra el ultimo bayte 00 porque el ciclo for lo tengo hasta $25 que es 37 y eso corresponte al 81 pero si ingremeto ese ciclo a $26 que es 38 si me muestra el ultimo byte que es 00 pero eso no es mi problema mi problema es porque envia ese oo de primero si no estoy enviando eso yo empiezo desde 25 9b ....

Saludos y espero su colaboracion y disculpen si no me explique.
Atten.
Alexander Santana.
Barcelona-Venezuela.

rretamar
10-06-2010, 13:53:42
Hola.
Es extraño lo del 00 al inicio. Para analizar secuencias recibidas por el port serie se puede usar el utilitario "Serial Port Monitor" (que entre otros formatos muestra un "vuelco" hexadecimal/asc de lo recibido) y ver si se sigue mostrando ese cero.

http://www.serial-port-monitor.com

Saludos !

astrocar
10-06-2010, 19:45:32
Hola buenas tardes, lo mismo tenia en mente hacer; de hecho tengo un analizador logico para rs232 y con el voy a trabajar para ver pero lo que si puedo confirmar es que si envia de primero el 00, en lo que haga las pruebas informo como me fue.

Saludos y gracias por su opinion pense que nadie podia tomar interes en este tema.

Atten.
Alexander Santana.
Barcelona-Venezuela.

bactering
01-10-2010, 20:21:39
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