Thread: problem C-Program (Cache?, Storage?, ...)

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    12

    problem C-Program (Cache?, Storage?, ...)

    Hey guys,
    I run a C-Program on a raspberry pi (Linux) and the Code funtion, but there is maybe a cache Problem. It is used to get data from a Sensor.

    If I run the program and ask for example the temperature, the sensor gives me the temperature. But if I asked after this the Humidtiy for example it don't function anymore.
    When I restart the program then I can ask again for a value.

    So maybe it is a chache problem with sending the commands?

    Maybe you can help me


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <termios.h>
    #include <signal.h>
    #include <errno.h>
    
    
    static sig_atomic_t end = 0;
    
    
    static void sighandler(int signo)
    {
        end = 1;
    }
    
    
    int main(int argc, char** argv)
    {
        struct termios tio;
        int tty_fd;
        struct sigaction sa;
    
    
        memset(&sa, 0, sizeof(struct sigaction));
        sa.sa_handler = sighandler;
        sigaction(SIGINT, &sa, NULL);
        sigaction(SIGQUIT, &sa, NULL);
        sigaction(SIGTERM, &sa, NULL);
    
    
        printf("RS232\n\n");
    
    
        memset(&tio, 0, sizeof(tio));
        tio.c_iflag = 0;
        tio.c_oflag = 0;
        tio.c_cflag = B9600 | CS8 | PARODD | CREAD | CLOCAL;
        tio.c_lflag = 0;
        tio.c_cc[VMIN] = 1;
        tio.c_cc[VTIME] = 5;
    
    
        printf("Open device\n");
        tty_fd = open("/dev/ttyAMA0",O_RDWR | O_NONBLOCK);
        if (-1 == tty_fd)
        {
            printf ("unableto open device, error '%s' (%d)\n",
            strerror(errno), errno);
            return EXIT_FAILURE;
        }
    
    
        cfsetospeed(&tio, B9600);
        cfsetispeed(&tio, B9600);
        tcsetattr(tty_fd, TCSANOW, &tio);
    
    
        //_____________________________________________________________________________________
    
    
        int choose = 0;
    
    
        do
        {
            printf("1. Temperature\n");
            printf("2. Humidity\n");
            printf("3. CO2\n");
            printf("4. Exit\n");
            scanf("%d", &choose);
            printf("\n");
    
    
            switch (choose)
            {
                case 1: // Temperature
                {
                    char command1[] = "T\r\n";
                    float temp;
                    char *p1;
                    unsigned char buftemp[8];
                    char buftemp2[5];
    
    
                    write(tty_fd, &command1, 5);
    
    
                    while (!(read(tty_fd, &buftemp, 8) && buftemp[0]>0))
                    {
    
    
                    }
                    memmove (buftemp2, buftemp + 3, 5);
                    temp = strtof(buftemp2, &p1);
                    temp = temp - 1000;
                    temp = temp / 10;
    
    
                    printf("Temperature: %.2f C\n", temp);
    
    
                    break;
                }
    
    
                case 2: // Humidity
                {
                    char command2[] = "H\r\n";
                    float humi;
                    char *p2;
                    unsigned char bufhumi[8];
                    char bufhumi2[5];
    
    
                    write(tty_fd, &command2, 5);
    
    
                    while (!(read(tty_fd, &bufhumi, 8) && bufhumi[0]>0))
                    {
    
    
                    }
                    memmove (bufhumi2, bufhumi + 3, 5);
                    humi = strtof(bufhumi2, &p2);
                    humi = humi / 10;
    
    
                    printf("Humidity: %.2f %\n", humi);
    
    
                    break;
                }
    
    
                case 3: // Carbondioxide
                {
                    char command3[] = "Z\r\n";
                    float carb;
                    char *p3;
                    unsigned char bufcarb[8];
                    char bufcarb2[5];
    
    
                    write(tty_fd, &command3, 5);
    
    
                    while (!(read(tty_fd, &bufcarb, 8) && bufcarb[0]>0))
                    {
    
    
                    }
                    memmove (bufcarb2, bufcarb + 3, 5);
                    carb = strtof(bufcarb2, &p3);
    
    
                    printf("Carbondioxide: %.2f ppm\n", carb);
    
    
                    carb = carb / 10000;
    
    
                    printf("Carbondioxide: %.2f %\n", carb);
    
    
                    break;
                }
            }
        }
        while (choose !=4);
    
    
        close(tty_fd);
    
    
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > char command1[] = "T\r\n";
    Well it might have something to do with the fact that your command strings are only 3 characters long, and not 5.
    So you're writing garbage to something.

    Recall that \r is the escape code for the single character carriage return.

    > while (!(read(tty_fd, &buftemp, 8)
    read may return any value in the range 1 to 8 inclusive, and all would count as success.
    Also, read may return -1 on error - again, that's some kind of success for your naive loop.

    read may hang around until the buffer is full, then again, it might not.

    To be sure, you should have
    Code:
    int filled = 0;
    int remainder = 8;
    do {
      ssize_t r = read(tty_fd,&buftemp[filled], remainder);
      if ( r > 0 ) {
        // progress through the buffer.
        filled += r;
        remainder -= r;
      } else {
        // test r == 0 and r == -1 cases as well
        // not all errors are fatal - read the manual
      }
    } while ( remainder > 0 );
    Another thing to bear in mind is that buftemp will NOT have a \0 on the end of it (unless the sender specifically sends one).
    So stringlike functions such as strtof() are likely to produce garbage.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ cache hit/miss
    By Elias Rizik in forum Tech Board
    Replies: 3
    Last Post: 04-26-2015, 05:36 PM
  2. D-Cache/I Cache Simulator
    By husslela2 in forum C Programming
    Replies: 7
    Last Post: 04-27-2010, 08:41 AM
  3. Difference between ARP Cache and DNS cache?
    By namasteall2000 in forum Networking/Device Communication
    Replies: 9
    Last Post: 06-26-2009, 08:49 AM
  4. Cache using c/c++
    By m_kk in forum C Programming
    Replies: 0
    Last Post: 01-28-2008, 09:36 PM
  5. Problem in Array Storage
    By Amitav in forum C Programming
    Replies: 4
    Last Post: 04-29-2005, 12:15 AM