Program demonštruje možnosť kominukácie pomocou sériového portu

Delphi & Pascal (česká wiki)
Přejít na: navigace, hledání
Kategórie: Periférie

Program: Serial_port.pas
Soubor exe: Serial_port.exe

Program demonštruje možnosť kominukácie pomocou sériového portu.
Pred spustením programu nastavte parametre sériového portu pomocou príkazu DOSu MODE:
MODE COMm[:] [BAUD=b] [PARITY=p] [DATA=d] [STOP=s] [RETRY=r]
Priklad:
MODE COM1:9600,N,8,1,P
{ SERIAL_PORT.PAS                                                   }
{ Example for communication via serial port.                        }
{ Parameters of serial port must set with DOS command before.       }
{ Command:                                                          }
{   MODE COMm[:] [BAUD=b] [PARITY=p] [DATA=d] [STOP=s] [RETRY=r]    }
{ Example:                                                          }
{   MODE COM1:9600,N,8,1,P                                          }
{                                                                   }
{ Datum:09.07.2013                             http://www.trsek.com }
 
program serial_port;
 
uses crt;
const noaddress=1;
 
var serialerror:byte;    {com1 = 0 atd.}
 
var port: word;
    serialmessage: string;
 
{ initialization serial port }
procedure Inicializace(port:word);assembler;
asm
  mov ah,00h
  mov dx,port
  mov al,255
  int 14h
end;
 
{ read string from serial port }
function ReceiveSerial(port:word):string;
var a,s,o:word;
    serialmessage:string;
begin
  s:=seg(serialmessage);
  o:=ofs(serialmessage);
  asm
    mov ah,18h
    mov dx,port
    mov cx,0ffh
    mov es,s
    mov di,o
    int 14h
    mov s,es
    mov o,di
    mov a,ax
  end;
  if (s <> seg(serialmessage)) and (o <> ofs(serialmessage)) then begin 
    serialerror:=noaddress
  end
  else begin
    byte(ptr(s,o)^):=a;
    ReceiveSerial:=serialmessage;
  end;
end;
 
{ send string to serial port }
procedure SendSerial(port:word;message:string);
var a,s,o:word;
    serialmessage:string;
begin
  serialmessage:=message;
  s:=seg(serialmessage);
  o:=ofs(serialmessage);
  a:=length(serialmessage)+1;
  asm
    mov ah,19h
    mov dx,port
    mov cx,a
    mov es,s
    mov di,o
    int 14h
  end;
end;
 
procedure Finish;
begin
  writeln('Error ', serialerror);
  readln;
  halt;
end;
 
begin
  clrscr;
  port:=0;
  serialerror:=0;
  write('Please choice serial port (0-COM1, 1-COM2, 2-COM3, etc.): ');
  readln(port);
 
  Inicializace(port);
  serialmessage:='Hello serial port';
  writeln('Send message to serial port:', serialmessage);
  SendSerial(port, serialmessage);
  if (serialerror <> 0) then 
     Finish;
 
  serialmessage:=ReceiveSerial(port);
  writeln('Receive form serial port:', serialmessage);
  if ((serialerror <> 0) or (serialmessage = '')) then 
     Finish;
 
  writeln('Final.');
  readln;
end.