Shop OBEX P1 Docs P2 Docs Learn Events
Need Help with serial interface — Parallax Forums

Need Help with serial interface

ReinhardReinhard Posts: 489
edited 2011-11-27 04:54 in Propeller 1
Hi,
today I tried several demos, any from myself and other from here in the forum, which use getchar, gets, fgets and so on,
which wait for an input from serial line.

All this programs work fine if I use the built in terminal from propeller-load.(!)

But no one works with other terminal programs like parallax terminal , hyperterminal or other usuall terminalprograms.

Yes, I have adjust the port, baudrate and other parameters correct.
Commands like printf, putc, puts works correct.

I spend hours about this problem and maybe now I am blind for this.
Can anybody confirm this, or is it my mistake ?
I see no reason and can not explain this.

best regards,
Reinhard
«1

Comments

  • ReinhardReinhard Posts: 489
    edited 2011-11-23 13:55
    Forget to say I works on Windows XP and my Propeller Platform is PROPSTICK/USB

    Thanks,
    Reinhard
  • David BetzDavid Betz Posts: 14,516
    edited 2011-11-23 14:01
    What happens? Do the characters you type get echoed? One possiblity is that the propgcc library and these terminal programs disagree as to what character indicates and end of line. I think we're expecting a CR character to be sent by the "return" key. That is the way old physical terminals used to work. I'm not sure what the default for Hyperterminal is but I'm pretty sure that the Parallax terminal program is somewhat non-standard.
  • ReinhardReinhard Posts: 489
    edited 2011-11-23 14:06
    Thanks for answer,
    I know this sounds a little bit hmm ... curious.
    for example this snippet :

    gets(Buffer); // see the echo
    i = atoi(Buffer);
    printf("i=%d\n",i); // see no printf !
  • David BetzDavid Betz Posts: 14,516
    edited 2011-11-23 14:10
    Try typing Ctrl-M to end your line rather than the "Return" key and see if it makes any difference.
  • ReinhardReinhard Posts: 489
    edited 2011-11-23 14:10
    more example:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    const char *cmdchars = "+, -, *, /, or q";
    
    
    // size of buffer for getting numbers
    #define NUMSIZE 12
    
    
    int getint(int num)
    {
        static char buff[NUMSIZE];
        printf("Enter %s number: ", (num==1) ? "first ":"second");
        fflush(stdout);
        fgets(buff, NUMSIZE, stdin);
        return atoi(buff);          // strtod works too, but it is bigger
    }
    
    
    int main()
    {
        char ch = 0;        // assign to supress uninitialized warnings
        int v1 = 2;
        int v2 = 3;
    
    
        printf("%d\n", v1+v2);
    
    
        printf("Very Simple Propeller Calculator Demo\n");
        printf("Commands %s then number1 number2.", cmdchars);
        for(;;)
        {
            printf("\nCommand? ");
            fflush(stdout);
    
    
            /* handle input */
            ch = getchar();
            switch(ch)
            {
                case 'q' :
                    return 0;
                case '+' :  // fall through
                case '-' :  // fall through
                case '*' :  // fall through
                case '/' :  // fall through
                    getchar();  // clobber newline
                    putchar('\b');
                    v1 = getint(1);
                    v2 = getint(2);
                    break;
                case '\r' : // fall through
                case '\n' : // fall through
                    ch = 0; // not a valid command
                    break;  // ignore these
                default :
                    ch = 0; // not a valid command
                    printf("Invalid command. Please enter one of these first: %s\n", cmdchars);
                    printf("Then enter 2 numbers for the operation.\n");
                    break;
            }
    
    
            /* print output */
            if(ch)
            {
                printf("%d %c %d = ",v1,ch,v2);
                switch(ch)
                {
                    case '+' :
                        printf("%d",v1+v2);
                        break;
                    case '-' :
                        printf("%d",v1-v2);
                        break;
                    case '*' :
                        printf("%d",v1*v2);
                        break;
                    case '/' :
                        printf("%d",v1/v2);
                        break;
                    default :
                        break;
                }
            }
        }
        return 0;
    }
    

    I take the demo from jazzed, make float to int, for avoid memory problems on my lmm board.

    what I see is the
    printf("Commands %s then number1 number2.", cmdchars);
    and not more ...

    For remember with propeller-load it works ?!
    but not with parallax terminal
  • ReinhardReinhard Posts: 489
    edited 2011-11-23 14:16
    David Betz wrote: »
    Try typing Ctrl-M to end your line rather than the "Return" key and see if it makes any difference.

    No , make not a difference
  • ReinhardReinhard Posts: 489
    edited 2011-11-23 14:31
    What I see with propeller-load:
    D:\myC\propeller_chip\gcc\calc>propeller-load -pcom7 -t -e calc.elf
    Propeller Version 1 on com7
    Writing 31668 bytes to EEPROM.
    Verifying ... Upload OK!
    [ Entering terminal mode. Type ESC or Control-C to exit. ]
    5
    Very Simple Propeller Calculator Demo
    Commands +, -, *, /, or q then number1 number2.
    Command? +
    Enter first number: 1
    Enter second number: 2
    1 + 2 = 3
    Command? q


    What I see with Parallax Terminal:
    Very Simple Propeller Calculator Demo
    Commands +, -, *, /, or q then number1 number2.
    Command? +

    and nothing more ...

    But this is only a example, other demos show the same behaviour (hmm)
  • David BetzDavid Betz Posts: 14,516
    edited 2011-11-23 14:34
    I understand your problem. I am on a Macintosh so I can't duplicate it right away. I'll try to boot up my Windows 7 system later tonight. Sorry you're having trouble! There really isn't anything special about the terminal mode in propeller-load. The only thing I can think of that might cause trouble is, as I suggested, line end characters. I noticed when I launched Hyperterminal on XP that it has a setting to send LF instead of CR when the Return key is pressed. That might cause problems if it is enabled.
  • ReinhardReinhard Posts: 489
    edited 2011-11-23 14:38
    David Betz wrote: »
    I understand your problem. I am on a Macintosh so I can't duplicate it right away. I'll try to boot up my Windows 7 system later tonight. Sorry you're having trouble!

    Thanks for effort, I have no trouble, only wonder :smile:
  • jazzedjazzed Posts: 11,803
    edited 2011-11-23 17:33
    This seems to be a library problem. Will look into it more.

    Thanks Reinhard.
    --Steve
  • frank freedmanfrank freedman Posts: 1,983
    edited 2011-11-23 21:38
    Reinhard wrote: »
    Hi,
    today I tried several demos, any from myself and other from here in the forum, which use getchar, gets, fgets and so on,
    which wait for an input from serial line.

    All this programs work fine if I use the built in terminal from propeller-load.(!)

    But no one works with other terminal programs like parallax terminal , hyperterminal or other usuall terminalprograms.

    Yes, I have adjust the port, baudrate and other parameters correct.
    Commands like printf, putc, puts works correct.

    I spend hours about this problem and maybe now I am blind for this.
    Can anybody confirm this, or is it my mistake ?
    I see no reason and can not explain this.

    best regards,
    Reinhard

    have you run the toggle demo? That works from GtkTerm w/ the settings of 115200, 8n1 none. Fedora Core 16, Fibo will not output anything. Fibo will output using the loader terminal mode.

    Frank
  • ReinhardReinhard Posts: 489
    edited 2011-11-24 01:52
    Hi Frank

    If I remember right, the toggle demos support no serial io.
    So with this the phenomenon can't detect.

    Actualy I have no propeller platform for test here, but I guess the problem can reduced to:

    #include <stdio.h>
    void foo ()
    {
    while(1)
    putchar(getchar());
    }

    with propeller-load you see your input and the echo.
    with other terminal programs ? ? ?

    best regards,
    Reinhard
  • ersmithersmith Posts: 6,100
    edited 2011-11-24 07:30
    I think the problem is that the library is looking for line feed (control-J) as a line terminator, instead of carriage return (control-M). I've tried fixing this, but it appears to be complicated -- the obvious fix in terminal.c doesn't help. I'll look into it some more.

    Eric
  • David BetzDavid Betz Posts: 14,516
    edited 2011-11-24 08:05
    ersmith wrote: »
    I think the problem is that the library is looking for line feed (control-J) as a line terminator, instead of carriage return (control-M). I've tried fixing this, but it appears to be complicated -- the obvious fix in terminal.c doesn't help. I'll look into it some more.

    Eric
    If the terminal mode in propeller-load is sending an LF at the end of a line I should probably change that. I think it is more standard to send CR. I had thought that was what I was doing but I may be wrong.
  • jazzedjazzed Posts: 11,803
    edited 2011-11-24 08:43
    ersmith wrote: »
    I think the problem is that the library is looking for line feed (control-J) as a line terminator, instead of carriage return (control-M). I've tried fixing this, but it appears to be complicated -- the obvious fix in terminal.c doesn't help. I'll look into it some more.

    Eric
    I tried the obvious fix last night too ... no joy. Glad to know it wasn't just me.
    I assume you're taking it. Can you file an issue?

    Thanks,
    --Steve
  • ersmithersmith Posts: 6,100
    edited 2011-11-24 10:02
    jazzed wrote: »
    I tried the obvious fix last night too ... no joy. Glad to know it wasn't just me.
    I assume you're taking it. Can you file an issue?

    I filed an issue, and have checked in a fix that works for me -- it'd be great if some other people could double check this.
    (It turns out a simple fix works, but it has to be in exactly the right place, namely before the character is written into the buffer.)

    Thanks,
    Eric
  • ReinhardReinhard Posts: 489
    edited 2011-11-24 10:14
    Hi

    I have try to reduce the issue in a minimal test:
    with this code the behaviour is reproducible:

    #include <stdio.h>
    unsigned char Buffer[10];
    void main ()
    {

    unsigned char i,j;
    while(1)
    {
    puts("\ngets\n ");
    gets(Buffer);
    printf("%s \n",Buffer);
    }

    }

    works with propeller-load, not with parallax terminal, hyperterminal and zoc (termial emulator)

    Thanks, Reinhard
  • ersmithersmith Posts: 6,100
    edited 2011-11-24 11:55
    Reinhard: if our analysis is correct, pressing CONTROL-J instead of RETURN will work. We will fix the libraries to translate this automatically.
  • ReinhardReinhard Posts: 489
    edited 2011-11-24 12:31
    ersmith wrote: »
    Reinhard: if our analysis is correct, pressing CONTROL-J instead of RETURN will work. We will fix the libraries to translate this automatically.

    Your analysis is correct , with this workaround the stdin works on all tested terminal programs.

    Thanks for this info.

    best regards,
    Reinhard
  • jazzedjazzed Posts: 11,803
    edited 2011-11-24 12:33
    ersmith wrote: »
    I filed an issue, and have checked in a fix that works for me -- it'd be great if some other people could double check this.
    (It turns out a simple fix works, but it has to be in exactly the right place, namely before the character is written into the buffer.)

    Thanks,
    Eric
    That seems to work for input. However ...

    One very annoying thing about some windows terminals is that we have to send \r\n to get a carriage-return/new-line. Just changing \r to \n doesn't really get it because some windows terminals translate things differently than Linux. I think the library has to send both \r and \n so windows stuff will work.

    I've been holding back on a new test distribution because of license stuff. However, I think that if we have a reasonable fix for this, we should post a new test package before I go on my trip.
  • ReinhardReinhard Posts: 489
    edited 2011-11-24 12:46
    Hi,

    now I understand the reason for the command line options -D CR_ON_LF and -D NO_CR_TO_LF which used

    by the Catalina C Compiler.

    best regards,
    Reinhard
  • David BetzDavid Betz Posts: 14,516
    edited 2011-11-24 12:58
    Reinhard wrote: »
    Hi,

    now I understand the reason for the command line options -D CR_ON_LF and -D NO_CR_TO_LF which used

    by the Catalina C Compiler.

    best regards,
    Reinhard
    I think we have a bug in the Windows version of propeller-load that causes this problem. There shouldn't be a need for those command line options. I'm going to look at the problem shortly.
  • David BetzDavid Betz Posts: 14,516
    edited 2011-11-24 13:00
    jazzed wrote: »
    That seems to work for input. However ...

    One very annoying thing about some windows terminals is that we have to send \r\n to get a carriage-return/new-line. Just changing \r to \n doesn't really get it because some windows terminals translate things differently than Linux. I think the library has to send both \r and \n so windows stuff will work.

    I've been holding back on a new test distribution because of license stuff. However, I think that if we have a reasonable fix for this, we should post a new test package before I go on my trip.
    I think all terminal emulators default to a mode where CR and LF are needed because that is the way hardware terminals used to work. I think we should support that model.
  • ersmithersmith Posts: 6,100
    edited 2011-11-24 13:11
    jazzed wrote: »
    That seems to work for input. However ...

    One very annoying thing about some windows terminals is that we have to send \r\n to get a carriage-return/new-line. Just changing \r to \n doesn't really get it because some windows terminals translate things differently than Linux. I think the library has to send both \r and \n so windows stuff will work.

    Good idea. I've checked in a change to make terminal output do this, as well as the fix for the issue Reinhard discovered.

    Eric
  • jazzedjazzed Posts: 11,803
    edited 2011-11-24 15:09
    ersmith wrote: »
    Good idea. I've checked in a change to make terminal output do this, as well as the fix for the issue Reinhard discovered.

    Eric
    We're closer, but PST still doesn't see \r\n after hitting "enter" on the calc.c demo.
    I'm afraid we'll need to add a putbyte ...
    I don't have anymore time to test today - sorry there's a turkey calling me.
    /* convert cr to lf */
    if (cooked && value == '\r') value = '\n';
    ... becomes ...
    /* convert cr to lf */
    if (cooked && value == '\r') {
      putbyte(value);
      value = '\n';
    }
    

    Happy Thanksgiving to all !
  • ersmithersmith Posts: 6,100
    edited 2011-11-24 17:36
    Thanks for the suggested fix, Steve -- I just looked at the output code, and missed the necessity of fixing up CR when echoed on input. Your suggestion is checked in now.

    Have a Happy Thanksgiving!

    Eric
  • David BetzDavid Betz Posts: 14,516
    edited 2011-11-24 18:31
    I'm confused about what is happening with the various terminal emulators. Am I correct that you've discovered that the behavior of propeller-load -t is different on Windows and Linux/Mac? It seems to me that it should work the same in both environments. I'm assuming that you've "solved" this problem by making the library work whether it gets a CR or an LF at the end of a line. Is that correct? I think I should probably make propeller-load behave the same as how most terminal emulators work in their default mode which I would think is to send CR when the Return key is pressed and require both CR and LF at the end of each line received from the Propeller. Do you agree? I know terminal emulators can be configured to behave in other ways but I think matching the default behavior is probably best as long as there is a general consensus as to what constitues default behavior. Any comments?
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2011-11-24 20:05
    I haven't read all of the docs, so this question might already be answered... why does propeller-load even offer a terminal function? It seems like this functionality should simply be offloaded to an actual terminal emulator instead, and just let the loader load programs.
  • jazzedjazzed Posts: 11,803
    edited 2011-11-24 21:08
    The current windows (osint_mingw.c) repository code works reasonably well now with the built in terminal and PST (default preferences with Line Feed unchecked). Tested with Reinhard's simple gets/printf program or calc.c from the other thread (putchar('\b') should be removed there). Some enhancements in loader/src/osint_mingw.c may be possible to match PST.

    @David, I'm not sure what the consensus should be exactly, but it would be really nice if our terminal behavior matched what is expected of the PST default behavior - unless there is proof that PST is wrong. The terminal behaviour not a perfect match with PST yet, but it is certainly workable.

    @Kevin, using the built-in propeller-load terminal is mostly a convenience. It makes certain things easy.
  • David BetzDavid Betz Posts: 14,516
    edited 2011-11-25 04:30
    jazzed wrote: »
    The current windows (osint_mingw.c) repository code works reasonably well now with the built in terminal and PST (default preferences with Line Feed unchecked). Tested with Reinhard's simple gets/printf program or calc.c from the other thread (putchar('\b') should be removed there). Some enhancements in loader/src/osint_mingw.c may be possible to match PST.

    @David, I'm not sure what the consensus should be exactly, but it would be really nice if our terminal behavior matched what is expected of the PST default behavior - unless there is proof that PST is wrong. The terminal behaviour not a perfect match with PST yet, but it is certainly workable.

    @Kevin, using the built-in propeller-load terminal is mostly a convenience. It makes certain things easy.
    It seems to me that I have had trouble with extra blank lines whenever I try to go from PST to a normal terminal emulator. I don't think its default settings match common practice. Maybe we can get Parallax to fix that.
Sign In or Register to comment.