home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.amiga.programmer
- Path: sparky!uunet!math.fu-berlin.de!pbinfo2!caro
- From: caro@uni-paderborn.de (Carsten Rose)
- Subject: Re: Serial Port Source
- Message-ID: <1992Aug13.094052.18561@uni-paderborn.de>
- Sender: news@uni-paderborn.de (News Uni-Paderborn)
- Nntp-Posting-Host: obelix
- Organization: Uni-GH Paderborn
- X-Newsreader: Tin 1.1 PL4
- References: <92222.115411IO20197@MAINE.MAINE.EDU>
- Date: Thu, 13 Aug 1992 09:40:52 GMT
- Lines: 71
-
- IO20197@MAINE.MAINE.EDU () writes:
- : HI,
- : I'm very new at this so please bear with me. I want to use C to open
- : the serial port and send/receive data. Is there somewhere I could pick up
- : a source to initialize the port & send/receive bytes?
- : Thanks
- A very simple is way is this solution:
-
- #include <stdio.h>
-
- char *text = "Only a stupid example";
-
- main()
- {
- FILE *fp;
- char buffer[256];
-
- if(!(fp=fopen("ser:","r+")))
- {
- perror("Couldn't open SER:");
- exit(1);
- }
-
- if(EOF==(fputs(text,fp)))
- {
- perror("writing to SER: failed");
- fclose(fp);
- exit(1);
- }
-
- if(!(fgets(buffer,256,fp)))
- {
- perror("reading from SER: failed");
- fclose(fp);
- exit(1);
- }
- puts(buffer);
-
- fclose(fp);
- exit(0);
- }
-
- There are more efficient Functions for reading and writing data (fread,fwrite)
- but fputs(),fgets() are easy to use. The fopen() Function opens any File or
- Device. Depending on the second string: "r+" the file is opened for reading
- and/or writing:
-
- r open for reading
-
- w truncate or create for writing
-
- a append: open for writing at end of file, or
- create for writing
-
- r+ open for update (reading and writing)
-
- w+ truncate or create for update
-
- a+ append; open or create for update at EOF
-
- If you open "ser:" the system configuration, done in Preferences, is used.
- I'm not sure, alternative you can specify "ser:9600,8,N,1" to skip the
- preferences.
-
- Hope I could help.
-
- Ciao
- Carsten
-
- Carsten Rose, Germany | Life. That's what happens while
- EMail : caro@uni-paderborn.de | you're thinking about the future
-