home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / MiscKit1.2.6 / Source / MiscSerialPort.m < prev    next >
Encoding:
Text File  |  1994-05-15  |  3.5 KB  |  230 lines

  1. //
  2. //    MiscSerialPort.m -- an OO wrapper around the serial ports
  3. //        Written by Matt Brandt Copyright (c) 1994 by Matt Brandt.
  4. //                Version 2.0.  All rights reserved.
  5. //        This notice may not be removed from this source code.
  6. //
  7. //    This object is included in the MiscKit by permission from the author
  8. //    and its use is governed by the MiscKit license, found in the file
  9. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  10. //    for a list of all applicable permissions and restrictions.
  11. //    
  12.  
  13.  
  14. #import <misckit/MiscSerialPort.h>
  15. #import    <fcntl.h>
  16. #import    <sgtty.h>
  17.  
  18. struct misc_brt
  19. {
  20.     char    name[10];
  21.     int        brate;
  22. }
  23.     misc_brtable[] = {
  24.         {"110",B110},
  25.         {"300",B300},
  26.         {"600",B600},
  27.         {"1200",B1200},
  28.         {"2400",B2400},
  29.         {"4800",B4800},
  30.         {"9600",B9600},
  31.         {"19200",B19200},
  32.         {"38400",B38400},
  33.         {"57600",B57600},
  34.         {"",0}};
  35.  
  36. @implementation MiscSerialPort
  37.  
  38. - init
  39. {
  40.     portName[0] = '\0';
  41.     connected = NO;
  42.     delegate = NULL;
  43.     currentBaud = B19200;
  44.     currentParity = -1;        // no parity by default
  45.     return self;
  46. }
  47.  
  48. - setDeviceName: (const char *)name
  49. {
  50.     if( strcmp(name,portName) == 0 )
  51.         return self;
  52.         
  53.     strncpy(portName,name,63);
  54.     portName[63] = '\0';
  55.     if( connected )
  56.     {
  57.         [self disconnect];
  58.         if( ![self connect] )
  59.             strncpy(portName,"UNCONNECTED",63);
  60.     }
  61.     return self;
  62. }
  63.  
  64. - setPortParameters
  65. {
  66.     struct sgttyb sg;
  67.     
  68.     ioctl(fd,TIOCGETP,&sg);            // get baudrate and mode
  69.     sg.sg_ispeed = currentBaud;
  70.     sg.sg_ospeed = currentBaud;
  71.     sg.sg_erase = 0;
  72.     sg.sg_kill = 0;
  73.     sg.sg_flags = RAW;
  74.     ioctl(fd,TIOCSETP,&sg);            // set baudrate and raw mode
  75.     ioctl(fd,TIOCEXCL,&sg);            // exclusive access
  76.     ioctl(fd,TIOCHPCL,&sg);            // hang up on close
  77.     return self;
  78. }
  79.  
  80. - dropDTR
  81. {
  82.     ioctl(fd, TIOCCDTR,0);
  83.     return self;
  84. }
  85.  
  86. - raiseDTR
  87. {
  88.     ioctl(fd, TIOCSDTR,0);
  89.     return self;
  90. }
  91.  
  92. - setBaud: (int)baud
  93. {
  94.     currentBaud = baud;
  95.     if( connected )
  96.         [self setPortParameters];
  97.     return self;
  98. }
  99.  
  100. - setBaudByName: (const char *)speed
  101. {
  102.     struct misc_brt    *brtp;
  103.     
  104.     brtp = misc_brtable;
  105.     while( brtp->name[0] != '\0' )
  106.     {
  107.         if( strcmp(brtp->name,speed) == 0 )
  108.             return [self setBaud: brtp->brate];
  109.         ++brtp;
  110.     }
  111.     return NULL;
  112. }
  113.  
  114. - setParity: (int)parity
  115. {
  116.     currentParity = parity;
  117.     return self;
  118. }
  119.  
  120. - setDelegate: theConsumer
  121. {
  122.     delegate = theConsumer;
  123.     return self;
  124. }
  125.  
  126. - delegate
  127. {
  128.     return delegate;
  129. }
  130.  
  131. - transmitChars: (const char *)buffer length: (int)length
  132. {
  133.     if( connected )
  134.         write(fd,buffer,length);
  135.     return self;
  136. }
  137.  
  138. - (void)checkReceiver
  139. {
  140.     int        len;
  141.     char    buf[512];
  142.     
  143.     len = read(fd,buf,512);
  144.     if( len > 0 )
  145.     {
  146.         [delegate receiveChars: buf length: len];
  147.     }
  148. }
  149.  
  150. void    *SerialPortTimedEntry(int fdtag, id thePort)
  151. {
  152.     [thePort checkReceiver];
  153.     return thePort;
  154. }
  155.  
  156. - (BOOL)connect
  157. {
  158.     if( connected )            // do nothing if already connected
  159.         return YES;
  160.         
  161.     if( portName[0] == '\0' )
  162.         return NO;
  163.         
  164.     fd = open(portName,O_RDWR /* | O_NDELAY*/);
  165.     if( fd < 0 )
  166.         return NO;
  167.             
  168.     connected = YES;
  169.     
  170.     [self setPortParameters];        // set up baudrate etc...
  171.     
  172.     DPSAddFD(fd, 
  173.         (DPSFDProc)SerialPortTimedEntry, 
  174.         self, NX_MODALRESPTHRESHOLD);
  175.     suspended = NO;
  176.     return YES;
  177. }
  178.  
  179. - disconnect
  180. {
  181.     if( connected )
  182.     {
  183.         if( !suspended )
  184.             DPSRemoveFD(fd);
  185.         close(fd);
  186.         connected = NO;
  187.     }
  188.     return self;
  189. }
  190.  
  191. - suspend
  192. {
  193.     if( connected )
  194.     {
  195.         if( !suspended )
  196.             DPSRemoveFD(fd);
  197.         suspended = YES;
  198.     }
  199.     return self;
  200. }
  201.  
  202. - continue
  203. {
  204.     return [self resume];
  205. }
  206.  
  207. - resume
  208. {
  209.     if( connected && suspended )
  210.     {
  211.         DPSAddFD(fd, 
  212.             (DPSFDProc)SerialPortTimedEntry, 
  213.             self, NX_MODALRESPTHRESHOLD);
  214.         suspended = NO;
  215.     }
  216.     return self;
  217. }
  218.  
  219. - (BOOL)suspended
  220. {
  221.     return suspended;
  222. }
  223.  
  224. - (int)filedes
  225. {
  226.     return fd;
  227. }
  228.  
  229. @end
  230.