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;