home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-04 | 9.3 KB | 169 lines | [TEXT/PJMM] |
- program SerialDrivers;
- {-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-}
- {This small program isn't very neat, but the procedures in it are very valuable. Some of the following set}
- {of Serial procedures are modified versions from Inside Macintosh, and some are just things I've needed.}
- {}
- {If you were to take the listing straight out of Inside Macintosh, and try to run it, you would run into a few}
- {problems. Those mistakes (Type-o's or whatever) are corrected here, and I think I've pointed them out.}
- {You can use this Code for whatever you want. IF you learn from it, at least give me credit with a copy of}
- {your programs that you make. You don't even have to mention my name anywhere, after all, some of this}
- {isn't straight out of my head.}
- {}
- {This Code was written in the ThinkPascal 4.0.2 environment. If you distribute this code, please keep the .rsrc}
- {file with it, and the .π file with it too.}
- {If you need to get in touch with me, here's my info...}
- {}
- {Nathan Hendler}
- {Tucson, Az}
- {equant@azstarnet.com}
- {Thanks to Apple, and their Inside Macintosh Books. Mostly the parts on the Serial Manager, but also the Speech}
- {Manager, 'cause it's a cool diversion.}
- {-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-}
- uses
- Serial;
- const
- kAboutDialogID = 129;
- kAboutMenuID = 128;
- kFileMenuID = 129;
- kMenuID = 131;
- var
- gOutputRefNum: Integer; {output driver reference number}
- gInputRefNum: Integer; {input driver reference number}
- gInputBufHandle: Handle; {handle to my input buffer}
- gOSErr: OSErr; {function results}
- gMenuBarHandle: handle;
- gEventRecord: EventRecord;
- gQuit: BOOLEAN; {If False we stay in the program, if True we will exit. (ie Quit)}
-
- procedure OpenSerialDrivers;
- {Use the Device Manager OpenDriver function to open the drivers.}
- {With this procedure we are opening the Modem Ports Drivers.}
- begin
- gOSErr := OpenDriver('.AOut', gOutputRefNum); {always open output first... '.AOut' = ModemPort Out}
- if gOSErr = noErr then {If all's well, then open the input...}
- gOSErr := OpenDri nd;
- end;
-
- procedure RestoreInputBuffer;
- {Restore the default input buffer.}
- begin
- gOSErr := SerSetBuf(gInputRefNum, gInputBufHandle^, 0); {0 means restore default}
- {Wooops! Here's where that mistake happened again!}
- HUnlock(gInputBufHandle); {release my old buffer}
- end;
-
- procedure CloseSerialDriver;
- {Use the Device Manager KillIO function to terminate all current and pending}
- { operations, then close the drivers. Note that you only need to call KillIO}
- { on the output driver to terminate both input and output operations.}
- begin
- gOSErr := KillIO(gOutputRefNum); {terminate all pending I/O operations}
- {Try to make sure you don't do this prematurely. Empty them buffers first!}
- if gOSErr = noErr then
- gOSErr := CloseDriver(gInputRefNum); {close the input driver first}
- if gOSErr = noErr then
- gOSErr := CloseDriver(gOutputRefNum); {then close the output driver}
- end;
-
- {-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= End o' Serial Procedures =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-}
- {-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Application Procedures… =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-}
- {The following procedures are the bare bones of the application. }
- {They're just here so that the program has a little home.}
-
- procedure CreateMenu;
- begin
- gMenuBarHandle := GetNewMBar(kMenuID);
- if gMenuBarHandle <> nil then
- begin
- HLock(gMenuBarHandle);
- SetMenuBar(gMenuBarHandle);
- SetMenuFlash(2);
- DrawMenuBar;
- end;
- end;
-
- procedure CloseMenu;
- begin
- if gMenuBarHandle <> nil then
- begin
- HUnlock(gMenuBarHandle);
- DisposHandle(gMenuBarHandle);
- end;
- end;
-
- procedure About;
- var
- MyDialogPtr: DialogPtr;
- TheItem: integer;
- begin
- MyDialogPtr := GetNewDialog(kAboutDialogID, nil, pointer(-1));
- if MyDialogPtr <> nil then
- begin
- ModalDialog(nil, TheItem);
- DisposDialog(MyDialogPtr);
- end;
- end;
-
- procedure DoMenu (MousePosition: Point);
- var
- MenuSelection: longint;
- MenuID, MenuItem: integer;
- dummy: boolean;
- begin
- MenuSelection := MenuSelect(MousePosition);
- MenuID := HiWord(MenuSelection);
- MenuItem := LoWord(MenuSelection);
- case MenuID of
- kAboutMenuID:
- if MenuItem = 1 then
- About;
- kFileMenuID:
- if MenuItem = 2 then
- gQuit := true;
- otherwise
- end;
- HiliteMenu(0);
- end;
-
- procedure DoMouseEvent (theEvent: EventRecord);
- var
- MouseWhere: integer;
- WindowSelected: WindowPtr;
- tempPt: point;
- begin
- MouseWhere := FindWindow(theEvent.where, WindowSelected);
- case MouseWhere of
- inMenuBar:
- DoMenu(theEvent.where);
- otherwise
- end;
- end;
-
-
- {-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= End Application Procedures… =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-}
-
-
-
-
-
- begin {UsingTheSerialDriver}
- CreateMenu;
- OpenSerialDrivers; {open the output and input drivers}
- ChangeInputBuffer; {replace the default input buffer}
- SetHandshakeOptions; {select flow control method}
- ConfigureThePort; {set baud rate and data format}
- SendMessage; {send some bytes to the output driver}
- while not (gQuit) do
- begin
- if waitNextEvent(everyEvent, gEventRecord, 10, nil) then
- case gEventRecord.what of
- mouseDown:
- DoMouseEvent(gEventRecord);
- otherwise
- end;
- end;
- CloseMenu;
- ReceiveMessage; {if we were expecting to read some bytes from the input driver here's where we'd do it.}
- RestoreInputBuffer; {restore the default input buffer}
- CloseSerialDriver; {terminate I/O and close the drivers}
- end.