home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / amiga / programm / 13070 < prev    next >
Encoding:
Text File  |  1992-09-03  |  9.1 KB  |  300 lines

  1. Path: sparky!uunet!ivgate!mtask!MsgPost.v1.00
  2. From: MsgPost.v1.00@mtask.omahug.org (MsgPost v1.00)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: [1 of 2] A serial device example (LONG)
  5. Message-ID: <14.2aa6c1ce@ivgate>
  6. Date: 03 Sep 92 08:19:34 CST
  7. Reply-To: msgpost.v1.00@mtask.omahug.org
  8. Organization: Multitasking Systems, Kansas City
  9. Sender: news@ivgate.omahug.org (UUscan 1.10)
  10. Followup-To: comp.sys.amiga.programmer
  11. Lines: 287
  12.  
  13. Xref: uunet comp.sys.amiga.programmer:20390
  14. Path: uunet!uunet!europa.asd.contel.com!darwin.sura.net!uvaarpa!adastra!mbs
  15. From: mbs@adastra.cvl.va.us (Michael B. Smith)
  16. Newsgroups: comp.sys.amiga.programmer
  17. Subject: A serial.device example (LONG) (was Re: SERIAL.DEVICE
  18. HEEEEELLLLLPPPPPPP)
  19. Distribution: world
  20. Message-ID: <mbs.0x9d@adastra.cvl.va.us>
  21. References:  <u895762.715081138@bruny> <Vic.00yf@angband.delfax.OCunix.On.Ca>
  22. X-NewsSoftware: GRn 1.16f (beta) by Mike Schwartz & Michael B. Smith
  23. Date: 1 Sep 92 19:00:10 EDT
  24. Organization: Private UUCP Node
  25. Lines: 544
  26.  
  27. In article <Vic.00yf@angband.delfax.OCunix.On.Ca>
  28. Vic@angband.delfax.OCunix.On.Ca (Vic Lewington) writes:
  29. > In article <u895762.715081138@bruny> u895762@bruny.cc.utas.edu.au (Paul S.E.)
  30. writes:
  31. > > I have a problem that has been driving me mad for the past 5 hours.
  32. > > I am trying to read data from serial.device, one character at a time. (i.e.
  33. in real time), but the 512 byte buffer prevents me from doing it.
  34. > > HELP, anybody?!
  35. >
  36. >     Having just spent a few hours going nuts myself, here, try this...
  37.  
  38. [example deleted]
  39.  
  40. Apologies for posting this to a non-source group. However, I spent a couple
  41. of days looking for a simple, working example, before writing this, and it
  42. wasn't to be found. This may do someone else some good...
  43.  
  44. Here is a piece of working code that comes with the next release of GRn. No
  45. warranties, and you get what you pay for. It was written to be clear, not
  46. extremely efficient. This requires 2.0+, due to the use of PrintFault().
  47. Remove that, and it should work on 1.3.
  48.  
  49. This is DICE code, but the only place it should matter is the declaration of
  50. InputHandler(), which is currently:
  51.              __geta4 void InputHandler (void);
  52. I think SAS/C will require this to be:
  53.              void __saveds InputHandler (void);
  54. but I'm not sure...
  55.  
  56. Flames to NIL:
  57.  
  58. /*
  59.         GRn-Term.c
  60.  
  61.         GRn - Gadtools Read News
  62.  
  63.         Copyright 1992 by Michael B. Smith.
  64.  
  65.         All Rights Reserved.
  66.  
  67.         No warranties, express or implied, are available. Use at your own risk.
  68.         The authors have taken pains to attempt to produce quality software,
  69.         but by no means are we liable for anything it does to your system.
  70.  
  71.         Feel free to modify the source for your own use, but do NOT distribute
  72.         unauthorized sources or binaries as part of GRn. You may distribute
  73.         the source and binaries in unmodified form.
  74. */
  75.  
  76. #define _GRNTERM_
  77.  
  78. #include <stdlib.h>
  79. #include <stdarg.h>
  80.  
  81. #include <exec/types.h>
  82. #include <devices/serial.h>
  83. #include <dos/dosextens.h>
  84. #include <dos/dostags.h>
  85. #include <clib/dos_protos.h>
  86.  
  87. #ifndef _GRNTERM_
  88. extern void Abort (void);
  89. extern void panic (const char *, ...);
  90. #endif
  91.  
  92. static struct MsgPort *NNTP_in_mp  = NULL;
  93. static struct MsgPort *NNTP_out_mp = NULL;
  94.  
  95. static int NNTP_in_ip  = 0; // input  IO in-process
  96. static int NNTP_out_ip = 0; // output IO in-process
  97.  
  98. struct IOExtSer *NNTP_in_io  = NULL;
  99. struct IOExtSer *NNTP_out_io = NULL;
  100.  
  101. int serial_error = 0;
  102.  
  103. void ShutDownSerialPort (void);
  104. void KillSerialPort     (const int, const char *);
  105. void SetupSerialPort    (const int, const char *, const int);
  106. int reads               (struct IOExtSer *, char *, int);
  107. int writes              (struct IOExtSer *, char *, int);
  108.  
  109. void ShutDownSerialPort (void)
  110. {
  111.         static int in_sdsp = 0;  // use as a semaphore
  112.  
  113.         if (in_sdsp)
  114.                 return;
  115.  
  116.         in_sdsp = 1;
  117.  
  118.         if (NNTP_out_io && NNTP_out_ip) {
  119.                 AbortIO (NNTP_out_io);
  120.                 WaitIO (NNTP_out_io);
  121.                 NNTP_out_ip = 0;
  122.         }
  123.         if (NNTP_in_io && NNTP_in_ip) {
  124.                 AbortIO (NNTP_in_io);
  125.                 WaitIO (NNTP_in_io);
  126.                 NNTP_in_ip = 0;
  127.         }
  128.  
  129.         if (NNTP_in_io && !serial_error)
  130.                 CloseDevice (NNTP_in_io);
  131.  
  132.         if (NNTP_out_io) {
  133.                 DeleteExtIO (NNTP_out_io, sizeof (struct IOExtSer));
  134.                 NNTP_out_io = NULL;
  135.         }
  136.  
  137.         if (NNTP_in_io) {
  138.                 DeleteExtIO (NNTP_in_io, sizeof (struct IOExtSer));
  139.                 NNTP_in_io = NULL;
  140.         }
  141.  
  142.         if (NNTP_out_mp) {
  143.                 DeletePort (NNTP_out_mp);
  144.                 NNTP_out_mp = NULL;
  145.         }
  146.  
  147.         if (NNTP_in_mp) {
  148.                 DeletePort (NNTP_in_mp);
  149.                 NNTP_in_mp = NULL;
  150.         }
  151.  
  152.         in_sdsp = 0;
  153.         return;
  154. }
  155.  
  156. void KillSerialPort (const int errno, const char *msg)
  157. {
  158. #ifdef _GRNTERM_
  159.         if (errno)
  160.                 PrintFault (errno, msg);
  161.         else {
  162.                 PutStr (msg);
  163.                 PutStr ("\n");
  164.         }
  165.         ShutDownSerialPort ();
  166.         exit (20);
  167. #else
  168.         ShutDownSerialPort ();
  169.         panic (msg);
  170.         Abort ();
  171. #endif
  172.         /* NOTREACHED */
  173.         return;
  174. }
  175.  
  176. int SendQuery (struct IOExtSer *iorequest)
  177. {
  178.         int error;
  179.  
  180.         iorequest->IOSer.io_Command = SDCMD_QUERY;
  181.         if (error = DoIO (iorequest)) {
  182.                 PrintFault (error, "SendQuery error");
  183.                 return -1;
  184.         }
  185.  
  186.         return iorequest->IOSer.io_Actual;
  187. }
  188.  
  189. void SiphonIO (struct IOExtSer *iorequest)
  190. {
  191.         int i;
  192.         char buf [16];
  193.  
  194.         while ((i = SendQuery (iorequest)) > 0) {
  195.                 iorequest->IOSer.io_Command = CMD_READ;
  196.                 iorequest->IOSer.io_Data    = buf;
  197.                 iorequest->IOSer.io_Length  = i > 16 ? 16 : i;
  198.                 iorequest->IOSer.io_Flags   = IOF_QUICK;
  199.  
  200.                 if (i = DoIO (iorequest))
  201.                         PrintFault (i, "SiphonIO error");
  202.         }
  203.         return;
  204. }
  205.  
  206. void SetupSerialPort (const int eofmode, const char *device, const int unit)
  207. {
  208.         int errno;
  209.  
  210.         if(!(NNTP_in_mp = (struct MsgPort *) CreatePort (NULL, 0)))
  211.                 KillSerialPort (0, "cannot create msgport for input");
  212.  
  213.         if (!(NNTP_out_mp = (struct MsgPort *) CreatePort (NULL, 0)))
  214.                 KillSerialPort (0, "cannot create msgport for output");
  215.  
  216.         if (!(NNTP_in_io = (struct IOExtSer *) CreateExtIO (NNTP_in_mp, sizeof
  217. (struct IOExtSer))))
  218.                 KillSerialPort (0, "cannot createExtIO() for input");
  219.  
  220.         if (!(NNTP_out_io = (struct IOExtSer *) CreateExtIO (NNTP_out_mp,
  221. sizeof (struct IOExtSer))))
  222.                 KillSerialPort (0, "cannot createExtIO() for output");
  223.  
  224.         NNTP_in_io->io_SerFlags = SERF_SHARED | SERF_XDISABLED;
  225.         if (eofmode) {
  226.                 NNTP_in_io->io_SerFlags |= SERF_EOFMODE;
  227.                 NNTP_in_io->io_TermArray.TermArray0 = 0x1615120c;
  228.                 NNTP_in_io->io_TermArray.TermArray1 = 0x0a030000;
  229.         }
  230.  
  231.         if (serial_error = OpenDevice (device, unit, NNTP_in_io, 0)) {
  232.                 printf ("device %s unit %ld IoErr = %ld\n", device, unit, IoErr
  233. ());
  234.                 KillSerialPort (serial_error, "OpenDevice() failed");
  235.         }
  236.  
  237.         SendQuery (NNTP_in_io);
  238.  
  239.         NNTP_in_io->io_SerFlags = SERF_SHARED | SERF_XDISABLED;
  240.         if (eofmode) {
  241.                 NNTP_in_io->io_SerFlags |= SERF_EOFMODE;
  242.                 NNTP_in_io->io_TermArray.TermArray0 = 0x1615120c;
  243.                 NNTP_in_io->io_TermArray.TermArray1 = 0x0a030000;
  244.         }
  245.         if (NNTP_in_io->io_RBufLen < 32768)
  246.                 NNTP_in_io->io_RBufLen = 32768;
  247.  
  248.         NNTP_in_io->IOSer.io_Command  = SDCMD_SETPARAMS;
  249.  
  250.         if (errno = DoIO (NNTP_in_io))
  251.                 KillSerialPort (errno, "setparams error");
  252.  
  253.         SiphonIO (NNTP_in_io);
  254.  
  255.         memcpy ((void *) NNTP_out_io, (void *) NNTP_in_io, sizeof (struct
  256. IOExtSer));
  257.         NNTP_out_io->IOSer.io_Message.mn_ReplyPort = NNTP_out_mp;
  258.  
  259.         return;
  260. }
  261.  
  262. int reads (struct IOExtSer *iorequest, char *buf, int len)
  263. {
  264.         int errno;
  265.         int mask;
  266.         //
  267.         // reads:   Get input from "iorequest". We DO NOT HANG. If
  268.         //          no bytes are available, we immediately return.
  269.         //          This is not an error. The caller should deal
  270.         //          with this.
  271.         //
  272.         //          Otherwise, we read UP TO len bytes.
  273.         //
  274.         //          Note that efficiency and high transfer rates will
  275.         //          require len to be much greater than 1!!!
  276.         //
  277.         //          Note that if len == -1, serial.device will build
  278.         //          the result string based on the TermArrays passed
  279.         //          when it was opened.
  280.         //
  281.         if (len >= 0) {
  282.                 if ((errno = SendQuery (iorequest)) == 0)
  283.                         return 0;
  284.                 if (len > errno)
  285.                         len = errno;
  286.         }
  287.  
  288.         iorequest->IOSer.io_Command = CMD_READ;
  289.         iorequest->IOSer.io_Data    = buf;
  290.         iorequest->IOSer.io_Length  = len;
  291.         iorequest->IOSer.io_Flags   = IOF_QUICK;
  292.         iorequest->IOSer.io_Actual  = 0;
  293.  
  294.         BeginIO (iorequest); // in amiga.lib
  295.  
  296.         if ((iorequest->IOSer.io_Flags & IOF_QUICK) == 0) {
  297.  
  298.                 NNTP_in_ip = 1;
  299.  
  300.