scpi-parser, adopted from github
common/scpi-def.cpp@0:d8aca47812e0, 2014-11-14 (annotated)
- Committer:
- iesensor
- Date:
- Fri Nov 14 01:07:59 2014 +0000
- Revision:
- 0:d8aca47812e0
tested on K64F
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
iesensor | 0:d8aca47812e0 | 1 | /*- |
iesensor | 0:d8aca47812e0 | 2 | * Copyright (c) 2012-2013 Jan Breuer, |
iesensor | 0:d8aca47812e0 | 3 | * |
iesensor | 0:d8aca47812e0 | 4 | * All Rights Reserved |
iesensor | 0:d8aca47812e0 | 5 | * |
iesensor | 0:d8aca47812e0 | 6 | * Redistribution and use in source and binary forms, with or without |
iesensor | 0:d8aca47812e0 | 7 | * modification, are permitted provided that the following conditions are |
iesensor | 0:d8aca47812e0 | 8 | * met: |
iesensor | 0:d8aca47812e0 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
iesensor | 0:d8aca47812e0 | 10 | * this list of conditions and the following disclaimer. |
iesensor | 0:d8aca47812e0 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
iesensor | 0:d8aca47812e0 | 12 | * notice, this list of conditions and the following disclaimer in the |
iesensor | 0:d8aca47812e0 | 13 | * documentation and/or other materials provided with the distribution. |
iesensor | 0:d8aca47812e0 | 14 | * |
iesensor | 0:d8aca47812e0 | 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
iesensor | 0:d8aca47812e0 | 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
iesensor | 0:d8aca47812e0 | 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
iesensor | 0:d8aca47812e0 | 18 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE |
iesensor | 0:d8aca47812e0 | 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
iesensor | 0:d8aca47812e0 | 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
iesensor | 0:d8aca47812e0 | 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
iesensor | 0:d8aca47812e0 | 22 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
iesensor | 0:d8aca47812e0 | 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
iesensor | 0:d8aca47812e0 | 24 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
iesensor | 0:d8aca47812e0 | 25 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
iesensor | 0:d8aca47812e0 | 26 | */ |
iesensor | 0:d8aca47812e0 | 27 | |
iesensor | 0:d8aca47812e0 | 28 | /** |
iesensor | 0:d8aca47812e0 | 29 | * @file scpi-def.c |
iesensor | 0:d8aca47812e0 | 30 | * @date Thu Nov 15 10:58:45 UTC 2012 |
iesensor | 0:d8aca47812e0 | 31 | * |
iesensor | 0:d8aca47812e0 | 32 | * @brief SCPI parser test |
iesensor | 0:d8aca47812e0 | 33 | * |
iesensor | 0:d8aca47812e0 | 34 | * |
iesensor | 0:d8aca47812e0 | 35 | */ |
iesensor | 0:d8aca47812e0 | 36 | |
iesensor | 0:d8aca47812e0 | 37 | #include <stdio.h> |
iesensor | 0:d8aca47812e0 | 38 | #include <stdlib.h> |
iesensor | 0:d8aca47812e0 | 39 | #include <string.h> |
iesensor | 0:d8aca47812e0 | 40 | #include "scpi/scpi.h" |
iesensor | 0:d8aca47812e0 | 41 | #include "scpi-def.h" |
iesensor | 0:d8aca47812e0 | 42 | |
iesensor | 0:d8aca47812e0 | 43 | scpi_result_t DMM_MeasureVoltageDcQ(scpi_t * context) { |
iesensor | 0:d8aca47812e0 | 44 | scpi_number_t param1, param2; |
iesensor | 0:d8aca47812e0 | 45 | char bf[15]; |
iesensor | 0:d8aca47812e0 | 46 | fprintf(stderr, "meas:volt:dc\r\n"); // debug command name |
iesensor | 0:d8aca47812e0 | 47 | |
iesensor | 0:d8aca47812e0 | 48 | // read first parameter if present |
iesensor | 0:d8aca47812e0 | 49 | if (!SCPI_ParamNumber(context, ¶m1, false)) { |
iesensor | 0:d8aca47812e0 | 50 | // do something, if parameter not present |
iesensor | 0:d8aca47812e0 | 51 | } |
iesensor | 0:d8aca47812e0 | 52 | |
iesensor | 0:d8aca47812e0 | 53 | // read second paraeter if present |
iesensor | 0:d8aca47812e0 | 54 | if (!SCPI_ParamNumber(context, ¶m2, false)) { |
iesensor | 0:d8aca47812e0 | 55 | // do something, if parameter not present |
iesensor | 0:d8aca47812e0 | 56 | } |
iesensor | 0:d8aca47812e0 | 57 | |
iesensor | 0:d8aca47812e0 | 58 | |
iesensor | 0:d8aca47812e0 | 59 | SCPI_NumberToStr(context, ¶m1, bf, 15); |
iesensor | 0:d8aca47812e0 | 60 | fprintf(stderr, "\tP1=%s\r\n", bf); |
iesensor | 0:d8aca47812e0 | 61 | |
iesensor | 0:d8aca47812e0 | 62 | |
iesensor | 0:d8aca47812e0 | 63 | SCPI_NumberToStr(context, ¶m2, bf, 15); |
iesensor | 0:d8aca47812e0 | 64 | fprintf(stderr, "\tP2=%s\r\n", bf); |
iesensor | 0:d8aca47812e0 | 65 | |
iesensor | 0:d8aca47812e0 | 66 | SCPI_ResultDouble(context, 0); |
iesensor | 0:d8aca47812e0 | 67 | |
iesensor | 0:d8aca47812e0 | 68 | return SCPI_RES_OK; |
iesensor | 0:d8aca47812e0 | 69 | } |
iesensor | 0:d8aca47812e0 | 70 | |
iesensor | 0:d8aca47812e0 | 71 | |
iesensor | 0:d8aca47812e0 | 72 | scpi_result_t DMM_ConfigureVoltageDc(scpi_t * context) { |
iesensor | 0:d8aca47812e0 | 73 | double param1, param2; |
iesensor | 0:d8aca47812e0 | 74 | fprintf(stderr, "conf:volt:dc\r\n"); // debug command name |
iesensor | 0:d8aca47812e0 | 75 | |
iesensor | 0:d8aca47812e0 | 76 | // read first parameter if present |
iesensor | 0:d8aca47812e0 | 77 | if (!SCPI_ParamDouble(context, ¶m1, true)) { |
iesensor | 0:d8aca47812e0 | 78 | return SCPI_RES_ERR; |
iesensor | 0:d8aca47812e0 | 79 | } |
iesensor | 0:d8aca47812e0 | 80 | |
iesensor | 0:d8aca47812e0 | 81 | // read second paraeter if present |
iesensor | 0:d8aca47812e0 | 82 | if (!SCPI_ParamDouble(context, ¶m2, false)) { |
iesensor | 0:d8aca47812e0 | 83 | // do something, if parameter not present |
iesensor | 0:d8aca47812e0 | 84 | } |
iesensor | 0:d8aca47812e0 | 85 | |
iesensor | 0:d8aca47812e0 | 86 | fprintf(stderr, "\tP1=%lf\r\n", param1); |
iesensor | 0:d8aca47812e0 | 87 | fprintf(stderr, "\tP2=%lf\r\n", param2); |
iesensor | 0:d8aca47812e0 | 88 | |
iesensor | 0:d8aca47812e0 | 89 | return SCPI_RES_OK; |
iesensor | 0:d8aca47812e0 | 90 | } |
iesensor | 0:d8aca47812e0 | 91 | |
iesensor | 0:d8aca47812e0 | 92 | static const scpi_command_t scpi_commands[] = { |
iesensor | 0:d8aca47812e0 | 93 | /* {"pattern", callback} * |
iesensor | 0:d8aca47812e0 | 94 | |
iesensor | 0:d8aca47812e0 | 95 | /* IEEE Mandated Commands (SCPI std V1999.0 4.1.1) */ |
iesensor | 0:d8aca47812e0 | 96 | {"*CLS", SCPI_CoreCls,}, |
iesensor | 0:d8aca47812e0 | 97 | {"*ESE", SCPI_CoreEse,}, |
iesensor | 0:d8aca47812e0 | 98 | {"*ESE?", SCPI_CoreEseQ,}, |
iesensor | 0:d8aca47812e0 | 99 | {"*ESR?", SCPI_CoreEsrQ,}, |
iesensor | 0:d8aca47812e0 | 100 | {"*IDN?", SCPI_CoreIdnQ,}, |
iesensor | 0:d8aca47812e0 | 101 | {"*OPC", SCPI_CoreOpc,}, |
iesensor | 0:d8aca47812e0 | 102 | {"*OPC?", SCPI_CoreOpcQ,}, |
iesensor | 0:d8aca47812e0 | 103 | {"*RST", SCPI_CoreRst,}, |
iesensor | 0:d8aca47812e0 | 104 | {"*SRE", SCPI_CoreSre,}, |
iesensor | 0:d8aca47812e0 | 105 | {"*SRE?", SCPI_CoreSreQ,}, |
iesensor | 0:d8aca47812e0 | 106 | {"*STB?", SCPI_CoreStbQ,}, |
iesensor | 0:d8aca47812e0 | 107 | {"*TST?", SCPI_CoreTstQ,}, |
iesensor | 0:d8aca47812e0 | 108 | {"*WAI", SCPI_CoreWai,}, |
iesensor | 0:d8aca47812e0 | 109 | |
iesensor | 0:d8aca47812e0 | 110 | /* Required SCPI commands (SCPI std V1999.0 4.2.1) */ |
iesensor | 0:d8aca47812e0 | 111 | {"SYSTem:ERRor?", SCPI_SystemErrorNextQ,}, |
iesensor | 0:d8aca47812e0 | 112 | {"SYSTem:ERRor:NEXT?", SCPI_SystemErrorNextQ,}, |
iesensor | 0:d8aca47812e0 | 113 | {"SYSTem:ERRor:COUNt?", SCPI_SystemErrorCountQ,}, |
iesensor | 0:d8aca47812e0 | 114 | {"SYSTem:VERSion?", SCPI_SystemVersionQ,}, |
iesensor | 0:d8aca47812e0 | 115 | |
iesensor | 0:d8aca47812e0 | 116 | //{"STATus:OPERation?", scpi_stub_callback,}, |
iesensor | 0:d8aca47812e0 | 117 | //{"STATus:OPERation:EVENt?", scpi_stub_callback,}, |
iesensor | 0:d8aca47812e0 | 118 | //{"STATus:OPERation:CONDition?", scpi_stub_callback,}, |
iesensor | 0:d8aca47812e0 | 119 | //{"STATus:OPERation:ENABle", scpi_stub_callback,}, |
iesensor | 0:d8aca47812e0 | 120 | //{"STATus:OPERation:ENABle?", scpi_stub_callback,}, |
iesensor | 0:d8aca47812e0 | 121 | |
iesensor | 0:d8aca47812e0 | 122 | {"STATus:QUEStionable?", SCPI_StatusQuestionableEventQ,}, |
iesensor | 0:d8aca47812e0 | 123 | {"STATus:QUEStionable:EVENt?", SCPI_StatusQuestionableEventQ,}, |
iesensor | 0:d8aca47812e0 | 124 | //{"STATus:QUEStionable:CONDition?", scpi_stub_callback,}, |
iesensor | 0:d8aca47812e0 | 125 | {"STATus:QUEStionable:ENABle", SCPI_StatusQuestionableEnable,}, |
iesensor | 0:d8aca47812e0 | 126 | {"STATus:QUEStionable:ENABle?", SCPI_StatusQuestionableEnableQ,}, |
iesensor | 0:d8aca47812e0 | 127 | |
iesensor | 0:d8aca47812e0 | 128 | {"STATus:PRESet", SCPI_StatusPreset,}, |
iesensor | 0:d8aca47812e0 | 129 | |
iesensor | 0:d8aca47812e0 | 130 | /* DMM */ |
iesensor | 0:d8aca47812e0 | 131 | {"MEASure:VOLTage:DC?", DMM_MeasureVoltageDcQ,}, |
iesensor | 0:d8aca47812e0 | 132 | {"CONFigure:VOLTage:DC", DMM_ConfigureVoltageDc,}, |
iesensor | 0:d8aca47812e0 | 133 | {"MEASure:VOLTage:DC:RATio?", SCPI_StubQ,}, |
iesensor | 0:d8aca47812e0 | 134 | {"MEASure:VOLTage:AC?", SCPI_StubQ,}, |
iesensor | 0:d8aca47812e0 | 135 | {"MEASure:CURRent:DC?", SCPI_StubQ,}, |
iesensor | 0:d8aca47812e0 | 136 | {"MEASure:CURRent:AC?", SCPI_StubQ,}, |
iesensor | 0:d8aca47812e0 | 137 | {"MEASure:RESistance?", SCPI_StubQ,}, |
iesensor | 0:d8aca47812e0 | 138 | {"MEASure:FRESistance?", SCPI_StubQ,}, |
iesensor | 0:d8aca47812e0 | 139 | {"MEASure:FREQuency?", SCPI_StubQ,}, |
iesensor | 0:d8aca47812e0 | 140 | {"MEASure:PERiod?", SCPI_StubQ,}, |
iesensor | 0:d8aca47812e0 | 141 | |
iesensor | 0:d8aca47812e0 | 142 | {"SYSTem:COMMunication:TCPIP:CONTROL?", SCPI_SystemCommTcpipControlQ,}, |
iesensor | 0:d8aca47812e0 | 143 | |
iesensor | 0:d8aca47812e0 | 144 | SCPI_CMD_LIST_END |
iesensor | 0:d8aca47812e0 | 145 | }; |
iesensor | 0:d8aca47812e0 | 146 | |
iesensor | 0:d8aca47812e0 | 147 | static scpi_interface_t scpi_interface = { |
iesensor | 0:d8aca47812e0 | 148 | /* error */ SCPI_Error, |
iesensor | 0:d8aca47812e0 | 149 | /* write */ SCPI_Write, |
iesensor | 0:d8aca47812e0 | 150 | /* control */ SCPI_Control, |
iesensor | 0:d8aca47812e0 | 151 | /* flush */ SCPI_Flush, |
iesensor | 0:d8aca47812e0 | 152 | /* reset */ SCPI_Reset, |
iesensor | 0:d8aca47812e0 | 153 | /* test */ SCPI_Test, |
iesensor | 0:d8aca47812e0 | 154 | }; |
iesensor | 0:d8aca47812e0 | 155 | |
iesensor | 0:d8aca47812e0 | 156 | #define SCPI_INPUT_BUFFER_LENGTH 256 |
iesensor | 0:d8aca47812e0 | 157 | static char scpi_input_buffer[SCPI_INPUT_BUFFER_LENGTH]; |
iesensor | 0:d8aca47812e0 | 158 | |
iesensor | 0:d8aca47812e0 | 159 | static scpi_reg_val_t scpi_regs[SCPI_REG_COUNT]; |
iesensor | 0:d8aca47812e0 | 160 | |
iesensor | 0:d8aca47812e0 | 161 | |
iesensor | 0:d8aca47812e0 | 162 | scpi_t scpi_context = { |
iesensor | 0:d8aca47812e0 | 163 | /* cmdlist */ scpi_commands, |
iesensor | 0:d8aca47812e0 | 164 | /* buffer */ { /* length */ SCPI_INPUT_BUFFER_LENGTH, /* position */ 0, /* data */ scpi_input_buffer, }, |
iesensor | 0:d8aca47812e0 | 165 | /* paramlist */ { /* cmd */ NULL, /* parameters */ NULL, /* length */ 0, }, |
iesensor | 0:d8aca47812e0 | 166 | /* interface */ &scpi_interface, |
iesensor | 0:d8aca47812e0 | 167 | /* output_count */ 0, |
iesensor | 0:d8aca47812e0 | 168 | /* input_count */ 0, |
iesensor | 0:d8aca47812e0 | 169 | /* cmd_error */ FALSE, |
iesensor | 0:d8aca47812e0 | 170 | /* error_queue */ NULL, |
iesensor | 0:d8aca47812e0 | 171 | /* registers */ scpi_regs, |
iesensor | 0:d8aca47812e0 | 172 | /* units */ scpi_units_def, |
iesensor | 0:d8aca47812e0 | 173 | /* special_numbers */ scpi_special_numbers_def, |
iesensor | 0:d8aca47812e0 | 174 | /* user_context */ NULL, |
iesensor | 0:d8aca47812e0 | 175 | /* idn */ {"MANUFACTURE", "INSTR2013", NULL, "01-02"}, |
iesensor | 0:d8aca47812e0 | 176 | }; |
iesensor | 0:d8aca47812e0 | 177 |