home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / sys / amiga / programm / 12258 < prev    next >
Encoding:
Text File  |  1992-08-12  |  2.1 KB  |  85 lines

  1. Newsgroups: comp.sys.amiga.programmer
  2. Path: sparky!uunet!math.fu-berlin.de!pbinfo2!caro
  3. From: caro@uni-paderborn.de (Carsten Rose)
  4. Subject: Re: Serial Port Source
  5. Message-ID: <1992Aug13.094052.18561@uni-paderborn.de>
  6. Sender: news@uni-paderborn.de (News Uni-Paderborn)
  7. Nntp-Posting-Host: obelix
  8. Organization: Uni-GH Paderborn
  9. X-Newsreader: Tin 1.1 PL4
  10. References: <92222.115411IO20197@MAINE.MAINE.EDU>
  11. Date: Thu, 13 Aug 1992 09:40:52 GMT
  12. Lines: 71
  13.  
  14. IO20197@MAINE.MAINE.EDU () writes:
  15. : HI,
  16. :      I'm very new at this so please bear with me.  I want to use C to open
  17. : the serial port and send/receive data.  Is there somewhere I could pick up
  18. : a source to initialize the port & send/receive bytes?
  19. :                                                            Thanks
  20.  A very simple is way is this solution:
  21.  
  22. #include <stdio.h>
  23.  
  24. char *text = "Only a stupid example";
  25.  
  26. main()
  27. {
  28.     FILE *fp;
  29.     char buffer[256];
  30.  
  31.     if(!(fp=fopen("ser:","r+")))
  32.     {
  33.     perror("Couldn't open SER:");
  34.     exit(1);
  35.     }
  36.  
  37.     if(EOF==(fputs(text,fp)))
  38.     {
  39.     perror("writing to SER: failed");
  40.         fclose(fp);
  41.         exit(1);
  42.     }
  43.  
  44.     if(!(fgets(buffer,256,fp)))
  45.     {
  46.      perror("reading from SER: failed");
  47.      fclose(fp);
  48.      exit(1);
  49.      }
  50.      puts(buffer);
  51.  
  52.      fclose(fp);
  53.      exit(0);
  54.  }
  55.  
  56.  There are more efficient Functions for reading and writing data (fread,fwrite)
  57.  but fputs(),fgets() are easy to use. The fopen() Function opens any File or
  58.  Device. Depending on the second string: "r+" the file is opened for reading
  59.  and/or writing:
  60.  
  61.        r         open for reading
  62.  
  63.        w         truncate or create for writing
  64.  
  65.        a         append: open for writing at end of  file,  or
  66.              create for writing
  67.  
  68.        r+        open for update (reading and writing)
  69.  
  70.        w+        truncate or create for update
  71.  
  72.        a+        append; open or create for update at EOF
  73.  
  74.  If you open "ser:" the system configuration, done in Preferences, is used.
  75.  I'm not sure, alternative you can specify "ser:9600,8,N,1" to skip the 
  76.  preferences. 
  77.  
  78.  Hope I could help.
  79.  
  80.  Ciao
  81.      Carsten
  82.  
  83. Carsten Rose, Germany        | Life. That's what happens while
  84. EMail : caro@uni-paderborn.de    | you're thinking about the future
  85.