lotus



previous page: 6.5) How do I convert from a string to a floating point type? (Text - Mac Programming)
  
page up: Mac Programming FAQ
  
next page: 7.2) Where is a Berkley sockets library for the Mac?

7.1) How do I get at the serial ports? (Communications and Networking - Mac Programming)




Description

This article is from the Mac Programming FAQ, by Jon Watte h+@austin.metrowerks.com with numerous contributions by others.

7.1) How do I get at the serial ports? (Communications and Networking - Mac Programming)

You call OpenDriver for the names "\p.AOut" and "\p.AIn" to get at
the modem port, and "\p.BOut" and "\p.BIn" for the printer port. The
function RAMSDOpen was designed for the original Mac with 128 kB of
memory and 64 kB of ROM, and has been extinct for several years.

However, many users use their serial ports for MIDI, LocalTalk, graphic
tablets, or what have you and have installed an additional serial port
card to get more ports. What you SHOULD do as a good application is to
use the Comm Toolbox Resource Manager to search for serial resources;
this requires that the Comms Toolbox is present (true on earlier System
6 with an INIT, on later System 6 and System 7 always, as well as on
A/UX) and that you have initialized the comms resource manager. The
exact code follows (adapted from Inside Mac Comms Toolbox):

*code*
#include "CommResources.h"
OSErr
FindPorts ( Handle * portOutNames , Handle * portInNames , Handle * names , Handle * iconHandles ) {
  
OSErr ret = noErr ;
short old = 0 ;
CRMRec theCRMRec , * found ;
CRMSerialRecord * serial ;
  
    * portOutNames = NewHandle ( 0L ) ;
    * portInNames = NewHandle ( 0L ) ;
    * names = NewHandle ( 0L ) ;
    * iconHandles = NewHandle ( 0L ) ;
    while ( ! ret ) {
        theCRMRec . crmDeviceType = crmSerialDevice ;
        theCRMRec . crmDeviceID = old ;
        found = ( CRMRec * ) CRMSearch ( ( QElementPtr ) & theCRMRec ) ;
        if ( found ) {
            serial = ( CRMSerialRecord * ) found -> crmAttributes ;
            old = found -> crmDeviceID ;
            PtrAndHand ( & serial -> outputDriverName , * portOutNames ,
                sizeof ( serial -> outputDriverName ) ) ;
            PtrAndHand ( & serial -> inputDriverName , * portInNames ,
                sizeof ( serial -> inputDriverName ) ) ;
            PtrAndHand ( & serial -> name , * names , 
                sizeof ( serial -> name ) ) ;
            PtrAndHand ( & serial -> deviceIcon , * iconHandles ,
                sizeof ( serial -> deviceIcon ) ) ;
        } else {
            break ;
        }
    }
    return err ;
}
*end*

This will create four handles with the driver names, device names and
driver icon handles for all of the available serial devices. Then let
the user choose with a pop-up menu or scrolling list, and save the
choice in your settings file.

You can use OpenDriver, SetReset, SetHShake, SetSetBuf, SerGetBuf and
the other Serial Manager functions on these drivers. To write to the
serial port, use FSWrite for synchronous writes that wait until all is
written, or PBWrite asynchronously for queuing up data that is supposed
to go out but you don't want to wait for it. At least once each time
through your event loop, you should call SerGetBuf on the in driver
reference number you got from OpenDriver, and call FSRead for that many
bytes - neither more nor less.

If you are REALLY interested in doing the right thing, you will use the
Communications Toolbox Connection Manager instead; this will give you
access to modems, direct lines, and networks of various kinds using the
same API! Great for stuff like BBSes that may be on a network as well
etc. The Comms Toolbox also provides modularized terminal emulation
and file transfer tools, although the Apple-suplied VT102 tool is pretty
lame, as is the VT102 mode of the VT320 tool. And it seems as though
it was designed by people who'd never used a Mac before... more in the
Unix style, right down to the lack of documentation and "magicCookie."

 

Continue to:















TOP
previous page: 6.5) How do I convert from a string to a floating point type? (Text - Mac Programming)
  
page up: Mac Programming FAQ
  
next page: 7.2) Where is a Berkley sockets library for the Mac?