home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Source / MiscSerialPort.m < prev    next >
Encoding:
Text File  |  1994-02-05  |  3.1 KB  |  219 lines

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