scpi-parser icon indicating copy to clipboard operation
scpi-parser copied to clipboard

Parsing decimal values

Open hakimoune opened this issue 1 year ago • 6 comments
trafficstars

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

hakimoune avatar Apr 21 '24 11:04 hakimoune

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

j123b567 avatar May 07 '24 07:05 j123b567

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

hakimoune avatar May 07 '24 11:05 hakimoune

@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

jancumps avatar May 10 '24 10:05 jancumps

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

j123b567 avatar May 10 '24 16:05 j123b567

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;

hakimoune avatar May 12 '24 20:05 hakimoune

@hakimoune the DAC_ConfigureVoltage you posted has several issues. Here is a version of it that will set the var variable to a correct uint32_t value (this is what HAL_DAC_SetValue() expects).

static scpi_result_t DAC_ConfigureVoltage(scpi_t * context) {
    uint32_t param1; // Channel number
    double param2; // Value 3.3 max

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

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

    uint32_t var = param2*4095/3.3;

    if (param1 <= 0 ) {
          return SCPI_RES_ERR;
        } else if (param1 > 2) {
          return SCPI_RES_ERR;
        } 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;
}

hinxx avatar Aug 15 '24 14:08 hinxx