Shop OBEX P1 Docs P2 Docs Learn Events
Terminal Serial input from a human typing at a keyboard. — Parallax Forums

Terminal Serial input from a human typing at a keyboard.

Duane C. JohnsonDuane C. Johnson Posts: 955
edited 2014-01-01 19:36 in Propeller 1
Hi guys;
I'm just getting started with PropellerGCC C.
I've been testing code snippets on a clone of the PropStick, thanks to Andrew William's M44D40+ .
So much easier than my home brew PropSticks. Thanks Andrew.

My problem is how do I communicate with the Prop through the serial terminal.
I have the printf() output to the terminal working OK.
However, the scanf() input from the terminal doesn't work for me and I don't know why?
Here is the code snippit I'm testing:
#include "stdio.h"
#include "float.h"
#include "math.h"
#include "simpletools.h"

float main() 
{
  char str [80];
  float DEG, D2R, SIN, COS, TAN;
  DEG = 12.345678;             // Preload a float angle in degrees for test.
  pause(250);                  // Needed to allow the terminal to get started.
  printf("Enter an angle in degrees?\n\a");
  for (int n = 1; n <= 4; n++) // try it 4 times
  {
    //scanf("%f",DEG);         // I enteres 30 or 30.0 or anything else 
                               // yet DEG contains 12.345678 when done.
    scanf("%f",&DEG);          // I enteres 30 or 30.0 or anything else 
                               // yet DEG contains 0.0 when done.
                               // so something happened to DEG but what?
    //scanf("%f*",DEG);        // I enteres 30 or 30.0 or anything else 
                               // yet DEG contains 12.345678 when done.
    //scanf("%f*",&DEG);       // I enteres 30 or 30.0 or anything else 
                               // yet DEG contains 0.0 when done.
                               // so something happened to DEG but what?
    // Do some float calculations 
    D2R = 180.0 / PI;
    SIN = sin(DEG / D2R);
    COS = cos(DEG / D2R);
    TAN = tan(DEG / D2R);
    printf("%d  DEG = %f  SIN = %1f  COS = %f  TAN = %f\n\a",n , DEG, SIN, COS, TAN);
  }
  printf("Done.\n\a");
}
The documentation I'm using is here:
scanf

So what am I doing wrong?

Duane J

