Serial port

Discussion in 'C Programming' started by BartC, Jun 2, 2013.

  1. BartC

    BartC Guest

    I'd been looking for a standalone Windows (7) executable to monitor a serial
    port, but thought I'd try the following code, and it worked!

    This is not exactly a C problem but perhaps someone might know why fgetc()
    mostly returns -1 (EOF), presumably when no character is ready, rather than
    wait until one is available (as it might do for f==stdin). As it is, it ties
    up the processor, so can't be used seriously.

    #include <stdio.h>
    #include <stdlib.h>

    int main (void) {
    FILE* f;
    int c;

    f=fopen("com3","r");

    if (!f) exit(0);

    while (1) {
    c=fgetc(f);
    if (c!=EOF) printf("%c",c);
    }
    }

    --
    Bartc
    BartC, Jun 2, 2013
    #1
    1. Advertising

  2. BartC

    James Kuyper Guest

    On 06/02/2013 08:31 AM, BartC wrote:
    > I'd been looking for a standalone Windows (7) executable to monitor a serial
    > port, but thought I'd try the following code, and it worked!
    >
    > This is not exactly a C problem ...


    Correct.

    > ... but perhaps someone might know why fgetc()
    > mostly returns -1 (EOF), presumably when no character is ready, rather than
    > wait until one is available (as it might do for f==stdin). ...


    Almost certainly true - but did you really think you were more likely to
    find such a person here, rather than in a forum associated with Windows?
    --
    James Kuyper
    James Kuyper, Jun 2, 2013
    #2
    1. Advertising

  3. In article <dXGqt.12646$4>, BartC <> wrote:
    >I'd been looking for a standalone Windows (7) executable to monitor a serial
    >port, but thought I'd try the following code, and it worked!


    A million or so years ago, I used a program called DSZ to do exactly this.
    It still works to this day.

    I could probably shoot you a binary if you are interested.

    --
    "They shall be attended by boys graced with eternal youth, who to the
    beholder?s eyes will seem like sprinkled pearls. When you gaze upon that
    scene, you will behold a kingdom blissful and glorious."

    --- Qur'an 76:19 ---
    Kenny McCormack, Jun 2, 2013
    #3
  4. BartC

    ddebug Guest

    Try this forum: http://social.msdn.microsoft.com/Forums/en-US/windowsgeneraldevelopmentissues/threads

    My guess is, the serial port should be opened in "binary" mode:
    fopen("com3","rb")

    -- dd

    On Sunday, June 2, 2013 3:31:53 PM UTC+3, Bart wrote:
    > I'd been looking for a standalone Windows (7) executable to monitor a serial
    >
    > port, but thought I'd try the following code, and it worked!
    >
    >
    >
    > This is not exactly a C problem but perhaps someone might know why fgetc()
    >
    > mostly returns -1 (EOF), presumably when no character is ready, rather than
    >
    > wait until one is available (as it might do for f==stdin). As it is, it ties
    >
    > up the processor, so can't be used seriously.
    >
    >
    >
    > #include <stdio.h>
    >
    > #include <stdlib.h>
    >
    >
    >
    > int main (void) {
    >
    > FILE* f;
    >
    > int c;
    >
    >
    >
    > f=fopen("com3","r");
    >
    >
    >
    > if (!f) exit(0);
    >
    >
    >
    > while (1) {
    >
    > c=fgetc(f);
    >
    > if (c!=EOF) printf("%c",c);
    >
    > }
    >
    > }
    >
    >
    >
    > --
    >
    > Bartc
    ddebug, Jun 2, 2013
    #4
  5. BartC

    Les Cargill Guest

    BartC wrote:
    > I'd been looking for a standalone Windows (7) executable to monitor a
    > serial
    > port, but thought I'd try the following code, and it worked!
    >
    > This is not exactly a C problem but perhaps someone might know why fgetc()
    > mostly returns -1 (EOF), presumably when no character is ready, rather than
    > wait until one is available (as it might do for f==stdin). As it is, it
    > ties
    > up the processor, so can't be used seriously.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main (void) {
    > FILE* f;
    > int c;
    >
    > f=fopen("com3","r");
    >
    > if (!f) exit(0);
    >
    > while (1) {
    > c=fgetc(f);
    > if (c!=EOF) printf("%c",c);
    > }
    > }
    >



    I use PuTTY for this purpose:

    http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

    --
    Les Cargill
    Les Cargill, Jun 2, 2013
    #5
  6. Kenny McCormack wrote:
    BartC wrote:
    >>I'd been looking for a standalone Windows (7) executable to monitor a serial
    >>port, but thought I'd try the following code, and it worked!

    >
    >A million or so years ago, I used a program called DSZ to do exactly this.
    >It still works to this day.


    This one works fine on Win 7 and is slightly younger (only about
    600,000 years old.)

    http://www.umonfw.com/ucon/
    --
    Roberto Waltman

    [ Please reply to the group,
    return address is invalid ]
    Roberto Waltman, Jun 14, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rasha
    Replies:
    0
    Views:
    1,688
    Rasha
    Mar 17, 2005
  2. Roberto
    Replies:
    0
    Views:
    494
    Roberto
    Dec 15, 2004
  3. Pom
    Replies:
    2
    Views:
    1,636
    Bas-i
    Jan 31, 2007
  4. Max Kotasek
    Replies:
    4
    Views:
    971
    Max Kotasek
    Apr 9, 2010
  5. msalerno
    Replies:
    3
    Views:
    407
    Ilmari Karonen
    Jul 14, 2005
Loading...

Share This Page