20 Replies Latest reply on Jan 20, 2014 7:42 AM by clemzky

    a simple "printf" function

    clemzky

      Hi all,

                  Why all of my "printf" lines have these kind of errors:

      stray "\302" in program

      stray "\250" in program

      stray "\" in program

       

      I deleted the lines and typed again but still the same. I do not think there are some typo error as all the lines with the "printf" have errors.

       

      Thanks,

       

      Clem

        • Re: a simple "printf" function
          bprewit

          Hi, Clem:

           

          These types of errors are typically caused by stray unprintable characters in the source; the compiler objects to them as they don't fit the defined syntax.

           

          It might be helpful if you posted the actual source code and corresponding error messages.

           

          HTH,

            • Re: a simple "printf" function
              clemzky

              //Include standard io

              #include <stdio.h>

              #include <stdlib.h>

               

               

              //Include ArduPi library

              #include "arduPi.h"

               

               

              //Include the Marth library

              #include <math.h>

               

               

               

               

               

               

              //Needed for Serial Communication

              SerialPi Serial;

               

               

              //Needed for accessing GPIO (pinMode, digitalWrite, digitalRead,

              //I2C funcions)

              WirePi Wire;

               

               

              //Needed for SPI

              SPIPi SPI;

               

               

              //Values need for Steinhart-Hart equation

              //and calculating resistance.

              #define TENKRESISTOR 10000 //our 10K resistor

              #define BETA 4000 // This is the Beta Coefficient of your thermistor

              #define THERMISTOR 10000  //The resistance of your thermistor at room temp

              #define ROOMTEMP 298.15 // standard room temperature in Kelvin (25 Cesius).

               

               

              //Number of readings to take

              //these will be averaged out to

              //get a more accurate reading

              //You can increase/decrease this as needed

              #define READINGS 7

               

               

              int main() {

                setup();

                while(1) {

                loop();

                }

                return(0)    ;

              }

               

               

              void setup(void){

                printf(¨Starting up thermometer\n¨);

                Wire.begin();

              }

               

               

               

               

               

               

              void loop(void) {

                float avResistance;

                float resistance;

                int combinedReadings[READINGS];

                byte val0;

                byte val1;

               

               

                //Our temperature variable

                float kelvin;

                float fahrenheit;

                float celcius;

               

               

                int channelReading;

                float analogReadingArduino;

               

               

                /***********************

                ADC mappings

               

               

                Pin Adress

               

               

                0 0xDC

                1 0x9C

                2 0xCC

                3 0x8C

                4 0xAC

                5 0xEC

                6 0xBC

                7 0xFC

                  ****************/

               

               

              //0xDC is our analop 0 pin

              Wire.beginTransmission(8);

              Wire.write(byte(0xDC));

              Wire.endTransmission();

               

               

              /* Grad the two bytes returned from the analog 0 pin, combine them

                and write the value to the combinedReadings array

              */

               

               

              for(int r=0; r<READINGS; r++) {

                Wire.requestFrom(8,2);

                val0 = Wire.read();

                val1 = Wire.read();

                channelReading = int(val0)*16 + int(val1>>4);

                analogReadingArduino = channelReading * 1023 / 4095;

                combinedReadings[r] = analogReadingArduino;

                delay(100);

              }

               

               

              //Grab the average of our 7 readings

              //in order to get a more accurate value avResistance = 0;

              for (int r=0; r<READINGS; r++) {

                avResistance += combinedReadings[r];

              }

              avResistance /= READINGS;

               

               

              /* We can now calculate the resistance of the readings that have come

              * back from analog 0

              */

              avResistance = (1023 / avResistance) - 1;

              avResistance = TENKRESISTOR /avResistance;

              resistance = avResistance / THERMISTOR;

               

               

              //Calculate the temperature in Kelvin

              kelvin = log(resistance);

              kelvin /= BETA;

              kelvin += 1.0 / ROOMTEMP;

              kelvin = 1.0 / kelvin;

              //printf(¨Temperature in K ¨);

              //printf(¨%f \n¨,kelvin);

               

               

              //Convert from Kelvin to Celsius

              celcius = kelvin -= 273.15;

              // printf(¨Temperature in C ¨);

              //printf(¨%f \n¨, celsius);

               

               

              //Convert from Celsius to Fahrenheit

              fahrenheit = (celcius * 1.8) + 32;

              //printf(¨Temperature in F ¨);

              //printf(¨%f \n¨, fahrenheit);

               

               

              //Three second delay before taking our next reading

              delay(3000);

              }

                • Re: a simple "printf" function
                  bprewit

                  Hi, Clem:

                   

                  On line 58 of your program the "quote" marks are encoded as 0xC2 0xA8 (Octal 302 and 250) rather than as 0x22 (the normal ascii quotation mark) .. actually, all of the quotation marks within the printf statements are that way, but as all of the other printf statements are commented out they're ignored by the compiler Interestingly enough the other quote marks were as expected.

                   

                  HTH,

                   

                  Bruce

                    • Re: a simple "printf" function
                      clemzky

                      Hi Bruce,

                                    Could you share how you managed to do that for the sake of the rest of newbies that might be reading this, as this will be very helpful.  What editor do you use?

                      Bruce and Iago, thanks you guys for helping and spending time to answer my questions.

                       

                      Best regards,

                       

                      Clemzky

                        • Re: a simple "printf" function
                          clemzky

                          Hi Iago, Bruce and John,

                                                                Thank you for leading the way, now I can move on, for now I do not think of any Hex Editor, I just downloaded the Eclipse for windows and from there opened up my source code and bingo!! there are special characters, from there I edited the source accordingly and move it to Pi, compiled and works.  If my memory serves me right, there is Eclipse for Pi but not sure, now searching for it to be loaded in the PI as an editor and compiler. Kudos to all of you guys!!!!

                           

                          Best regards,

                           

                          Clemzk

                            • Re: a simple "printf" function
                              iagorubio

                              Clem, you can cross compile in your desktop and then load the code on the pi.

                               

                              As you are using Eclipse, you just need the C plugin.

                               

                              C plugin (CDT) - Eclipse CDT

                               

                              The cross compiler - https://github.com/raspberrypi/tools/archive/master.zip.If you have git or load git on the Eclipse - EGit - you can clone the cross compiler from here https://github.com/raspberrypi/tools

                               

                              And then setup it to use the pi cross compiler on Eclipse  - GuruCoding.com - Raspberry Pi Eclipse Tutorial - Setting Up Cross-Compilation In Eclipse

                               

                              After that you can compile your code for arm in the workstation, and load it on the Pi.

                              1 of 1 people found this helpful
                                • Re: a simple "printf" function
                                  clemzky

                                  Hi Iago,

                                              Thanks again, I will fix that one later, now I just bought another set of Pi and now my objective is to set them head less so that I can use my desktop to access them, followed this RPi VNC Server - eLinux.org but the TightVNC is not working whatever I did, the same website recommended using X11VNC which according to that website is better and more secure and can changed the default port from 5901 but never mentioned how.  Found this Setting-Up Raspberry Pi for Headless Mode with X11VNC | Code Chief's Space and followed now successfully installed X11VNC and now it is working on a default port 5901, searched the internet how to change the default but unsuccessful even though I followed them like putting x11vnc -bg -nevershared -forever -tightfilexfer -usepw rfbport 566 -display

                                   

                                  Thanks,

                                   

                                  Clemzky

                                    • Re: Re: a simple "printf" function
                                      iagorubio

                                      You should open a new thread for this question Clemzky, so people on the list will get a meaningful subject on what is being talked on the thread so people with x11vnc experience can jump in the discussion.

                                       

                                      Right now thy are getting a message saying "A simple printf" when we are talking about headless raspberry. Some people that may be interested or experienced on the topic can miss it.

                                       

                                      I have checked the command line switches for x11vnc and looks like "rfbport" is the right command line option http://www.karlrunge.com/x11vnc/x11vnc_opts.html

                                       

                                      The only thing I don't see right in your x11vnc invocation, is you are not using it as a command line switch with a dash in front of it "-rfbport" .

                                       

                                      Try with: " sudo x11vnc -bg -nevershared -forever -tightfilexfer -usepw -rfbport 566 -display  :0".

                                       

                                      If it doesn't works try the verbose option "-v" to see if the output tells you something about what's going wrong.

                                        • Re: Re: a simple "printf" function
                                          clemzky

                                          Hi Iago,

                                                      Will do but for now time to rest I burnt my 15 hours in front of the computer, using command line will work but when i put it inside the xsession.rc to load the settings during boot up so that i do not have to do it manually, still it goes to the default port 5901.

                                           

                                          Thanks a lot,

                                           

                                           

                                          Clemzky

                                            • Re: Re: a simple "printf" function
                                              iagorubio

                                              Are you using sudo in the command line or using root permissions ?

                                               

                                              xsession.rc will be executed with the permissions of the user running the server and 566 is a privileged port. Only root can open privileged ports.

                                               

                                              If you use a port higher than 1024 does it works ? Only ports below 1024 are privileged, 5901 as example is not a privileged port.

                                              • Re: a simple "printf" function
                                                bprewit

                                                Okay, dirt-simple way to do VNC with a raspberry pi:

                                                 

                                                1. Become root user: sudo su -
                                                2. Use raspi-config to enable desktop login.
                                                3. Install tightvncserver: apt-get install tightvncserver.
                                                4. Edit the file /etc/lightdm/lightdm.conf as follows:
                                                  1. Comment out lines beginning with 'autologin'.
                                                  2. Uncomment the lines in the section [VNCServer]  The server port can be set via the line beginning 'port='; I set it to 5901 to correspond to the VNC standard. Please see below.
                                                  3. Change the "width" and "height" lines to suit.
                                                  4. I changed the line 'depth=8' to 'depth=16', for no particularly good reason.
                                                5. Restart lightdm: /etc/init.d/lightdm restart

                                                This will give you a greeter (login screen) available using a vnc viewer at "ipaddr-or-hostname:x" where "ipaddr-or-hostname" is either the ip address or recognized hostname of your RPi and x is either (port number) or  (5900+x-display-number).

                                                 

                                                IMHO, using the greeter takes care of a whole lot of security issues and more or less allows running VNC on default ports; I've used this technique to provide remote access to Linux systems for many years without any trouble.  Sure, the passwords are sent clear-text but someone would have to REALLY WANT to hack your pi .. YMMV

                                                 

                                                EDIT: added 'sudo su -' as first command ..

                                      • Re: Re: a simple "printf" function
                                        bprewit

                                        Hi, Clem:

                                         

                                        I may not be the right person to answer this question as I'm primarily a Unix guy; I typically use vi to edit program source files.  On Windows, I'd normally use Notepad++ (free).  The Arduino editor/compiler also works in this situation .. it did hit on the problem fairly quickly.

                                         

                                        Regards,

                                         

                                        Bruce

                                         

                                        EDIT: MIsunderstood the "arduPi" part of things .. although the arduino editor DID actually hone on the problem fairly quickly ..

                                    • Re: a simple "printf" function
                                      iagorubio

                                      Right on the spot Bruce.

                                       

                                      That happens a lot when you copy and paste from the web and Word Clem.

                                       

                                      What text editor are you using ?

                                       

                                      I've never seen a developer editor doing that, and on most of them syntax highlighting would be wrong with this code.

                                        • Re: a simple "printf" function
                                          clemzky

                                          Hi Iago,

                                                      I am very new to this platform and I am just following what was written in the book, I typed all of them manually, the book recommended to use Geany but it seems this is the culprit but in its bottom line, the mode = Unix; encoding: UTF-8.  What editor can you suggest please.

                                           

                                          Best regards,

                                           

                                          Clemzky

                                    • Re: a simple "printf" function
                                      rjrpaz

                                      Another possible source of trouble, is terminal character encoding set. Maybe you are using ISO-8859, and program is using UTF-8, or viceversa.

                                      • Re: a simple "printf" function
                                        iagorubio

                                        Most likely, you've got a non-printable character in your code.

                                         

                                        You can see it opening the file in an hex editor.

                                         

                                        Long time ago when I wrote a developer text editor, I had a lot of troubles with weird text people copied from the web, and  then reported problems in how my program managed those chars.

                                         

                                        Also many people using shift+space and shift+ ... combinations inadvertently, ended up with weird non printable characters in their documents.

                                         

                                        To help me debug those I did a tiny executable to dump the offending parts. I'm posting it here but for a whole document the hex editor is much better.

                                         

                                        #include <stdio.h>

                                        #include <string.h>

                                        #include <errno.h>

                                         

                                        int main (int argc, char **argv)

                                        {

                                                FILE *f;

                                                int c, i=1;

                                         

                                                if( argc != 2 ){

                                                        printf("Ussage: %s file\n", argv[0]);

                                                        return 0;

                                                }

                                         

                                                f = fopen(argv[1],"r");

                                         

                                                if( f == NULL ){

                                                        printf("%s\n", strerror(errno));

                                                        return -1;

                                                }

                                                printf("%d: ",i);

                                                while((c = fgetc(f))!= EOF){

                                                        if( c == '\n') printf("\n%d:", ++i);

                                                        else printf( "'%c'=>'%d' ", c, c);

                                                }

                                                printf("\n");

                                                return 0;

                                        }

                                         

                                        It just dumps the decimal value of the character and the printable representation of that character.

                                         

                                        As example the output for a file containing the word "test", it would be:

                                         

                                        1: 't'=>'116' 'e'=>'101' 's'=>'115' 't'=>'116'

                                         

                                        It may help or not, and will for sure fail with UTF-8 or any multi byte encoding, but most source code is ascii.

                                         

                                        It helped me when trying to deal with those pesky non printable characters.

                                         

                                        Use it at your convenience.

                                        1 of 1 people found this helpful