j123b567 / scpi-parser

Open Source SCPI device library
BSD 2-Clause "Simplified" License
447 stars 191 forks source link

Parsing decimal values #158

Open hakimoune opened 3 months ago

hakimoune commented 3 months ago

Hello, I am having a problem sending decimal value like 2.8 to an STM32 DAC, it worked for me only with integer values. for a DAC in 12 bits mode for example, accepted values are only from 0 to 4096. maybe i made a mistake in conversion. Thank you

j123b567 commented 2 months ago

Hard to say what are you trying to do without example code

hakimoune commented 2 months ago

Hello Jan, for example for this code

static scpi_result_t DAC_MeasureVoltage(scpi_t * context) {
    uint32_t param1;  // Channel number
    double output1 ;

    if (!SCPI_ParamUInt32(context, &param1, TRUE)) {
                return SCPI_RES_ERR;
            }

    if (param1 <= 0 ) {
        return SCPI_ERROR_ILLEGAL_PARAMETER_VALUE;
    } else if (param1 > 2) {
        return SCPI_ERROR_ILLEGAL_PARAMETER_VALUE;
    } else {

        if (param1 == 1) {   // Channel number
            //Channel 1 : PA4
            HAL_DAC_Start(&hdac, DAC_CHANNEL_1);
            output1 = HAL_DAC_GetValue(&hdac, DAC_CHANNEL_1);
        } else {
        //Do something else
        }
    }

    //SCPI_ResultDouble(context, 3.3*output/4095);
    SCPI_ResultDouble(context, output1);
       return SCPI_RES_OK;
}

it work for integer values from 0 to 4096 but the instruction SCPI_ResultDouble(context, 3.3*output/4095); want result in decimal values like 2.3

Thank you

jancumps commented 2 months ago

@hakimoune , have you tried to put 3.3*output/4095 in a variable of type double, and see what its value is?

suggestion to see what's wrong:

double dacval = 3.3 * output1 / 4095; // set breakpoint for value inspection with debugger
SCPI_ResultDouble(context, dacval);
return SCPI_RES_OK;

edit: your code has output in the calculation, instead of output1

j123b567 commented 2 months ago

I can't se anything wrong except the typo output vs output1 as pointed out by jancumps

hakimoune commented 2 months ago

It didn't work even with double. in the following the code with some corrections I have a 12 bits DAC in a microcontroller and want to send value like : "CONFigure:DAC Channel ,value ", for example : CONFigure:DAC 1, 2.3 to get 2.3 volts from the DAC

scpi_result_t DAC_ConfigureVoltage(scpi_t * context) { uint32_t param1; // Channel number uint32_t param2; // Value 4095 max in binary

double var = param2*4095/3.3;

/* read first parameter if present */
if (!SCPI_ParamUInt32(context, &param1, TRUE)) {
    return SCPI_RES_ERR;
}

/* read second parameter if present */
    if (!SCPI_ParamUInt32(context, &param2, TRUE)) {
        return SCPI_RES_ERR;
    }

    if (param1 <= 0 ) {
          return SCPI_ERROR_ILLEGAL_PARAMETER_VALUE;
        } else if (param1 > 2) {
          return SCPI_ERROR_ILLEGAL_PARAMETER_VALUE;
        } else {

            if (param1 == 1) {   // Channel number

                HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, var);
                HAL_DAC_Start(&hdac, DAC_CHANNEL_1);

            } else {

                //.........
            }

        }
return SCPI_RES_OK;