parallaxinc / Simple-Libraries

Contents of the SimpleIDE workspace folder and its Parallax Learn Simple Libraries subfolder.
http://learn.parallax.com/propeller-c-set-simpleide/update-your-learn-folder
21 stars 21 forks source link

readDec and dscan don't work correctly with serial.h #167

Open AndyLindsay opened 6 years ago

AndyLindsay commented 6 years ago

It looks like the culprit is in the simpletext library's _safe_gets function. The hardware echo feature that's useful for the terminal with half duplex serial ends up causing trouble for other device communication. Before removing or making hardware echo a configurable feature, existing serial and fdserial apps will need to be tested. This would also have to be coordinated with a blocklyprop terminal update.

Below is a block comment that I would recommend adding. Alternately, it could be something that only gets executed when rxPin == 31 and txPin == 30, and maybe some mode conditions.

#include <ctype.h>
#include "simpletext.h"

char* _safe_gets(text_t *text, char* origBuf, int count)
{
  char* buf = origBuf;
  while (count-- > 0)
  {
      int ch = text->rxChar(text);

      /*

      if (ch == 8 || ch == 127)
      {
          if (buf > origBuf)
          {
              text->txChar(text, '\010');
              text->txChar(text, ' ');
              text->txChar(text, '\010');
              count += 1;
              buf--;
          }
          count += 1;
          continue;
      }

      text->txChar(text, ch);

      if (ch == '\r')
          text->txChar(text, '\n');

      */   

      if (ch == '\r' || ch == '\n')
          break;

      *(buf++) = ch;
  }
  *buf = 0;

  return (origBuf);
}

Here is some test code that fails with the existing library, and executes correctly when the echo code is commented out.

#include "simpletools.h"
#include "serial.h"
#include "fdserial.h"

serial *msgOut;
serial *msgIn;

char s[10];

int val;

int c;

void sendVals();

int main()
{
  msgIn = serial_open(3, 4, 0, 9600);
  pause(100);
  cog_run(sendVals, 256);
  while(1)
  {
    //val = readDec(msgIn);
    dscan(msgIn, "%d", &val);
    print("val = %d\r", val);
  }
}

void sendVals()
{
  msgOut = serial_open(4, 3, 0, 9600);
  int t = CNT;
  int dt = CLKFREQ/10;
  int n = 0;
  while(1)
  {
    waitcnt(t += dt);
    writeDec(msgOut, n);
    writeChar(msgOut, '\r');
    n++;
    //writeChar(msgOut, '\n');
    //writeChar(msgOut, ' ');
  }
}