FB II Compiler

PG PRO

Debugging

Memory

System

Mathematics

Resources

Disk I/O

Windows

Controls

Menus

Mouse

Keyboard

Text

Fonts

Drawing

Sound

Clipboard

Printing

Communication

ASM

Made with FB

COMMUNICATION

Reset the serial port


This is what I use to slam dunk the serial ports :
'----------------------------
' Serial Port Reset
' by Jon Birck
' mods by Ross W. Lambert
' This source code is in
' public domain.

'---------------------------- Constants

_portFree               = 0
_portBusy               = 2

_serModemPortIn         = -6
_serModemPortOut        = -7
_serPrinterPortIn       = -8
_serPrinterPortOut      = -9

'---------------------------- Functions

CLEAR LOCAL MODE
LOCAL FN closeSerialDriver(drvName$)
   DIM pBlk.50
   pBlk.ioRefNum% = VAL(drvName$)                  ' really!
END FN = FN CLOSE(@pBlk)                          ' returns osErr%

'----------------------------

LOCAL FN closeDrivers(port%)
   DIM 255 drv$
   DIM osErr%

   LONG IF port% = _modemPort
     drv$                  = STR$(_serModemPortIn)
     osErr%                = FN closeSerialDriver(drv$)
     LONG IF osErr%        = _noErr
       drv$                = STR$(_serModemPortOut)
       osErr%              = FN closeSerialDriver(drv$)
     END IF
   XELSE                                           ' printer port
     drv$                  = STR$(_serPrinterPortIn)
     osErr%                = FN closeSerialDriver(drv$)
     LONG IF osErr%        = _noErr
       drv$                = STR$(_serPrinterPortOut)
       osErr%              = FN closeSerialDriver(drv$)
     END IF
   END IF

END FN = osErr%

'----------------------------

' FN portStatus returns the status of the given serial port.

' OK for Miniruntime: Yes

' INPUTS:  port%   -  INTEGER: port number (_modemPort or _printerPort)
' OUTPUT:  pStatus% -  INTEGER: status: 0=not in use, 1=AppleTalk, 2=in use
' GLOBALS: none

LOCAL MODE
LOCAL FN portStatus(port%)
   DIM pStatus%

   SELECT port%
     CASE _modemPort
       pStatus% = PEEK(_portAUse)
     CASE _printerPort
       pStatus% = PEEK(_portBUse)
   END SELECT
   IF (pStatus% AND &0F) = _useATalk THEN pStatus% = _useATalk
   IF pStatus% > _portBusy THEN pStatus% = _portFree

END FN = pStatus%

'----------------------------

' OK for Miniruntime: Yes

' INPUTS:  port%   -  INTEGER: port number (_modemPort or _printerPort)
' OUTPUT:  none
' GLOBALS: none

LOCAL MODE
LOCAL FN resetPort(port%)
   DIM osErr%

   osErr% = _noErr
   LONG IF FN portStatus(port%) = _portBusy
     osErr% = FN closeDrivers(port%)
   END IF
END FN = osErr%

'----------------------------

LOCAL MODE
LOCAL FN waitForInput$(msg$)
   DIM 1 key$
   DIM mouseDn%
   PRINT msg$
   DO
     key$ = INKEY$
     mouseDn% = FN BUTTON
   UNTIL ((LEN(key$)>0) OR (mouseDn% =_true))
END FN = key$

'---------------------------- Main

WINDOW 1, "Serial Port Software Reset"
PRINT
PRINT " This demo will do a software reset of your modem port."
PRINT

k$ = FN waitForInput$(" Press RETURN to continue, any other key to abort...")
PRINT

LONG IF k$ = CHR$(13)
   LONG IF FN resetPort(_modemPort) = _noErr
     PRINT " Port reset successful!"
   XELSE
     PRINT " Error on reset, #" + STR$(osErr%)
   END IF
XELSE
   PRINT " Reset aborted."
END IF
'
PRINT " Do you want to reset your printer port?"
PRINT
k$ = FN waitForInput$(" Press RETURN to continue, any other key to abort...")
LONG IF k$ = CHR$(13)
   LONG IF FN resetPort(_printerPort) = _noErr
     PRINT " Port reset successful!"
   XELSE
     PRINT " Error on reset, #" + STR$(osErr%)
   END IF
XELSE
   PRINT " Reset aborted."
END IF
'
PRINT

PRINT
FN waitForInput$ (" Press any key to end.")

Mel Patrick

This may not be the answer, but look at Closing a Serial Port section in the technical note 1119

http://developer.apple.com/technotes/tn/tn1119.html

The following techniques are ways to ensure that you close the serial driver even if your application quit abnormally.

If you are a CFM application, use your CFM fragment's terminate procedure. See Inside Mac: PowerPC system software for detail

If the Thread Manager is available, set a terminate procedure for your main thread using SetThreadTerminator

If neither of the above, patch ExitToShell.

Masa