Alguien puede darme un pequeño ejemplo de como se configura un puerto com para usarlo como entrada y salida .

Necesito para un programa de comunicaciones trabajar con los puertos com y estoy muy perdido no encuentro información.

1 Respuesta

Respuesta
1

Te dejo un código de ejemplo:

var
hCommFile: THandle;
procedure TForm1.Button1Click(Sender: TObject);
var
  NumberWritten, BytesRead, BytesToRead: LongWord;
  Data: String;
  Buffer: PChar;
begin
  hCommFile := CreateFile('COM2', GENERIC_WRITE or GENERIC_READ, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); //Change COM1 to the comport you are using...
  if hCommFile = INVALID_HANDLE_VALUE then
    begin

      ShowMessage('Unable to open COM1');
      Exit;
    end;
  NumberWritten := 0;
  Data := 'Data come here...';
  if WriteFile(hCommFile, PChar(Data)^, Length(Data), NumberWritten, nil) = False then ShowMessage('Unable to write to COM1'); //Write...
  BytesRead := 0;
  BytesToRead := 5; //Change it to as much as many bytes you want to read...
  if ReadFile(hCommFile, Buffer, BytesToRead, BytesRead, nil) = False then ShowMessage('Unable to read from COM1'); //Read...the variable named Buffer will contain the data recieved...
end;
//Don't forget to close the COM port..
procedure TForm1.Button2Click(Sender: TObject);
begin
  CloseHandle(hCommFile);
end;

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas