home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Pascal / Snippets / Serial / serial.p < prev    next >
Encoding:
Text File  |  1995-12-04  |  9.3 KB  |  169 lines  |  [TEXT/PJMM]

  1. program SerialDrivers;
  2. {-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-}
  3. {This small program isn't very neat, but the procedures in it are very valuable.  Some of the following set}
  4. {of Serial procedures are modified versions from Inside Macintosh, and some are just things I've needed.}
  5. {}
  6. {If you were to take the listing straight out of Inside Macintosh, and try to run it, you would run into a few}
  7. {problems.  Those mistakes (Type-o's or whatever) are corrected here, and I think I've pointed them out.}
  8. {You can use this Code for whatever you want.  IF you learn from it, at least give me credit with a copy of}
  9. {your programs that you make.  You don't even have to mention my name anywhere, after all, some of this}
  10. {isn't straight out of my head.}
  11. {}
  12. {This Code was written in the ThinkPascal 4.0.2 environment.  If you distribute this code, please keep the .rsrc}
  13. {file with it, and the .π file with it too.}
  14. {If you need to get in touch with me, here's my info...}
  15. {}
  16. {Nathan Hendler}
  17. {Tucson, Az}
  18. {equant@azstarnet.com}
  19. {Thanks to Apple, and their Inside Macintosh Books.  Mostly the parts on the Serial Manager, but also the Speech}
  20. {Manager, 'cause it's a cool diversion.}
  21. {-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-}
  22.     uses
  23.         Serial;
  24.     const
  25.         kAboutDialogID = 129;
  26.         kAboutMenuID = 128;
  27.         kFileMenuID = 129;
  28.         kMenuID = 131;
  29.     var
  30.         gOutputRefNum: Integer; {output driver reference number}
  31.         gInputRefNum: Integer; {input driver reference number}
  32.         gInputBufHandle: Handle; {handle to my input buffer}
  33.         gOSErr: OSErr; {function results}
  34.         gMenuBarHandle: handle;
  35.         gEventRecord: EventRecord;
  36.         gQuit: BOOLEAN; {If False we stay in the program, if True we will exit.  (ie Quit)}
  37.  
  38.     procedure OpenSerialDrivers;
  39. {Use the Device Manager OpenDriver function to open the drivers.}
  40. {With this procedure we are opening the Modem Ports Drivers.}
  41.     begin
  42.         gOSErr := OpenDriver('.AOut', gOutputRefNum);         {always open output first... '.AOut' = ModemPort Out}
  43.         if gOSErr = noErr then                                     {If all's well, then open the input...}
  44.             gOSErr := OpenDrind;
  45.     end;
  46.  
  47.     procedure RestoreInputBuffer;
  48. {Restore the default input buffer.}
  49.     begin
  50.         gOSErr := SerSetBuf(gInputRefNum, gInputBufHandle^, 0); {0 means restore default}
  51.                                                                        {Wooops! Here's where that mistake happened again!}
  52.         HUnlock(gInputBufHandle); {release my old buffer}
  53.     end;
  54.  
  55.     procedure CloseSerialDriver;
  56. {Use the Device Manager KillIO function to terminate all current and pending}
  57. { operations, then close the drivers. Note that you only need to call KillIO}
  58. { on the output driver to terminate both input and output operations.}
  59.     begin
  60.         gOSErr := KillIO(gOutputRefNum); {terminate all pending I/O operations}
  61.                                             {Try to make sure you don't do this prematurely.  Empty them buffers first!}
  62.         if gOSErr = noErr then
  63.             gOSErr := CloseDriver(gInputRefNum); {close the input driver first}
  64.         if gOSErr = noErr then
  65.             gOSErr := CloseDriver(gOutputRefNum); {then close the output driver}
  66.     end;
  67.  
  68. {-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= End o' Serial Procedures =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-}
  69. {-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Application Procedures… =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-}
  70. {The following procedures are the bare bones of the application.  }
  71. {They're just here so that the program has a little home.}
  72.  
  73.     procedure CreateMenu;
  74.     begin
  75.         gMenuBarHandle := GetNewMBar(kMenuID);
  76.         if gMenuBarHandle <> nil then
  77.             begin
  78.                 HLock(gMenuBarHandle);
  79.                 SetMenuBar(gMenuBarHandle);
  80.                 SetMenuFlash(2);
  81.                 DrawMenuBar;
  82.             end;
  83.     end;
  84.  
  85.     procedure CloseMenu;
  86.     begin
  87.         if gMenuBarHandle <> nil then
  88.             begin
  89.                 HUnlock(gMenuBarHandle);
  90.                 DisposHandle(gMenuBarHandle);
  91.             end;
  92.     end;
  93.  
  94.     procedure About;
  95.         var
  96.             MyDialogPtr: DialogPtr;
  97.             TheItem: integer;
  98.     begin
  99.         MyDialogPtr := GetNewDialog(kAboutDialogID, nil, pointer(-1));
  100.         if MyDialogPtr <> nil then
  101.             begin
  102.                 ModalDialog(nil, TheItem);
  103.                 DisposDialog(MyDialogPtr);
  104.             end;
  105.     end;
  106.  
  107.     procedure DoMenu (MousePosition: Point);
  108.         var
  109.             MenuSelection: longint;
  110.             MenuID, MenuItem: integer;
  111.             dummy: boolean;
  112.     begin
  113.         MenuSelection := MenuSelect(MousePosition);
  114.         MenuID := HiWord(MenuSelection);
  115.         MenuItem := LoWord(MenuSelection);
  116.         case MenuID of
  117.             kAboutMenuID: 
  118.                 if MenuItem = 1 then
  119.                     About;
  120.             kFileMenuID: 
  121.                 if MenuItem = 2 then
  122.                     gQuit := true;
  123.             otherwise
  124.         end;
  125.         HiliteMenu(0);
  126.     end;
  127.  
  128.     procedure DoMouseEvent (theEvent: EventRecord);
  129.         var
  130.             MouseWhere: integer;
  131.             WindowSelected: WindowPtr;
  132.             tempPt: point;
  133.     begin
  134.         MouseWhere := FindWindow(theEvent.where, WindowSelected);
  135.         case MouseWhere of
  136.             inMenuBar: 
  137.                 DoMenu(theEvent.where);
  138.             otherwise
  139.         end;
  140.     end;
  141.  
  142.  
  143. {-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= End Application Procedures… =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-}
  144.  
  145.  
  146.  
  147.  
  148.  
  149. begin {UsingTheSerialDriver}
  150.     CreateMenu;
  151.     OpenSerialDrivers; {open the output and input drivers}
  152.     ChangeInputBuffer; {replace the default input buffer}
  153.     SetHandshakeOptions; {select flow control method}
  154.     ConfigureThePort; {set baud rate and data format}
  155.     SendMessage; {send some bytes to the output driver}
  156.     while not (gQuit) do
  157.         begin
  158.             if waitNextEvent(everyEvent, gEventRecord, 10, nil) then
  159.                 case gEventRecord.what of
  160.                     mouseDown: 
  161.                         DoMouseEvent(gEventRecord);
  162.                     otherwise
  163.                 end;
  164.         end;
  165.     CloseMenu;
  166.     ReceiveMessage; {if we were expecting to read some bytes from the input driver here's where we'd do it.}
  167.     RestoreInputBuffer; {restore the default input buffer}
  168.     CloseSerialDriver; {terminate I/O and close the drivers}
  169. end.