Link to home
Start Free TrialLog in
Avatar of Cerf
Cerf

asked on

89C52 <| Serial Port |> PC

Hello there,
I need to establish communication between my project (delphi 7) and a 89C52 µcontroller. Can you please help me? Newbie here.
Cërf.
Avatar of mokule
mokule
Flag of Poland image

hPort := INVALID_HANDLE_VALUE;

//-----------------------------------------------------------
function OpenPort(var hPort: THandle; Port: PChar;
        Body, Bity, Parz, Stop: Cardinal): boolean;
var
  dcbPort:TDCB;
begin
  result := true;
  if hPort = INVALID_HANDLE_VALUE then
    begin
    result := False;
    hPort := CreateFile( Port,
                      GENERIC_READ or GENERIC_WRITE,
                      0, nil,
                      OPEN_EXISTING,
                      FILE_ATTRIBUTE_NORMAL,
                      LongInt(0));
    if hPort <> INVALID_HANDLE_VALUE then
      begin
      if GetCommState(hPort, dcbPort) then
        begin
            { fill in the fields of the structure }
        dcbPort.BaudRate := Body;
        dcbPort.ByteSize := Bity;
        dcbPort.Parity := Parz;
        dcbPort.StopBits := Stop;
        dcbPort.Flags := 0;
            { flag bit fields:
            dcb_Binary, dcb_Parity, dcb_OutxCtsFlow, dcb_fOutxDsrFlow,
            dcb_fOutX, dcb_fInX, dcb_DtrFlow, dcb_RtsFlow
            }
        SetCommState(hPort, dcbPort);
        SetReadTimeouts(hPort);
        SetupComm(hPort,1200,1200);
        result := true;
        end;
    end;
  end
end;

//-----------------------------------------------------------
procedure ClosePort( var hPort: THandle);
begin
  if hPort <> INVALID_HANDLE_VALUE then
    begin
    if CloseHandle(hPort) then
      begin
      hPort := INVALID_HANDLE_VALUE;
      end;
    end
end;

//-----------------------------------------------------------
procedure SetReadTimeouts(hPort: THandle; Mnoznik, Staly, Odstep: Cardinal);
var
  tout: TCommTimeouts;
begin
  if hPort <> INVALID_HANDLE_VALUE  then
    begin
    GetCommTimeouts(hPort,tout);
    tout.ReadTotalTimeoutMultiplier := Mnoznik;
    tout.ReadTotalTimeoutConstant := Staly;
    tout.ReadIntervalTimeout := Odstep;
    SetCommTimeouts(hPort,tout);
    end;
end;

function SendRS( hPort: THandle; buf: PChar; count: Cardinal): Cardinal;
var
  nSended: Cardinal;
begin
  if hPort <> INVALID_HANDLE_VALUE then
    begin
    WriteFile(hPort, PChar(buf)^, count, nSended, nil);
    end
end;

//--------------------------------------------------------
function RcvRS(  hPort: THandle; buf: PChar; count: Cardinal): Cardinal;
var
  nReceived: Cardinal;
begin
  result := 0;
  if hPort <> INVALID_HANDLE_VALUE then
    begin
    ReadFile(hPort, PChar(buf)^, count, nReceived, nil);
    result := nReceived;
    end;
end;
ASKER CERTIFIED SOLUTION
Avatar of mokule
mokule
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cerf
Cerf

ASKER

Thank you very much!
I will let you know how everything ends....