energy.cpp 3.44 KB
Newer Older
1 2 3

#include "energy.h"

Martin Perdacher's avatar
Martin Perdacher committed
4
// #include <sys/ioctl.h>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

Hioki::Hioki(){
    this->rs_232_open();
    this->rs_232_config();
}

Hioki::~Hioki(){
    close(fd);
}

void Hioki::start(){
    const char *query = ":INTEGrate:STATe START\n";

    int nbytes = write(getFd(), query, strlen(query));
    if (nbytes < 0){
        perror("write of 'start' failed!\n");
        exit(1);
    }
    // printf("start: %d written\n",nbytes);
    usleep( SLEEPVAL * (strlen(query) ) );
}

int Hioki::getFd(){
    return fd;
}

void Hioki::setFd(int filedescriptor){
    fd = filedescriptor;
}

void Hioki::reset(){
    const char *query = ":INTEGrate:STATe RESET\n";
    int nbytes = write(getFd(), query, strlen(query));
    if (nbytes < 0){
        perror("write of 'reset' failed!\n");
        exit(1);
    }

    usleep(SLEEPVAL * (strlen(query) ) );
}

void Hioki::stop(){
    const char *query = ":INTEGrate:STATe STOP\n";

    int nbytes = write(getFd(), query, strlen(query));
    if (nbytes < 0){
        // printf("fd: %d\n", getFd());
        perror("write of 'stop' failed!\n");
        exit(1);
    }

    usleep(SLEEPVAL * (strlen(query) ) );

}

double Hioki::getWH(){
    char buffer[1024];
    char *bufptr;
    int nbytes,nread;
    double valueWH = 0.0;
    const char *query = ":MEASure? WH\n";

    nbytes = write(getFd(), query, strlen(query));
    if (nbytes < 0){
        perror("query of 'measure' failed!\n");
        exit(1);
    }

    usleep(SLEEPVAL * (strlen(query) + 50) );
    bufptr = buffer;
    // are there some bytes available on input
    ioctl(getFd(), FIONREAD, &nbytes);
    if ( nbytes > 0 ){
        nread = read(getFd(), bufptr, nbytes);
        bufptr[nread] = '\0';

        // receiving data in format:
        // WH +0.00053E+3
        bufptr += 3;

        valueWH = atof(bufptr);
    }else{
        fprintf(stderr, "Error in parsing value!\n");
        exit(1);
    }

    return valueWH;
}

void Hioki::rs_232_open(){
    int fdesc = open(device, O_CREAT | O_RDWR | O_NOCTTY | O_NDELAY);
    setFd(fdesc);
    // printf("fd: %d\n", getFd() );
    if(fd == -1) {
        perror("failed to open port\n" );
        exit(1);
    }else
        fcntl(getFd(), F_SETFL, 0);
}

void Hioki::rs_232_config(){
    struct termios options;
    // configuring port
    tcgetattr(getFd(), &options);
    cfsetispeed(&options, B9600); // BAUDRATE to 9600
    cfsetospeed(&options, B9600);

    options.c_cflag = (options.c_cflag & ~CSIZE) | CS8; // 8-bit chars
    options.c_iflag &= ~IGNBRK;
    options.c_lflag = 0;
    options.c_oflag = 0;                // no remapping, no delays
    options.c_cc[VMIN]  = 0;            // read doesn't block
    options.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

    options.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

    options.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                    // enable reading
    options.c_cflag &= ~(PARENB | PARODD);      // shut off parity
    options.c_cflag |= 0; // 0 no parity, PARENB|PARODD odd parity, PARENB (enable parity and use even), PARENB|PARODD|CMSPAR (mark parity), and PARENB|CMSPAR (space parity).
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CRTSCTS;

    /*
     * Enable the receiver and set local mode...
     */
    options.c_cflag |= (CLOCAL | CREAD);
    /*
     * Set the new options for the port...
     */
    if ( tcsetattr(getFd(), TCSANOW, &options) < 0 ){
        perror("Failed to apply settings\n");
        exit(1);
    }

}