Anlogue Output Voltage



  • Hi,

    finally I was able to set (and read) the analogue output (AO1) on my Neuron M103 device through C with modbus_read_registers().

    So I am using as documented regsiter 3000-3001 as floating point variable.

    Works fine so far- I can set and verify the value by reading.

    Now when attaching a voltmeter to measure the real physical AO voltage I am getting some strange numbers.

    Here is a little table from what I observed:
    Setting the register to value (upper line) yields in measuring a voltage of the lower line on the AO1:

    Set to: 0 1.3 3.5 5.0 5.7 7.9 10.
    Result: 0 1.1 2.9 4.0 4.8 6.5 8.2

    Somehow I doubt it is mismeasured. The device usually is accurate.

    So how does it come to these differences?

    Thanks!

    /KNEBB


  • administrators

    Hi @knebb,
    can you please post the C code?

    Thank you and have a nice day,
    Martin



  • @Martin-Kudláček :

    Of course. Here you are. I set the registers and am measuring the AO port during the sleep() cycle.

    #include "header.h"
    #include "vars.h"
    void main(int argc, char* argv[])
    {
    
    modbus_t *modbusglob;
    union {
           uint16_t word[2];
           uint32_t dword;
           float fl;
    } counter,direct;
    int n[9],mode;
    
    /********************************* Function **********************************/
    void fieldbus_free(modbus_t *modbus)
    {
            if (modbus != NULL) {
            modbus_close(modbus);
            modbus_free(modbus);
        }
    }
    
    /********************************* Function **********************************/
    modbus_t * fieldbus_new(void)
    {
    int err;
    modbus_t *modbus;
    char host[15];
    int port;
    
        strcpy(host,"127.0.0.1");
        port   = 502;
        modbus = modbus_new_tcp(host, port);
        if (modbus == NULL )
        {
           fieldbus_free(modbus);
           exit (55);
        }
        err = modbus_connect(modbus);
        if (err  == -1)
        {
            fieldbus_free(modbus);
            modbus = NULL;
            exit (55);
        };
        err=modbus_set_slave(modbus, 0);
        if(  err == -1)
        {
            fieldbus_free(modbus);
            modbus = NULL;
            exit (60);
        } else {
            return modbus;
        }
    }
    
    /********************************* Function **********************************/
    int setvoltage(float wish)
    {
    int errlvl;
    union {
           uint16_t word[2];
           uint32_t dword;
           float fl;
    } wert;
    uint16_t ao;
    
            wert.fl=wish;
            errlvl=modbus_write_registers(modbusglob, 3000, 2, (&wert.word[0]));
            if (errlvl == -1) {printf("ERR1 modbus_write_register, %s\n",modbus_strerror(errno));}
            else { printf("Register set to %f\n",wert.fl);}
            return errlvl;
    }
    modbusglob=fieldbus_new();
    mode=1;
    setvoltage(1.3);
    sleep(60);
    setvoltage(3.5);
    sleep(60);
    setvoltage(5.7);
    sleep(60);
    setvoltage(7.9);
    sleep(60);
    setvoltage(10.0);
    sleep(60);
    setvoltage(0);
    sleep(60);
    setvoltage(5.0);
    
    fieldbus_free(modbusglob);
    exit (0);
    }
    
    

Log in to reply