home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 339.lha / SerMod / source / serialio.c < prev    next >
C/C++ Source or Header  |  1990-02-08  |  7KB  |  287 lines

  1. /* SerialIO.c */
  2.  
  3. #include <exec/types.h>
  4. #include <exec/memory.h>
  5. #include <stdio.h>
  6. #include <devices/serial.h>
  7. #include <functions.h>
  8.  
  9. /* Default starting values */
  10. #define STARTBAUD 300  /* default baud rate */
  11. #define SERFLAGS    (SERF_SHARED | SERF_XDISABLED)
  12. #define CTLCHAR     0x11130501    
  13.  
  14. int Mode = 0;
  15.  
  16. /* declarations for the serial stuff */
  17. static struct IOExtSer *Read_Request = NULL;
  18. static UBYTE rs_in[2];
  19. static struct IOExtSer *Write_Request = NULL;
  20. static UBYTE rs_out[2];
  21.  
  22. /* stack to save Serial Port flags */
  23. #define MAXSTACK 10
  24.  
  25.  
  26. /*------------------------------------------------------------*/
  27. /*  GetSerialSigBit: return Read_Request's Signal Bit          */
  28. /*------------------------------------------------------------*/
  29. /*
  30. int GetSerialSigBit()
  31. {
  32.    return Read_Request->IOSer.io_Message.mn_ReplyPort->mp_SigBit;
  33. }
  34. */
  35. /*------------------------------------------------------------*/
  36. /*  CheckSerIO : return TRUE if serial port has a character   */
  37. /*------------------------------------------------------------*/
  38.  
  39. BOOL CheckSerIO()
  40. {
  41.    return (BOOL)( CheckIO( Read_Request ) != NULL);
  42. }
  43.  
  44. /*------------------------------------------------------------*/
  45. /*      FlushSerIO: Flush the receive buffer              */
  46. /*------------------------------------------------------------*/
  47.  
  48. void FlushSerIO()
  49. {
  50. register struct IOExtSer *r = Read_Request;
  51.  
  52.     if(CheckIO( r ) == FALSE) {          /* if IO pending */
  53.         AbortIO( r );                 /* abort it */
  54.         }
  55.     WaitIO( r );
  56.  
  57.     r->IOSer.io_Command = CMD_FLUSH;
  58.     DoIO( r );                         /* flush all IO requests */
  59.     r->IOSer.io_Command = CMD_CLEAR;
  60.     DoIO( r);                          /* flush receive buffer  */
  61. }
  62.  
  63. void RestartSerIO()
  64. {
  65.    register struct IOExtSer *r = Read_Request;
  66.    Read_Request->IOSer.io_Command = CMD_READ;
  67.    BeginIO( r );                       /* start receive request */
  68. }
  69.  
  70. /*------------------------------------------------------------*/
  71. /*  PushSerState: save current io flags                       */
  72. /*------------------------------------------------------------*/
  73. /*
  74. void PushSerState()
  75. {
  76.    register struct IOExtSer *r = Read_Request; 
  77.  
  78.    if ( stackSize < MAXSTACK )
  79.      {
  80.         /comment* Save the current Mode /
  81.         modeStack[ stackSize ] = Mode;
  82.         
  83.     /comment* Get the current flags /
  84.     AbortIO( r );
  85.     flagStack[ stackSize++ ] = r->io_SerFlags;
  86.     BeginIO( r );
  87.      }
  88. }
  89. */
  90.  
  91. /*------------------------------------------------------------*/
  92. /*  PullSerState: restore last saved flag state              */
  93. /*------------------------------------------------------------*/
  94. /*
  95. void PullSerState()
  96. {
  97.    register struct IOExtSer *r = Read_Request; 
  98.  
  99.    if ( stackSize > 0 )
  100.      {
  101.     /comment* Reset the Mode /
  102.     Mode = modeStack[ --stackSize ];
  103.  
  104.     /comment* Set the old flags /
  105.     AbortIO( r );
  106.     r->io_SerFlags = flagStack[ stackSize ];
  107.     r->IOSer.io_Command = SDCMD_SETPARAMS;
  108.     DoIO( r );
  109.     r->IOSer.io_Command = CMD_READ;
  110.     BeginIO( r );
  111.      }
  112. }
  113. */
  114.  
  115. /*-------------------------------------------------------------*/
  116. /*           CloseSerialIO: Close the serial port              */
  117. /*-------------------------------------------------------------*/
  118. void CloseSerialIO()
  119. {
  120.    register struct IOExtSer *r = Read_Request;
  121.    register struct IOExtSer *w = Write_Request;
  122.  
  123.    if ( r != NULL )
  124.      {
  125.     CloseDevice( r );
  126.     DeletePort( r->IOSer.io_Message.mn_ReplyPort );
  127.     FreeMem( r, (long)sizeof( *r ) );
  128.     Read_Request = NULL;
  129.       }
  130.  
  131.     if ( w != NULL )
  132.       {
  133.     CloseDevice( w );
  134.     DeletePort( w->IOSer.io_Message.mn_ReplyPort );
  135.     FreeMem( w, (long)sizeof( *w ) );
  136.     Write_Request = NULL;
  137.       }
  138. }
  139. /*-------------------------------------------------------------*/
  140. /* InitSerialIO: Open serial IO - return read Port           */
  141. /*-------------------------------------------------------------*/    
  142. int InitSerialIO(rd, wr)
  143. struct IOExtSer **rd, **wr;
  144. {
  145.    register struct IOExtSer *r, *w;
  146.  
  147.    r = (struct IOExtSer *) AllocMem( (long)sizeof( *r),
  148.      (long)MEMF_PUBLIC|MEMF_CLEAR);
  149.    if (r == NULL)
  150.     return NULL;
  151.  
  152.    Read_Request = r;
  153.    r->io_SerFlags = SERFLAGS;
  154.    r->io_CtlChar  = CTLCHAR;
  155.    r->IOSer.io_Message.mn_ReplyPort = CreatePort(NULL,NULL);
  156.    if(OpenDevice(SERIALNAME,NULL,r,NULL))
  157.       {
  158.      puts("Can't open Read device\n");
  159.     goto q4;
  160.       }
  161.  
  162.    r->io_Baud = STARTBAUD;
  163.    r->io_ReadLen = 8;
  164.    r->io_WriteLen = 8;
  165.    r->IOSer.io_Command = SDCMD_SETPARAMS;
  166.    DoIO(r);
  167.    r->IOSer.io_Command = CMD_READ;
  168.    r->IOSer.io_Length = 1;
  169.    r->IOSer.io_Data = (APTR)&rs_in[0];
  170.  
  171.  
  172.    w = (struct IOExtSer *)AllocMem( (long)sizeof( *w),
  173.       (long)MEMF_PUBLIC|MEMF_CLEAR);
  174.    if (w == NULL) goto q3;
  175.  
  176.    Write_Request = w;
  177.    w->io_SerFlags = SERFLAGS;
  178.    w->io_CtlChar  = CTLCHAR;
  179.    w->IOSer.io_Message.mn_ReplyPort = CreatePort(NULL,NULL);
  180.    if(OpenDevice(SERIALNAME,NULL,w,NULL))
  181.       {
  182.     puts("Can't open Write device\n");
  183.     goto q1;
  184.       }
  185.    w->io_Baud = STARTBAUD;
  186.    w->io_ReadLen = 8;
  187.    w->io_WriteLen = 8;
  188.    w->IOSer.io_Command = SDCMD_SETPARAMS;
  189.    DoIO(w);
  190.  
  191.    w->IOSer.io_Command = CMD_WRITE;
  192.    w->IOSer.io_Length = 1;
  193.    w->IOSer.io_Data = (APTR)&rs_out[0];
  194.  
  195.    BeginIO( r );
  196.     *rd = r;
  197.     *wr = w;
  198.    return 1;
  199.  
  200. q1:   DeletePort( w->IOSer.io_Message.mn_ReplyPort );
  201. q2:   FreeMem( w, (long)sizeof( *w) );
  202. q3:   CloseDevice( r );
  203. q4:   DeletePort( r->IOSer.io_Message.mn_ReplyPort );
  204. q5:   FreeMem( r, (long)sizeof( *r) );
  205.  
  206.    return NULL;
  207. }
  208.  
  209. /*----------------------------------------------------------*/
  210. /*         SetXonMode: set Xon On or Off                    */
  211. /*----------------------------------------------------------*/
  212.  
  213. void SetXonMode( status )
  214. BOOL status;
  215. {
  216.  
  217.    register UBYTE flags;
  218.    register struct IOExtSer *r = Read_Request; 
  219.  
  220.    /* Get the current flags */
  221.    AbortIO( r );
  222.    flags = r->io_SerFlags;
  223.  
  224.    if ( status )
  225.       flags &= ~(SERF_XDISABLED);
  226.    else
  227.       flags |= SERF_XDISABLED; 
  228.    r->io_SerFlags = flags;
  229.    r->IOSer.io_Command = SDCMD_SETPARAMS;
  230.    DoIO( r );
  231.    r->IOSer.io_Command = CMD_READ;
  232.    BeginIO( r );
  233. }
  234.  
  235. /*----------------------------------------------------------*/
  236. /*         SetSerBaud: set Serial Baud Rate                 */
  237. /*----------------------------------------------------------*/
  238.  
  239. void SetSerBaud( baud )
  240. int baud;
  241. {
  242.    register struct IOExtSer *r = Read_Request; 
  243.  
  244.    /* Get the current flags */
  245.    AbortIO( r );
  246.    r->io_Baud = baud;
  247.    r->IOSer.io_Command = SDCMD_SETPARAMS;
  248.    DoIO( r );
  249.    r->IOSer.io_Command = CMD_READ;
  250.    BeginIO( r );
  251. }
  252.  
  253. /*----------------------------------------------------------*/
  254. /*         SerIOPut: put a byte out the serial port        */
  255. /*----------------------------------------------------------*/
  256.  
  257. void SerIOPut( c )
  258. register UBYTE c;
  259. {
  260.  
  261.    *rs_out = c;
  262.    DoIO( Write_Request );
  263.  
  264. }
  265.  
  266. /*----------------------------------------------------------*/
  267. /*         SerIOGet: get a byte from the serial port        */
  268. /*----------------------------------------------------------*/
  269.  
  270. UBYTE SerIOGet()
  271. {
  272.    register struct IOExtSer *r = Read_Request;
  273.    register UBYTE c;
  274.  
  275.    WaitIO( r );
  276.    c = *rs_in;
  277.    BeginIO( r );
  278.  
  279.    if ( Mode )    /* if MODEECHO */
  280.       {
  281.          *rs_out = c;
  282.          DoIO( Write_Request );
  283.       }
  284.    return c;
  285. }
  286.  
  287.