home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 2926 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.4 KB

  1. Path: jane.cs.waikato.ac.nz!not-for-mail
  2. From: trigg@jane.cs.waikato.ac.nz (Len Trigg)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Simple Serial/Timer problem
  5. Date: 7 Feb 1996 04:07:31 GMT
  6. Organization: University of Waikato, New Zealand
  7. Sender: trigg@waikato.ac.nz
  8. Message-ID: <4f98i3$1e48@thebes.waikato.ac.nz>
  9. Reply-To: trigg@waikato.ac.nz
  10. NNTP-Posting-Host: jane.cs.waikato.ac.nz
  11. X-Newsreader: TIN [AMIGA 1.3 950726BETA PL0]
  12.  
  13.  
  14. Hi all.  I'm hoping you can help me out with a small problem.
  15. I'm trying to write a program to talk to a Newton through the
  16. serial line.  Below is my code to read a character from the
  17. serial device - I'm wanting it to timeout if no character is
  18. available.  I thought the way to do this is to have a timer and
  19. then just wait until either the timer or the character came back,
  20. then cancel the request for the other.  Unfortunately it does not
  21. work :-)
  22.  
  23. The first time the routine gets called and a character is
  24. available it works.  The second time it gets called, the signal
  25. for the both the character and the timer is set, even though a
  26. character is available immediately.
  27.  
  28. There's probably something simple I'm not doing (as you can tell
  29. from the ugly code, I haven't really done much AmigaDOS
  30. programming)
  31.  
  32.  
  33. /* NewtonReadChar - Either get a single char from the Newton or timeout
  34.  */
  35. LONG NewtonReadChar(char *achar)
  36. {
  37.     LONG Result = NewtonOK;
  38.     ULONG    SignalSet;
  39.  
  40.     /* Ask for a byte from the serial line, or a timeout,
  41.      * whichever comes first
  42.      */
  43.             
  44.     /* Set command to TR_ADDREQUEST 
  45.      */
  46.     TimerRequest->tr_node.io_Command = TR_ADDREQUEST;
  47.     TimerRequest->tr_time.tv_secs   = 5;         /* 5 second timeout */
  48.     TimerRequest->tr_time.tv_micro  = 0;
  49.     SendIO((struct IORequest *)TimerRequest);
  50.  
  51.     /* Request a single byte from the
  52.      * serial line.
  53.      */
  54.     SerReadRequest->IOSer.io_Command    = CMD_READ;
  55.     SerReadRequest->IOSer.io_Length    = 1;
  56.     SerReadRequest->IOSer.io_Data    = achar;
  57.     SendIO((struct IORequest *)&SerReadRequest -> IOSer);
  58.  
  59.  
  60.     /* Wait and see what we get */
  61.     SignalSet = Wait(SIG_SERIAL | SIG_TIMER | SIGBREAKF_CTRL_C);
  62.  
  63.  
  64.     /* Did we get a signal from the serial line?
  65.      */
  66.     if(SignalSet & SIG_SERIAL)
  67.     {
  68.  
  69.         WaitIO((struct IORequest *)&SerReadRequest -> IOSer);
  70.  
  71.         /* Clear the timeout
  72.          */
  73.         AbortIO((struct IORequest *)TimerRequest);
  74.         WaitIO((struct IORequest *)TimerRequest);
  75.  
  76.         /* Echo the single character to the logfile
  77.          */
  78.         if (logfile)
  79.             fprintf(logfile, "%d %c ", *achar, *achar);
  80.     }
  81.  
  82.  
  83.     /* Did we get CTRL_C
  84.      */
  85.     if(SignalSet & SIGBREAKF_CTRL_C)
  86.     {
  87.         Result = NewtonBREAK;
  88.  
  89.         if (logfile)
  90.             fprintf(logfile, "Received CTRL_C\n");
  91.  
  92.         /* Clear both the timeout and the serial request
  93.          */
  94.         AbortIO((struct IORequest *)TimerRequest);
  95.         WaitIO((struct IORequest *)TimerRequest);
  96.         AbortIO((struct IORequest *)&SerReadRequest -> IOSer);
  97.         WaitIO((struct IORequest *)&SerReadRequest -> IOSer);
  98.     }
  99.  
  100.  
  101.     /* Did we get a timeout
  102.      */
  103.     if(SignalSet & SIG_TIMER)
  104.     {
  105.         Result = NewtonTIMEOUT;
  106.  
  107.         if (logfile)
  108.             fprintf(logfile, "Received TIMER\n");
  109.  
  110.         WaitIO((struct IORequest *)TimerRequest);
  111.  
  112.         /* Abort the serial read request */
  113.         AbortIO((struct IORequest *)&SerReadRequest -> IOSer);
  114.         WaitIO((struct IORequest *)&SerReadRequest -> IOSer);
  115.  
  116.     }
  117.  
  118.     return Result;
  119. }
  120.  
  121.  
  122. -- 
  123.  Len Trigg ===================================================
  124.  Comp Sci Grad   DoD#1334   trigg@cs.waikato.ac.nz             
  125.  Waikato Uni     GPX250     http://www.cs.waikato.ac.nz/~trigg          
  126.  Finger trigg@jane.cs.waikato.ac.nz for PGP Public key
  127.