home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / comm.swg / 0029_UART.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-05-25  |  4.0 KB  |  189 lines

  1.  
  2. {
  3. I've read some questions latelly with questions about how to use a com-port in
  4. pascal. I've written a couple of procedures for doing this. The following
  5. routines can be improved, for example they can be satt to run on interrupts
  6. and a few other thing, but... I'm not supposed to do all the job for you, am
  7. I??
  8. }
  9.  
  10. USES CRT,DOS;
  11.  
  12.  
  13. CONST
  14.      Com1 : WORD = 1;
  15.      Com2 : WORD = 2;
  16.  
  17. type
  18.     port = object
  19.        port: byte;
  20.        base: word;
  21.        baud: longint;
  22.        inter: byte;
  23.        function init(comport: word; baudrate: longint): boolean;
  24.        function sendchar(c: char): boolean;
  25.        function getchar(var c: char): boolean;
  26.     end;
  27.  
  28. function port.init(comport: word; baudrate: longint): boolean;
  29. var
  30.    tmp: word;
  31.    bas: word;
  32.    test: byte;
  33. begin
  34.      if odd(comport) then inter:=$C else inter:=$B;
  35.                           {This is for later use with interrupts...}
  36.      init:=false;
  37.      if comport<5 then
  38.      begin
  39.           asm {get port base address}
  40.              mov bx,40h
  41.              mov es,bx
  42.              mov bx,comport
  43.              dec bx
  44.              shl bx,1
  45.              mov ax,es:[bx]
  46.              mov bas,ax
  47.           end;
  48.           if bas=0 then
  49.           begin
  50.                writeln('Could''n find selected com-port!');
  51.                exit;
  52.           end;
  53.      end
  54.      else
  55.      begin
  56.           case comport of {don't know where to find ps/2 etd
  57.                            bios, standard base is supposed}
  58.             5: bas:=$4220;
  59.             6: bas:=$4228;
  60.             7: bas:=$5220;
  61.             8: bas:=$5228;
  62.           end;
  63.      end;
  64.      base:=bas;
  65.      tmp:=115200 div baudrate; {baudrate divisor}
  66.      asm {lower DTS and DSR}
  67.         mov dx,bas
  68.         add dx,4
  69.         xor al,al
  70.         out dx,al
  71.      end;
  72.      delay(50);
  73.      asm {raise DTS and DSR}
  74.         mov dx,bas
  75.         add dx,4
  76.         mov al,11b
  77.         out dx,al
  78.      end;
  79.      asm {set baudrate and N,8,1}
  80.         mov dx,bas
  81.         add dx,3
  82.         mov al,10000011b {N,8,1, set baudrate divisor}
  83.         out dx,al
  84.         mov ax,tmp {baudrate divisor}
  85.         mov dx,bas
  86.         out dx,al
  87.         inc dx
  88.         mov al,ah
  89.         out dx,al
  90.         mov dx,bas
  91.         add dx,3
  92.         mov al,00000011b {N,8,1}
  93.         out dx,al
  94.      end;
  95.      asm {interrupt enable, no interrupts enabled --> gain time}
  96.         mov dx,bas
  97.         inc dx
  98.         xor al,al
  99.         out dx,al
  100.      end;
  101.      asm {raise DTS and DSR}
  102.         mov dx,bas
  103.         add dx,4
  104.         mov al,11b
  105.         out dx,al
  106.         in al,dx
  107.         and al,11b
  108.         mov test,al
  109.      end;
  110.      if test<>3 then
  111.      begin
  112.           writeln('Some error....');
  113.           exit;
  114.      end;
  115.      init:=true;
  116. end;
  117.  
  118. function port.sendchar(c: char): boolean;
  119. var
  120.    bas: word;
  121.    cts: byte;
  122. label
  123.      no_send;
  124. begin
  125.      cts:=0;
  126.      bas:=base;
  127.      asm
  128.         mov dx,bas
  129.         add dx,5
  130.         in al,dx
  131.         and al,00100000b {test CTS (Clear To Send status)}
  132.         jz no_send
  133.         mov cts,1
  134.         mov dx,bas
  135.         mov al,c
  136.         out dx,al
  137.      no_send:
  138.      end;
  139.      if cts=0 then sendchar:=false else sendchar:=true;
  140. end;
  141.  
  142. function port.getchar(var c: char): boolean;
  143. var
  144.    bas: word;
  145.    rts: byte;
  146.    c2: char;
  147. label
  148.      no_data;
  149. begin
  150.      rts:=0;
  151.      bas:=base;
  152.      asm
  153.         mov dx,bas
  154.         add dx,5
  155.         in al,dx
  156.         and al,00000001b {test for data ready}
  157.         jz no_data
  158.         mov rts,1
  159.         mov dx,bas
  160.         in al,dx
  161.      no_data:
  162.         mov c2,al
  163.      end;
  164.      c:=c2;
  165.      if rts=0 then getchar:=false else getchar:=true;
  166. end;
  167.  
  168.  
  169. var
  170.    modem: port;
  171.    s: string;
  172.    a: byte;
  173.    c : Char;
  174.  
  175. begin
  176.      if not modem.init(com2,38400) then
  177.      begin
  178.           writeln('Couldn''t initialize modem...');
  179.           halt;
  180.      end;
  181.      s:='atz'+#13;
  182.      for a:=1 to length(s) do modem.sendchar(s[a]);
  183.  
  184. end.
  185.  
  186.  
  187. If you think these routines are just great and you decide to use them as they
  188. are I wouldn't mind if you gave me a credit.
  189.