home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / pascal / 7552 < prev    next >
Encoding:
Internet Message Format  |  1992-12-17  |  5.8 KB

  1. Path: sparky!uunet!usc!sdd.hp.com!zaphod.mps.ohio-state.edu!malgudi.oar.net!news.ans.net!cmcl2!adm!news
  2. From: david@back.vims.edu
  3. Newsgroups: comp.lang.pascal
  4. Subject: Re: Need serial port unit.
  5. Message-ID: <34738@adm.brl.mil>
  6. Date: 17 Dec 92 18:22:27 GMT
  7. Sender: news@adm.brl.mil
  8. Lines: 212
  9.  
  10. >
  11. >Anyone got a decent unit for changing/setting the COM port parameters?
  12. >
  13. >Thanks in advance!
  14. >--
  15. >John DeRose                cs108069@pandora.sdsu.edu
  16. >Computer Science Undergrad
  17. >
  18. The following code is a text device driver that allows you to assign a COM port to a "text" type file and do i/o with the regular read/write statements, etc.  The only visible procedure is AssignAux which sets the parameters of the serial port.
  19.  
  20. This unit, based on an example in the TP4.0 manual, was used for driving a serial plotter and I have embedded in it various prompts and visual confirmation that data *is* going out of the port.
  21.  
  22. --------------------------------cut here----------------------------
  23. unit auxinout;
  24. interface
  25. uses dos, crt, util;
  26.  
  27. type  __paritytype = (odd, even, none);
  28.  
  29. procedure assignaux(var F: text;
  30.                     portnumber, baudrate, stopbits, databits:  word;
  31.                     parity : __paritytype );
  32. implementation
  33. {$R-,S-}
  34. type
  35.   auxrec = record
  36.              port,params: word;
  37.              unused: array[1..12] of byte;
  38.            end;
  39.  
  40. var
  41.   port, params: word;
  42.   xcol,ycol : byte;
  43.  
  44. function auxinit(port,params: word): byte;
  45. inline(
  46.   $58/              { POP AX       ; pop parameters }
  47.   $5A/              { POP DX       ; pop port number }
  48.   $B4/$00/          { MOV AH,0     ; code for initialize }
  49.   $CD/$14);         { INT 14H      ; Call BIOS }
  50.  
  51. function auxinchar(port: word): char;
  52. inline(
  53.   $5A/              { POP DX      ; pop port number }
  54.   $B4/$02/          { MOV AH,2    ; code for input }
  55.   $CD/$14);         { INT 14H     ; call BIOS }
  56.  
  57. procedure auxoutchar(port: word; ch: char);
  58. inline(
  59.   $58/              {      POP AX      ; pop character }
  60.   $5A/              {      POP DX      ; pop port number }
  61.   $B4/$01/          { LOOP MOV AH,1    ; code for output }
  62.   $CD/$14/          {      INT 14H     ; call BIOS }
  63.   $80/$E4/$80/      {      AND AH,$80  ; check time-out }
  64.   $75/$F7);         {      JNZ LOOP    ; try again }
  65.  
  66. function auxinready(port: word): boolean;
  67. inline(
  68.   $5A/              { POP DX      ; pop port number }
  69.   $B4/$03/          { MOV AH,3    ; code for status }
  70.   $CD/$14/          { INT 14H     ; call BIOS }
  71.   $88/$E0/          { MOV AL,AH   ; get line status in AH }
  72.   $24/$01);         { AND AL,1    ; isolate Data Ready bit }
  73.  
  74. {$F+}
  75.  
  76. function auxinput(var F: textrec): integer;
  77. var
  78.   p: word;
  79. begin
  80.   with f,auxrec(userdata) do begin
  81.     p := 0;
  82.     repeat until auxinready(port);
  83.     while auxinready(port) and (p<bufsize) do begin
  84.       bufptr^[p] := auxinchar(port);
  85.       inc(p)
  86.     end;
  87.     bufpos := 0;
  88.     bufend := p
  89.   end;
  90.   auxinput := 0
  91. end; { auxinput }
  92.  
  93. function auxoutput(var f: textrec): integer;
  94. var
  95.   p: word;
  96. begin
  97.   with f,auxrec(userdata) do begin
  98.     p := 0;
  99.     gotoxy(27, 20);
  100.     clreol;
  101.     while p<bufpos do begin
  102.       auxoutchar(port,bufptr^[p]);
  103.       write(#9);
  104.       inc(p)
  105.     end;
  106.     bufpos := 0
  107.   end;
  108.   auxoutput := 0
  109. end; { auxoutput }
  110.  
  111. function auxclose(var f : textrec) : integer;
  112. begin
  113.   gotoxy(1,20);
  114.   clreol;
  115.   gotoxy(xcol, ycol);
  116.  
  117.   auxclose := 0
  118. end; { auxclose }
  119.  
  120. function auxignore(var f: textrec ): integer;
  121. begin
  122.   auxignore := 0
  123. end; { auxignore }
  124.  
  125. function auxopen(var f: textrec): integer;
  126. begin
  127.   with f,auxrec(userdata) do begin
  128.     if mode=fmInput then begin
  129.       inoutfunc := @auxinput;
  130.       flushfunc := @auxignore
  131.     end
  132.     else begin
  133.       mode := fmOutput;
  134.       inoutfunc := @auxoutput;
  135.       flushfunc := @auxoutput
  136.     end;
  137.     closefunc := @auxclose
  138.   end;
  139.   xcol := wherex;
  140.   ycol := wherey;
  141.   gotoxy(10,20);
  142.   clreol;
  143.   write('plotting to COM', port+1,':');
  144.   gotoxy(xcol, ycol);
  145.  
  146.   auxopen := 0
  147. end; { auxopen }
  148.  
  149. {$F-}
  150.  
  151. procedure assignaux;
  152. var comstr : string;
  153.     temp   : string;
  154.     ch     : char;
  155.     ready  : boolean;
  156.     regs   : registers;
  157.     dsr    : word;
  158. begin
  159.  
  160.   case baudrate of
  161.      110:  baudrate := 0;
  162.      150:  baudrate := 1;
  163.      300:  baudrate := 2;
  164.      600:  baudrate := 3;
  165.     1200:  baudrate := 4;
  166.     2400:  baudrate := 5;
  167.     4800:  baudrate := 6;
  168.     else baudrate := 7;  { default to 9600 }
  169.   end;  { case }
  170.  
  171.   if stopbits = 2 then stopbits := 1
  172.   else stopbits := 0;    { default to 1 stop bit }
  173.  
  174.   if databits = 7 then databits := 2
  175.   else databits := 3;    { default to 8 data bits }
  176.  
  177.   params := (baudrate shl 5) + (stopbits shl 2) + databits;
  178.   port := portnumber - 1;
  179.  
  180.   case parity of
  181.      odd:  params := params + 8;
  182.     even:  params := params + 24;
  183.     else;  { default to no parity }
  184.   end; { case }
  185.  
  186.     dsr := (auxinit(port,params) shr 4) and 3;
  187.     ready := dsr = 3;
  188.  
  189.   while not Ready do begin
  190.     inc(textattr, blink);
  191.     center_write('Plotter not accepting data.', wherey+1);
  192.     center_write('Press <space> to resume when setup corrected, <Esc> to quit.', wherey+1);
  193.     buzz;
  194.     dec(textattr, blink);
  195.     repeat
  196.       ch := readkey
  197.     until ch in [' ', #27];
  198.     if ch = #27 then abort;
  199.     dsr := (auxinit(port,params) shr 4) and 3;
  200.     ready := dsr = 3;
  201.     gotoxy(1, wherey-2);
  202.   end;
  203.   gotoxy(1, wherey+1); clreol;
  204.   gotoxy(1, wherey+1); clreol;
  205.  
  206.  
  207.   with textrec(f) do begin
  208.     handle := $FFFF;
  209.     mode := fmClosed;
  210.     bufsize := sizeof(buffer);
  211.     bufptr := @buffer;
  212.     openfunc := @auxopen;
  213.     auxrec(userdata).port := port;
  214.     auxrec(userdata).params := params;
  215.     name[0] := #0
  216.   end
  217. end; { assignaux }
  218.  
  219. begin
  220. end. { unit AuxInOut }
  221. ------------------------------cut here-------------------------------*****************************************************************david@vims.eduDavid A. Evans,School of Marine Science, Virginia Institute of Marine ScienceCollege of William and MaryGloucester Point, Virginia, USA*****************************************************************
  222.