Shop OBEX P1 Docs P2 Docs Learn Events
program stop at getChar() and readChar() ? — Parallax Forums

program stop at getChar() and readChar() ?

KevinKKKevinKK Posts: 27
edited 2013-09-09 17:22 in Propeller 1
Here is a simple code of my C program :
I don't want my program to stop at the line getChar() . System will stop at this line and wait until the keyboard input a character.
I also test the command readChar() and have the same result.
The same situation in BASIC Stamp, Spin code or Arduino has different objects to solve easily.
Can anyone tell me how to do ?


#include "simpletools.h" // Include simple tools
#include "servo.h"
#include "ping.h"
void adder(void *par); // Forward declaration

static volatile int t, cmDist; // Global vars for cogs to share
unsigned int stack[40 + 25];

int main() // Main function
{
int cmd ;
int cog = cogstart(&adder, NULL, stack, sizeof(stack));

while(1){
print("cmDist = %d\n", cmDist);
cmd = getChar();
servo_start();
switch (cmd) {
case 48 :
servo_angle(16, 900); // P16 servo to 0 degrees
pause(100);
break;
case 49 :
servo_angle(16, 1800); // P16 servo to 0 degrees
pause(100);
break;
case 50 :
servo_angle(16, 900); // P16 servo to 0 degrees
pause(100);
break;
case 51 :
servo_angle(16, 900); // P16 servo to 0 degrees
pause(100);
break;
case 52 : // stop servo
servo_stop();
break;
}
}
}

void adder(void *par) // adder keeps going on its own
{
while(1) // Endless loop
{
cmDist = ping_cm(15); // Get cm distance from Ping)))

pause(200);
}
}

Comments

  • KevinKKKevinKK Posts: 27
    edited 2013-09-09 06:08
    I tried the functions of fdserial but still failed. I just want to write a function like "Serin" command in BASIC Stamp which can set a timeout limit while waiting for a keyboard input.
    No matter how I use fdserial_rxCheck or fdserial_rxReady, the program will halt at the fdserial_rxChar(RCV.

    int main() // Main function
    {
    int cmd ;
    int cog = cogstart(&adder, NULL, stack, sizeof(stack));
    fdserial *RCV = fdserial_open(31, 30, 0, 115200);
    //do
    while(1){
    print("cmDist = %d\n", cmDist);
    //scanf("%c",&cmd);

    if (fdserial_rxCheck(RCV)!=0) {
    cmd = fdserial_rxChar(RCV);
    servo_start();
    print("xxxxxxxxxxxxxxxx");
    print("收到cmd = %c \n", cmd);
    switch (cmd) {
    case 48 :
    //print ("比對出來cmd = 48 \n");
    servo_angle(16, 900); // P16 servo to 0 degrees
    pause(100);
    //servo_stop(); // servo_stop()
    break;
    case 49 :
    //print ("比對出來cmd = 49 \n");
    servo_angle(16, 1800); // P16 servo to 0 degrees
    pause(100);
    //servo_stop();
    break;
    case 50 :
    //print ("比對出來cmd = 50 \n");
    servo_angle(16, 900); // P16 servo to 0 degrees
    pause(100);
    //servo_stop();
    break;
    case 51 :
    //print ("比對出來cmd = 51 \n");
    servo_angle(16, 900); // P16 servo to 0 degrees
    pause(100);
    //servo_stop();
    break;
    case 52 : // stop servo
    //print ("比對出來cmd = 52 \n");
    servo_stop();
    break;
    } // switch loop
    } // if-loop close

    } //while ( cmd != 57) ;
    }

    void adder(void *par) // adder keeps going on its own
    {
    while(1) // Endless loop
    {
    cmDist = ping_cm(15); // Get cm distance from Ping)))

    pause(200);
    }
    }
  • Mike GreenMike Green Posts: 23,101
    edited 2013-09-09 06:49
    You're not using rxCheck correctly. rxCheck works like rxChar except that it returns -1 if there's no character available. If there is a character available, rxCheck returns the value of the character and removes the character from the buffer. You use rxCheck instead of rxChar and use the character value of -1 as a "do nothing" case.

    You can use rxCheck to implement a time-out version of rxChar. Here's a Spin version of that using rxCheck:
    PUB rxtime(ms) : rxbyte | t
    
    '' Wait ms milliseconds for a byte to be received
    '' returns -1 if no byte received, $00..$FF if byte
    
      t := cnt
      repeat until (rxbyte := rxcheck) => 0 or (cnt - t) / (clkfreq / 1000) > ms
    
  • tdlivingstdlivings Posts: 437
    edited 2013-09-09 08:19
    To do it the way you have it, you need to use rxReady which only tells you if there is a char there
    but does not take it out of the buffer.
    When you do the following
    if (fdserial_rxCheck(RCV)!=0) {
    cmd = fdserial_rxChar(RCV);

    Calling rxCheck took the char but you do not save it to use.
    Then cmd=fdserial_rxChar see's an empty receive buffer and waits
    for a char.

    Think of rxReady as characters available

    while(rxReady) {
    cmd = rxChar()
    Execute Commands
    }

    Tom

  • KevinKKKevinKK Posts: 27
    edited 2013-09-09 17:02
    Thanks for your suggestions. But it just can't work correctly.
    I just want my Activity Bot can read the PING distance and be controlled by the keyboard from serial interface in the same time. In BASIC Stamp, I can use the SERIN with timeout setting to complete this. How would that be so difficult for Propeller ? (in C)

    ---- I tried the way tdlivings told me above
    int main() // Main function
    {
    int cmd ;
    fdserial *RCV = fdserial_open(30, 31, 0, 115200); <
    is this line correctly ? rx and tx should be crossed ?

    while (fdserial_rxReady(RCV)){

    print("aaaaaaaaaaaaaaaaaaaaaaaaa\n");
    cmd = fdserial_rxCheck(RCV);
    print("cmd = %d\n", cmd);
    }

    print("bbbbbbbbbbbbbbbbbbbbbb\n");
    }
    But the result is the program will not go inside the while-loop.
  • kuronekokuroneko Posts: 3,623
    edited 2013-09-09 17:22
    The rx pin is 31, tx is 30. Why do you think they need to be crossed?

    As for never entering the while loop, if there is no character at the time rxReady is executed you never will. After that you do a final print call and then the program stops. You could wrap the whole thing into an endless loop like this (maybe add a delay somewhere):
    for (;;) {
        while (fdserial_rxReady(RCV)) {
          print("aaaaaaaaaaaaaaaaaaaaaaaaa\n");
          cmd = fdserial_rxCheck(RCV);
          print("cmd = %d\n", cmd);
        }
        print("bbbbbbbbbbbbbbbbbbbbbb\n"); 
      }
    
Sign In or Register to comment.