0

I have a circuit connected to a thermistor and using system(command) I can get it to read the value into the terminal but I can't get it to send the value properly to my C program.

I'm sending code to the terminal to run, and this code is returning a value in the terminal. I want to take this value into my C program

Here is my code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>       //for Log

float Vo;                     //voltage out
float Ro = 5000;        //value of resistance at 25C in the thermistor
float R ;               //value of fixed resistor
float B = 3977;         //Beta constant
float T0 = 298.15;      //25 degrees C in Kelvin
//float logR2,  R2,T;
//float A = 1.281426510e-03, B = 2.368116050e-04, C = 0.9002008458e-07;  // Ste$


    int main(){
    
    char command[50];
    
    //get the Analog value
    sprintf(command, "cd /sys/bus/iio/devices/iio\\:device0 && cat in_voltage0_raw"$
    Vo = system(command);
    printf("Analog Reading: %d\n", Vo);
return 0;
}

An example output would be

3973

Analog Reading: 4319452

where is the 4319452 coming from? I want it to read 3973

After reading the comments I tried to use fopen in the following way

//#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>       //for Log

float Vo;                       //voltage out
float Ro = 5000;        //value of resistance at 25C in the thermistor
float R ;               //value of fixed resistor
float B = 3977;         //Beta constant
float T0 = 298.15;      //25 degrees C in Kelvin
//float logR2,  R2,T;
//float A = 1.281426510e-03, B = 2.368116050e-04, C = 0.9002008458e-07;  // Steinhart-Hart and Hart Coefficients



int main(){

FILE *fp;
char command[50];
int c;
//get the Analog value
//sprintf(command, "cd /sys/bus/iio/devices/iio\\:device0 && cat in_voltage0_raw" );
fp = fopen("/sys/bus/iio/devices/iio\\:device0/in_voltage0_raw","r");
while(1) {
      c = fgetc(fp);
      if( feof(fp) ) {
         break ;
      }
      printf("%c", c);
   }
   fclose(fp);
//Vo[i] = system(command);

printf("Analog Reading: %d\n", c);

return 0;
}

I get

Segmentation fault

after running the code

I edited the code in the following way

sprintf(command, "cd /sys/bus/iio/devices/iio:device0 && cat in_voltage0_raw"$

After I run it I get the following output

3 9 6 8

Analog Reading: ▒

It reads the output correctly once but in the second

printf("Analog Reading: %d\n", c);

it gives me

Analog Reading: ▒

Full code up to this point

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>       //for Log

int Vo;                 //voltage out
float Ro = 5000;        //value of resistance at 25C in the thermistor
float R ;               //value of fixed resistor
float B = 3977;         //Beta constant
float T0 = 298.15;      //25 degrees C in Kelvin
//float logR2,  R2,T;
//float A = 1.281426510e-03, B = 2.368116050e-04, C = 0.9002008458e-07;  // Steinhart-Hart and Hart Coefficients



int main(){

FILE *fp;
char command[50];
int c;
//get the Analog value
//sprintf(command, "cd /sys/bus/iio/devices/iio\\:device0 && cat in_voltage0_raw" );
fp = fopen("/sys/bus/iio/devices/iio:device0/in_voltage0_raw","r");
if ( fp == NULL ){
perror("Error: ");
return(-1);
}
while(1) {
      c = fgetc(fp);
      if( feof(fp) ) {
         break ;
      }
      printf("%c ", c);
//      Vo = c;
   }

   fclose(fp);
//Vo[i] = system(command);

printf("Analog Reading: %c\n", c);

return 0;

}

I solved the problem by taking every character read and putting it in an array

Here the working code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>       //for Log

char Vo[10];                    //voltage out
float Ro = 5000;        //value of resistance at 25C in the thermistor
float R ;               //value of fixed resistor
float B = 3977;         //Beta constant
float T0 = 298.15;      //25 degrees C in Kelvin
//float logR2,  R2,T;
//float A = 1.281426510e-03, B = 2.368116050e-04, C = 0.9002008458e-07;  // Steinhart-Hart and Hart Coefficients



int main(){

FILE *fp;
char command[50];
int c;
//get the Analog value
//sprintf(command, "cd /sys/bus/iio/devices/iio\\:device0 && cat in_voltage0_raw" );
fp = fopen("/sys/bus/iio/devices/iio:device0/in_voltage0_raw","r");
if ( fp == NULL ){
perror("Error: ");
return(-1);
}
int i=0;
while(1) {
      c = fgetc(fp);
      if( feof(fp) ) {
         break ;
      }
      printf("%c ", c);
if (c != '\0'){
  Vo[i] = c;
++i;
}
//      Vo = c;
   }

   fclose(fp);
//Vo[i] = system(command);

printf("Analog Reading: %s\n", Vo);

return 0;
}

Output:

3 9 7 0

Analog Reading: 3970

Sp1cyChef
  • 23
  • 5
  • 1
    Take a look at `popen` and examples of the usage. – Eugene Sh. Jan 06 '21 at 19:31
  • 1
    Alternatively (and looks like it is way simpler and straightforward) simply `fopen` the `in_voltage0_raw` file in your program and read as regular file. – Eugene Sh. Jan 06 '21 at 19:32
  • @EugeneSh. in_voltage0_raw is not a file. It is a command I'm sending to the circuit I'm using (beaglebone black) to read the analog input. It spits the output in the terminal and I'm trying to take that output to my C program – Sp1cyChef Jan 06 '21 at 19:38
  • 1
    `cat in_voltage0_raw` is definitely treating it as file. – Eugene Sh. Jan 06 '21 at 19:39
  • Yes, you should absolutely be able to read from /sys/bus/iio/devices/iio:device0/in_voltage0_raw as a file, depending on permissions. – Lee Daniel Crocker Jan 06 '21 at 19:47
  • @EugeneSh. I tried to open the file with fopen with no luck take a look at my edited code – Sp1cyChef Jan 06 '21 at 20:02
  • 1
    Check if `fp` is `NULL` and if it is, use `perror` to tell why – Eugene Sh. Jan 06 '21 at 20:03
  • @EugeneSh. it says no such file or directory. I think because, I'm running my code from /Documents and the file is in /sys/bus/iio/devices/iio\\:device0/in_voltage0_raw. How can I get around this? – Sp1cyChef Jan 06 '21 at 20:09
  • maybe something with the excess of the backslashes. What is the actual path (as you see it in the shell)? – Eugene Sh. Jan 06 '21 at 20:11
  • @EugeneSh. the actual path is /sys/bus/iio/devices/iio\:device0/in_voltage0_raw – Sp1cyChef Jan 06 '21 at 20:12
  • I think you need to use just `/sys/bus/iio/devices/iio:device0/in_voltage0_raw`. The backslash is for the shell. – Eugene Sh. Jan 06 '21 at 20:13
  • @EugeneSh. the path works fine now, thanks but I have another problem. See the edit – Sp1cyChef Jan 06 '21 at 20:26
  • It prints the `c` read after EOF. Strange it is not printing integer value though. Are you sure it is the exact code? – Eugene Sh. Jan 06 '21 at 20:29
  • @EugeneSh. I added the full code up to this point – Sp1cyChef Jan 06 '21 at 20:36
  • So the value of `c` is likely `-1`, because it is read at EOF. You should check it instead of `feof`. See [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Eugene Sh. Jan 06 '21 at 20:38
  • @EugeneSh. Thank you for the help. I think I know where to go from here. If you want to post a answer and I'll accept it – Sp1cyChef Jan 06 '21 at 20:46

1 Answers1

0

It looks like you're assuming the output from the system function is your value; it is not. The return value from system() is the exit status of the shell and it is an int and not a float.

I'm not sure what that return code is but you are not casting correctly and I'm surprised you're not getting a compile Warning like:

t.c:22:36: warning: format specifies type 'int' but the argument has type 'float' [-Wformat]
    printf("Analog Reading: %d\n", Vo);
                            ~~     ^~
                            %f
1 warning generated.

Basically this means that you are taking an int and putting it not a float without a proper cast or conversion. Its not actually the data you want. You want the content of the file.

What you might want to do is to read /sys/bus/iio/devices/iio\\:device0/in_voltage0_raw directly as a string and then convert it to the appropriate numeric value.

Technically what you're trying to do is read the stdout from the shell through a return value which is not possible with the function you are using.

Hogstrom
  • 3,581
  • 2
  • 9
  • 25
  • Could you show me how I can do that? I have not done this before – Sp1cyChef Jan 06 '21 at 19:56
  • I suggest that you clarify what problem you have. It's not intended to be an interactive debugging forum. You can find examples of using fread here https://www.educative.io/edpresso/c-reading-data-from-a-file-using-fread and if you have other questions feel free to post a new question. – Hogstrom Jan 06 '21 at 20:59
  • While I appreciate you trying to help me. Your comment is unnecessary, I posted the question to the best of my knowledge and people were able to understand what I was trying to do – Sp1cyChef Jan 06 '21 at 22:22