Comments

  • David BetzDavid Betz Posts: 14,516
    edited 2014-01-01 08:20
    I ran this version and it worked for me. Your main problem is a missing "&" in front of "DEG" in your call to scanf. The remaining changes were do to the fact that I didn't use SimpleIDE to build it.
    #include "stdio.h"
    #include "float.h"
    #include "math.h"
    //#include "simpletools.h"
    
    #define  PI 3.14159
    
    float main() 
    {
      char str [80];
      float DEG, D2R, SIN, COS, TAN;
      DEG = 12.345678;             // Preload a float angle in degrees for test.
    //  pause(250);                  // Needed to allow the terminal to get started.
      printf("Enter an angle in degrees?\n\a");
      for (int n = 1; n <= 4; n++) // try it 4 times
      {
        scanf("%f",&DEG);           // I enteres 30 or 30.0 or anything else 
                                   // yet DEG contains 12.345678 when done.
        //scanf("%f",&DEG);        // I enteres 30 or 30.0 or anything else 
                                   // yet DEG contains 0.0 when done.
                                   // so something happened to DEG but what?
        //scanf("%f*",DEG);        // I enteres 30 or 30.0 or anything else 
                                   // yet DEG contains 12.345678 when done.
        //scanf("%f*",&DEG);       // I enteres 30 or 30.0 or anything else 
                                   // yet DEG contains 0.0 when done.
                                   // so something happened to DEG but what?
        // Do some float calculations 
        D2R = 180.0 / PI;
        SIN = sin(DEG / D2R);
        COS = cos(DEG / D2R);
        TAN = tan(DEG / D2R);
        printf("%d  DEG = %f  SIN = %1f  COS = %f  TAN = %f\n\a",n , DEG, SIN, COS, TAN);
      }
      printf("Done.\n\a");
    }
    

    I used this command line to build it:
    propeller-elf-gcc -Os -mcmm -std=c99 -o test.elf test.c -lm
    
    Here is the output I got:
    david-betzs-macbook-pro:test dbetz$ propeller-load test.elf -r -t
    Propeller Version 1 on /dev/cu.usbserial-A8004ILf
    Loading test.elf to hub memory
    27624 bytes sent                  
    Verifying RAM ... OK
    [ Entering terminal mode. Type ESC or Control-C to exit. ]
    Enter an angle in degrees?
    0
    1  DEG = 0.000000  SIN = 0.000000  COS = 1.000000  TAN = 0.000000
    30
    2  DEG = 30.000000  SIN = 0.500000  COS = 0.866026  TAN = 0.577350
    45
    3  DEG = 45.000000  SIN = 0.707106  COS = 0.707107  TAN = 0.999999
    90
    4  DEG = 90.000000  SIN = 1.000000  COS = 0.000001  TAN = 721084.375000
    Done.
    
  • David BetzDavid Betz Posts: 14,516
    edited 2014-01-01 08:29
    In rereading your code I notice that you did try scanf with the ampersand in front of "DEG". I'm not sure why that didn't work for you.
  • jazzedjazzed Posts: 11,803
    edited 2014-01-01 09:27
    If you use standard functions like printf and scanf with float, you must link the math library -lm.

    This is one of the primary reasons for using simpletext.h or simpletools.h - no immediate failures.
    The biggest reason of course is so that you'll have room for other code :) without more surprizes.

    Also, please don't use "float main()", use "int main(void)" instead.

    Please go through Andy's tutorials.
  • David BetzDavid Betz Posts: 14,516
    edited 2014-01-01 09:31
    jazzed wrote: »
    If you use standard functions like printf and scanf with float, you must link the math library -lm.

    This is one of the primary reasons for using simpletext.h or simpletools.h - no immediate failures.
    The biggest reason of course is so that you'll have room for other code :) without more surprizes.

    Also, please don't use "float main()", use "int main(void)" instead.

    Please go through Andy's tutorials.
    I didn't even notice the "float main(void)" problem!

    By the way, why do these posted examples always seem to use "stdio.h" instead of <stdio.h>? Is this something that the Parallax tutorials suggest? If so, why?
  • jazzedjazzed Posts: 11,803
    edited 2014-01-01 09:35
    David Betz wrote: »
    I didn't even notice the "float main(void)" problem!

    By the way, why do these posted examples always seem to use "stdio.h" instead of <stdio.h>? Is this something that the Parallax tutorials suggest? If so, why?
    No idea. Andy uses #include <stdio.h> in simpletools.h .
  • David BetzDavid Betz Posts: 14,516
    edited 2014-01-01 09:39
    jazzed wrote: »
    No idea. Andy uses #include <stdio.h> in simpletools.h .
    That's good to hear.
  • Duane C. JohnsonDuane C. Johnson Posts: 955
    edited 2014-01-01 12:46
    Ok guys, I have attempted to implement your suggestions.

    I added angle brackets "<>" to stdio.h
    Added the define PI but I know the PI didn't need defining.

    I am using "SimpleIDE 0.9.45"
    #include <stdio.h>
    #include "float.h"
    #include "math.h"
    #include "simpletools.h"
    
    #define  PI 3.14159            // OK, I added this define although
                                   // PI by itself seems to work OK.
    
    // propeller-elf-gcc -Os -mcmm -std=c99 -o test.elf test.c -lm
                                   // I don't know where to put this?
                                   // There is a place under compiler
                                   // called "Other Compiler Options"
                                   // but when I put it there SimpleIDE
                                   // fails.
    
    int main(void) 
    {
      char str [80];
      float DEG, D2R, SIN, COS, TAN;
      DEG = 12.345678;             // Preload a float angle in degrees for testing.
      pause(250);                  // Needed to allow the terminal to get started.
      printf("Enter an angle in degrees?\n\a");
      for (int n = 1; n <= 4; n++) // try it 4 times
      {
        scanf("%f",&DEG);          // I enteres 30 or 30.0 or anything else 
                                   // yet DEG contains 0.0 when done.
                                   // So something happened to DEG but what?
                                   // DEG now containd zero.
        // Do some float calculations 
        D2R = 180.0 / PI;
        SIN = sin(DEG / D2R);
        COS = cos(DEG / D2R);
        TAN = tan(DEG / D2R);
        printf("%d  DEG = %f  SIN = %1f  COS = %f  TAN = %f\n\a",n , DEG, SIN, COS, TAN);
      }
      printf("Done.\n\a");
    }
    
    This is the terminal output.
    Enter an angle in degrees?
    30.0
    1  DEG = 0.000000  SIN = 0.000000  COS = 1.000000  TAN = 0.000000
    15.0
    2  DEG = 0.000000  SIN = 0.000000  COS = 1.000000  TAN = 0.000000
    123
    3  DEG = 0.000000  SIN = 0.000000  COS = 1.000000  TAN = 0.000000
    123.0
    4  DEG = 0.000000  SIN = 0.000000  COS = 1.000000  TAN = 0.000000
    Done.
    
    This is in the "Build Status" box
    It apparently compiled correctly.
    Project Directory: C:/Users/Shirley/Documents/SimpleIDE/My Projects/
    
    propeller-elf-gcc.exe -v GCC 4.6.1 (propellergcc_v1_0_0_2162)
    propeller-elf-gcc.exe -I . -L . -I C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libfdserial -L C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libfdserial/cmm/ -I C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libsimpletext -L C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libsimpletext/cmm/ -I C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools -L C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools/cmm/ -I C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c -L C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c/cmm/ -o cmm/Float_Tests_1.elf -Os -mcmm -m32bit-doubles -fno-exceptions -std=c99 Float_Tests_1.c -lm -lfdserial -lsimpletext -lsimpletools -lsimplei2c -lm -lfdserial -lsimpletext -lsimpletools -lm -lfdserial -lsimpletext -lm -lfdserial -lm
    Float_Tests_1.c:36:0: warning: "PI" redefined [enabled by default]
    C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools/simpletools.h:118:0: note: this is the location of the previous definition
    propeller-load -s cmm/Float_Tests_1.elf
    propeller-elf-objdump -h cmm/Float_Tests_1.elf
    Done. Build Succeeded!
    
    propeller-load.exe -Dreset=dtr -I C:/Program Files (x86)/SimpleIDE/bin/../propeller-gcc/propeller-load/ cmm/Float_Tests_1.elf -r -p COM4
    Propeller Version 1 on COM4
    Loading cmm/Float_Tests_1.elf to hub memory
    
    24488 bytes sent
    
    Verifying RAM ... 
    OK
    
    In "Project Options" I have
    "PROPSTICK" although "GENERIC" and "EEPROM" do the same thing.
    C++
    CMM Main RAM Compact
    -Os Size

    in "Compiler" the "32bit Double" box is checked and
    -std=c99
    is already in the "Other Compiler Options".

    in "Linker" the "MATH lib" is checked.
    I added
    -lm
    in the "Other Linker Options"

    Where do I put the rest of the suggested options?
    propeller-elf-gcc -Os -mcmm -std=c99 -o test.elf test.c -lm
    Maybe they are already there in the "SimpleIDE Properties".
    GCC Compiler "C:/Program Files (x86)/SimpleIDE/bin/../propeller-gcc/bin/propeller-elf-gcc.exe"
    Library Folder "C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries"
    Workspace Folder "C:/Users/Shirley/Documents/SimpleIDE/"

    Where do I find "Andy's tutorials" ?

    Duane J
  • David BetzDavid Betz Posts: 14,516
    edited 2014-01-01 13:56
    Ok guys, I have attempted to implement your suggestions.

    I added angle brackets "<>" to stdio.h
    Added the define PI but I know the PI didn't need defining.

    I am using "SimpleIDE 0.9.45"
    #include <stdio.h>
    #include "float.h"
    #include "math.h"
    #include "simpletools.h"
    
    #define  PI 3.14159            // OK, I added this define although
                                   // PI by itself seems to work OK.
    
    // propeller-elf-gcc -Os -mcmm -std=c99 -o test.elf test.c -lm
                                   // I don't know where to put this?
                                   // There is a place under compiler
                                   // called "Other Compiler Options"
                                   // but when I put it there SimpleIDE
                                   // fails.
    
    int main(void) 
    {
      char str [80];
      float DEG, D2R, SIN, COS, TAN;
      DEG = 12.345678;             // Preload a float angle in degrees for testing.
      pause(250);                  // Needed to allow the terminal to get started.
      printf("Enter an angle in degrees?\n\a");
      for (int n = 1; n <= 4; n++) // try it 4 times
      {
        scanf("%f",&DEG);          // I enteres 30 or 30.0 or anything else 
                                   // yet DEG contains 0.0 when done.
                                   // So something happened to DEG but what?
                                   // DEG now containd zero.
        // Do some float calculations 
        D2R = 180.0 / PI;
        SIN = sin(DEG / D2R);
        COS = cos(DEG / D2R);
        TAN = tan(DEG / D2R);
        printf("%d  DEG = %f  SIN = %1f  COS = %f  TAN = %f\n\a",n , DEG, SIN, COS, TAN);
      }
      printf("Done.\n\a");
    }
    
    This is the terminal output.
    Enter an angle in degrees?
    30.0
    1  DEG = 0.000000  SIN = 0.000000  COS = 1.000000  TAN = 0.000000
    15.0
    2  DEG = 0.000000  SIN = 0.000000  COS = 1.000000  TAN = 0.000000
    123
    3  DEG = 0.000000  SIN = 0.000000  COS = 1.000000  TAN = 0.000000
    123.0
    4  DEG = 0.000000  SIN = 0.000000  COS = 1.000000  TAN = 0.000000
    Done.
    
    This is in the "Build Status" box
    It apparently compiled correctly.
    Project Directory: C:/Users/Shirley/Documents/SimpleIDE/My Projects/
    
    propeller-elf-gcc.exe -v GCC 4.6.1 (propellergcc_v1_0_0_2162)
    propeller-elf-gcc.exe -I . -L . -I C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libfdserial -L C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libfdserial/cmm/ -I C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libsimpletext -L C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libsimpletext/cmm/ -I C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools -L C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools/cmm/ -I C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c -L C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c/cmm/ -o cmm/Float_Tests_1.elf -Os -mcmm -m32bit-doubles -fno-exceptions -std=c99 Float_Tests_1.c -lm -lfdserial -lsimpletext -lsimpletools -lsimplei2c -lm -lfdserial -lsimpletext -lsimpletools -lm -lfdserial -lsimpletext -lm -lfdserial -lm
    Float_Tests_1.c:36:0: warning: "PI" redefined [enabled by default]
    C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools/simpletools.h:118:0: note: this is the location of the previous definition
    propeller-load -s cmm/Float_Tests_1.elf
    propeller-elf-objdump -h cmm/Float_Tests_1.elf
    Done. Build Succeeded!
    
    propeller-load.exe -Dreset=dtr -I C:/Program Files (x86)/SimpleIDE/bin/../propeller-gcc/propeller-load/ cmm/Float_Tests_1.elf -r -p COM4
    Propeller Version 1 on COM4
    Loading cmm/Float_Tests_1.elf to hub memory
    
    24488 bytes sent
    
    Verifying RAM ... 
    OK
    
    In "Project Options" I have
    "PROPSTICK" although "GENERIC" and "EEPROM" do the same thing.
    C++
    CMM Main RAM Compact
    -Os Size

    in "Compiler" the "32bit Double" box is checked and
    -std=c99
    is already in the "Other Compiler Options".

    in "Linker" the "MATH lib" is checked.
    I added
    -lm
    in the "Other Linker Options"

    Where do I put the rest of the suggested options?
    propeller-elf-gcc -Os -mcmm -std=c99 -o test.elf test.c -lm
    Maybe they are already there in the "SimpleIDE Properties".
    GCC Compiler "C:/Program Files (x86)/SimpleIDE/bin/../propeller-gcc/bin/propeller-elf-gcc.exe"
    Library Folder "C:/Users/Shirley/Documents/SimpleIDE/Learn/Simple Libraries"
    Workspace Folder "C:/Users/Shirley/Documents/SimpleIDE/"

    Where do I find "Andy's tutorials" ?

    Duane J
    I defined PI because I didn't include "simpletools.h". You don't need it.
  • jazzedjazzed Posts: 11,803
    edited 2014-01-01 14:20
    Duane, I'm not sure what's happening with the standard library stuff.

    Looks like we have some work to do there :(

    I use simple library calls and it works fine.
    #include "simpletools.h"
    
    int main(void) 
    {
      char str [80];
      float DEG, D2R, SIN, COS, TAN;
      DEG = 12.345678;             // Preload a float angle in degrees for testing.
      pause(250);                  // Needed to allow the terminal to get started.
      print("Enter an angle in degrees?\n\a");
      for (int n = 1; n <= 4; n++) // try it 4 times
      {
        scan("%f",&DEG);          // I enteres 30 or 30.0 or anything else 
                                   // yet DEG contains 0.0 when done.
                                   // So something happened to DEG but what?
                                   // DEG now containd zero.
        // Do some float calculations 
        D2R = 180.0 / PI;
        SIN = sin(DEG / D2R);
        COS = cos(DEG / D2R);
        TAN = tan(DEG / D2R);
        print("%d  DEG = %f  SIN = %1f  COS = %f  TAN = %f\n\a",n , DEG, SIN, COS, TAN);
      }
      print("Done.\n\a");
    }
    
    Enter an angle in degrees?
    30
    1  DEG = 30.000000  SIN = 0.500000  COS = 0.866025  TAN = 0.577350
    
    Pointers to Andy's tutorials are in my .sig

    Learn Parallax PropellerC: http://learn.parallax.com/propeller-c
  • David BetzDavid Betz Posts: 14,516
    edited 2014-01-01 15:28
    jazzed wrote: »
    Duane, I'm not sure what's happening with the standard library stuff.

    Looks like we have some work to do there :(

    I use simple library calls and it works fine.
    #include "simpletools.h"
    
    int main(void) 
    {
      char str [80];
      float DEG, D2R, SIN, COS, TAN;
      DEG = 12.345678;             // Preload a float angle in degrees for testing.
      pause(250);                  // Needed to allow the terminal to get started.
      print("Enter an angle in degrees?\n\a");
      for (int n = 1; n <= 4; n++) // try it 4 times
      {
        scan("%f",&DEG);          // I enteres 30 or 30.0 or anything else 
                                   // yet DEG contains 0.0 when done.
                                   // So something happened to DEG but what?
                                   // DEG now containd zero.
        // Do some float calculations 
        D2R = 180.0 / PI;
        SIN = sin(DEG / D2R);
        COS = cos(DEG / D2R);
        TAN = tan(DEG / D2R);
        print("%d  DEG = %f  SIN = %1f  COS = %f  TAN = %f\n\a",n , DEG, SIN, COS, TAN);
      }
      print("Done.\n\a");
    }
    
    Enter an angle in degrees?
    30
    1  DEG = 30.000000  SIN = 0.500000  COS = 0.866025  TAN = 0.577350
    
    Pointers to Andy's tutorials are in my .sig

    Learn Parallax PropellerC: http://learn.parallax.com/propeller-c
    Maybe there is a bug in the release_1_0 branch. I'm using the default branch and, as you could see from my previous post, it worked fine for me.
  • Duane C. JohnsonDuane C. Johnson Posts: 955
    edited 2014-01-01 19:15
    Thanks Guys;

    For some reason it started to work.
    And I can't see for the life of me reason why.
    Heck, I can't get it to fail now, except for obvious errors which wont compile anyway.

    What I am doing is attempting to do calculate the bisector angle between 2 spherical angles.
    This is the orientation of a heliostat, a flat mirror, between the sun and a target.

    I have done this in BASIC on a PC but now want to do it on the Prop using C.
    A really good reference is here:
    Calculate distance, bearing and more between Latitude/Longitude points.
    Equations and all.

    I like doing solar trackers.
    In this case I want an SD card that has precalculated solar position data for 30 years or so, at least 10. These bisector calculations are much easier to do than the full equation of time calculations. Just look of the sun and move the mirror. And float precision is plenty accurate. Unlike with the full calculation.

    Anyway, thanks for the assistance. What ever it was %^)

    Duane J
  • David BetzDavid Betz Posts: 14,516
    edited 2014-01-01 19:36
    Thanks Guys;

    For some reason it started to work.
    And I can't see for the life of me reason why.
    Heck, I can't get it to fail now, except for obvious errors which wont compile anyway.

    What I am doing is attempting to do calculate the bisector angle between 2 spherical angles.
    This is the orientation of a heliostat, a flat mirror, between the sun and a target.

    I have done this in BASIC on a PC but now want to do it on the Prop using C.
    A really good reference is here:
    Calculate distance, bearing and more between Latitude/Longitude points.
    Equations and all.

    I like doing solar trackers.
    In this case I want an SD card that has precalculated solar position data for 30 years or so, at least 10. These bisector calculations are much easier to do than the full equation of time calculations. Just look of the sun and move the mirror. And float precision is plenty accurate. Unlike with the full calculation.

    Anyway, thanks for the assistance. What ever it was %^)

    Duane J
    I'm glad you were able to get it working! Congratulations!
Sign In or Register to comment.