program stop at getChar() and readChar() ?
KevinKK
Posts: 27
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);
}
}
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
https://propsideworkspace.googlecode.com/hg/Learn/Simple Libraries/Text Devices/libfdserial/Documentation fdserial Library.html
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);
}
}
You can use rxCheck to implement a time-out version of rxChar. Here's a Spin version of that using rxCheck:
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
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.
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):