Search code examples
craspberry-pistdio

Writing to a file with stdio


I am using an example code from the wiringPi library to read data from Arduino to Raspberry Pi through serial, it is displaying the data correctly with printf("%c", newChar); but I can't write the same data to a text file.

This is the whole file:

 /*
 Pi_Serial_test.cpp - SerialProtocol library - demo
 Copyright (c) 2014 NicoHood.  All right reserved.
 Program to test serial communication

 Compile with:
 sudo gcc -o Pi_Serial_Test.o Pi_Serial_Test.cpp -lwiringPi -DRaspberryPi -pedantic -Wall
 sudo ./Pi_Serial_Test.o
 */

// just that the Arduino IDE doesnt compile these files.
#ifdef RaspberryPi

//include system librarys
#include <stdio.h> //for printf
#include <stdint.h> //uint8_t definitions
#include <stdlib.h> //for exit(int);
#include <string.h> //for errno
#include <errno.h> //error output

//wiring Pi
#include <wiringPi.h>
#include <wiringSerial.h>

char device[]= "/dev/ttyAMA0";
// filedescriptor
int fd;
unsigned long baud = 9600;
unsigned long timeTemp=0;
unsigned long timeHum=0;
//unsigned long timeLight=0;
//unsigned long timeMotion=0;


//prototypes
int main(void);
void loop(void);
void setup(void);

void setup(){
  printf("%s \n", "Raspberry Startup!");
  fflush(stdout);

  //get filedescriptor
  if ((fd = serialOpen (device, baud)) < 0){
    fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
    exit(1); //error
  }

  //setup GPIO in wiringPi mode
  if (wiringPiSetup () == -1){
    fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
    exit(1); //error
  }

}

void loop() {
  // Temperature every 3 seconds
  if(millis()-timeTemp>=3000){
    serialPuts (fd, "05\n");
    // you can also write data from 0-255
    // 65 is in ASCII 'A'
    //serialPutchar (fd, 5);
    timeTemp=millis();
  }

  // read signal
  if(serialDataAvail (fd)){
    char newChar = serialGetchar (fd);
    FILE * writeTemp = fopen("temp.txt", "w");
    printf("%c", newChar);
    fputc(newChar, writeTemp);
    fflush(stdout);
    fclose(writeTemp);
    }


// Humidity every 4 seconds
  if(millis()-timeHum>=4000){
    serialPuts (fd, "06\n");
    // you can also write data from 0-255
    // 65 is in ASCII 'A'
    //serialPutchar (fd, 5);
    timeHum=millis();
  }

  // read signal
  if(serialDataAvail (fd)){
    char newChar = serialGetchar (fd);
    //printf("received from ardiono \n");
    printf("%c", newChar);
    fflush(stdout);
    }



  }

// main function for normal c++ programs on Raspberry
int main(){
  setup();
  while(1) loop();
  return 0;
}

#endif //#ifdef RaspberryPi

I've tried different commands, but I'm constantly getting errors for invalid conversions from const char* to char or to FILE.

I just need to write the data from printf("%c", newChar); in a file.


Solution

  • In the line fputc(&newChar, writeTemp);, you're taking a pointer to your character, converting it to int, then writing that to your file. You should just write your character; something like fputc(newChar, writeTemp);. Or fprintf(writeTemp, "%c", newchar); if you prefer printf.