Shop OBEX P1 Docs P2 Docs Learn Events
SimpleIDE question: terminal input not buffered? (SOLVED) — Parallax Forums

SimpleIDE question: terminal input not buffered? (SOLVED)

mparkmpark Posts: 1,305
edited 2020-07-13 20:31 in Propeller 1
The little Spin program below echoes what I type but very slowly: one character per second. If I type many characters (e.g., HELLO) in less than a second, the characters are buffered and over the next five seconds the program prints H... E... L... L... O, one character per second.
_clkmode = xtal1+pll16x
_clkfreq = 80_000_000

obj
term: "fullduplexserial"

pub Main | ch
  term.start(31, 30, 0, 115200)
  repeat
    waitcnt(clkfreq + cnt)
    ch := term.rx
    term.tx(ch)
    term.tx(13)
Now I'm trying to do the same thing in C, but I obviously don't know what I'm doing. Whenever the following C program pauses for one second, it just ignores anything I type during that time. If I type EXCELSIOR, the program might print L or S or whatever I happen to type after the pause expires.
#include "simpletools.h"                      // Include simple tools

int main()                                    // Main function
{
  while(1)
  {
    pause(1000);
    char ch = getChar();
    putChar(ch);
    putChar('\n');
  }  
}
How do I do in C what I did in Spin?

Comments

  • Right, by default the library loads half duplex serial object so no buffering is done.

    In your code you are using the full duplex object and here is the same in C.
    #include "simpletools.h"
    #include "fdserial.h"
    
    #define RX 31
    #define TX 30
    fdserial *fd;
    char ch;
    
    
    int main()
    {
      fd = fdserial_open( RX, TX, FDSERIAL_MODE_NONE, 115200);
    
     
      while(1)
      {
        pause(1000);
        ch = fdserial_rxChar(fd);
        fdserial_txChar(fd, ch);
        fdserial_txChar(fd, '\r');
      }
    }
    

    Mike

    I changed the newline character to a return character so it moves down the page.
  • mparkmpark Posts: 1,305
    Ah, the penny drops! Thank you, @iseries!

    Now, since I already have code using get/putXXX, can I replace the default object, like so?
    #include "simpletools.h"                      // Include simple tools
    #include "fdserial.h"
    
    #define RX 31
    #define TX 30
    extern text_t * dport_ptr;
    char ch;
    
    int main()
    {
      serial_close(dport_ptr);
      dport_ptr = fdserial_open( RX, TX, FDSERIAL_MODE_NONE, 115200);
    
     
      while(1)
      {
        pause(1000);
        ch = getChar();
        putChar(ch);
        putChar('\n');
      }
    }
    
    Or, to save space, is there a way to not load the half-duplex object at all and just use full-duplex from the get-go?

    Also, I didn't understand your remark re newline vs return. They both behave the same in SimpleIDE Terminal, afaict?

  • When I use the simple terminal the new line just erases the character output. If I use the return character it advances the cursor to the next line preserving the previous character.

    You don't need to close simple serial because the full duplex version see you using pins 31, 30 and will automatically close it for you.

    Since the simple serial is part of the startup code I guess there is no way to remove it from the code.

    Mike
  • mparkmpark Posts: 1,305
    Thanks again, @iseries!

Sign In or Register to comment.