Jan Breuer
2012-12-04 697414b42a36a0c529bd1c95d82c81a09bfb0ec9
Update README - syntax correction and cleanup
1个文件已修改
56 ■■■■■ 已修改文件
README.md 56 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
README.md
@@ -28,15 +28,17 @@
-----------
Command callbac is defined as function with context parameter, e.g.:
```c
    int DMM_MeasureVoltageDcQ(scpi_context_t * context)
```
The "Q" at the end of the function name indicates, that this function is Query function (command with "?").
The command callback can use predefined function to parse input parameters and to write output.
Reading input parameter is done by functions "SCPI_ParamInt", "SCPI_ParamDouble", "SCPI_ParamString".
Reading input parameter is done by functions `SCPI_ParamInt`, `SCPI_ParamDouble`, `SCPI_ParamString` adn `SCPI_ParamNumber`.
Writing output is done by functions "SCPI_ResultInt", "SCPI_ResultDouble", "SCPI_ResultString", "SCPI_ResultText". You can write multiple output variables. They are automaticcaly separated by coma ",".
Writing output is done by functions `SCPI_ResultInt`, `SCPI_ResultDouble`, `SCPI_ResultString`, `SCPI_ResultText`. You can write multiple output variables. They are automaticcaly separated by coma ",".
Source code organisation
------------
@@ -58,15 +60,18 @@
-------------
First of all you need to fill structure of SCPI command definitions
    
```c
    scpi_command_t scpi_commands[] = {
        { .pattern = "*IDN?", .callback = SCPI_CoreIdnQ,},
        { .pattern = "*RST", .callback = SCPI_CoreRst,},
        { .pattern = "MEASure:VOLTage:DC?", .callback = DMM_MeasureVoltageDcQ,},
        SCPI_CMD_LIST_END
    };
```
Than you need to initialize interface callbacks structure. If you don't want to provide some callbacks, just initialize it as NULL. write callback is mandatory and is used to output data from the library.
Than you need to initialize interface callbacks structure. If you don't want to provide some callbacks, just initialize it as `NULL`. write callback is mandatory and is used to output data from the library.
```c
    scpi_interface_t scpi_interface = {
        .write = myWrite,
        .error = NULL,
@@ -74,15 +79,18 @@
        .test = NULL,
        .srq = NULL,
    };
```
Important thing is command buffer. Maximum size is up to you and it should be larger than any possible largest command. 
```c
    #define SCPI_INPUT_BUFFER_LENGTH 256
    static char scpi_input_buffer[SCPI_INPUT_BUFFER_LENGTH];
```
The last structure is scpi context used in parser library.
```c
    scpi_t scpi_context = {
        .cmdlist = scpi_commands,
        .buffer = {
@@ -94,63 +102,75 @@
        .units = scpi_units_def,
        .special_numbers = scpi_special_numbers_def,
    };
```
All these structures should be global variables of the c file or allocated by function like malloc. It is common mistake to create these structures inside a function as local variables of this function. This will not work. If you don't know why, you should read something about [function stack.](http://stackoverflow.com/questions/4824342/returning-a-local-variable-from-function-in-c).
Now we are ready to initialize SCPI context. It is possible to use more SCPI contexts and share some configurations (command list, registers, units list, error callback...)
```c
    SCPI_Init(&scpi_context);
```
Test implementation of function myWrite, which outputs everything to stdout, can be
    
```c
    size_t myWrite(scpi_context_t * context, const char * data, size_t len) {
        (void) context;
        return fwrite(data, 1, len, stdout);
    }
```
Interactive demo can beimplemented using this loop
```c
    #define SMALL_BUFFER_LEN
    char smbuffer[SMALL_BUFFER_LEN];
    while(1) {
        fgets(smbuffer, SMALL_BUFFER_LEN, stdin);
        SCPI_Input(&scpi_context, smbuffer, strlen(smbuffer));
    }
```
Implementation of command callback
-------------
Command callback is defined as function with result of type scpi_result_t and one parameter - scpi context
    scpi_result_t DMM_MeasureVoltageDcQ(scpi_t * context)
Command callback is defined as function with result of type `scpi_result_t` and one parameter - scpi context
Command callback should return SCPI_RES_OK if everything goes well.
```c
    scpi_result_t DMM_MeasureVoltageDcQ(scpi_t * context)
```
Command callback should return `SCPI_RES_OK` if everything goes well.
You can read command parameters and write command results. There are several functions to do this.
Every time, you call function to read parameter, it shifts pointers to the next parameter. You can't read specified parameter directly by its index - e.g. 
```c
    // pseudocode
    param3 = read_param(3); // this is not possible
    read_param();           // discard first parameter
    read_param();           // discard second parameter
    param3 = read_param();  // read third parameter
```
If you discard some parameters, there is no way to recover them.
These are the functions, you can use to read parameters
 - SCPI_ParamInt - read signed 32bit integer value (dec or hex with 0x prefix)
 - SCPI_ParamDouble - read double value
 - SCPI_ParamNumber - read double value with or without units or represented by special number (DEF, MIN, MAX, ...). This function is more universal then SCPI_ParamDouble.
 - SCPI_ParamText - read text value - may be encapsuled in ""
 - SCPI_ParamString - read unspecified parameter not encapsulated in ""
 - `SCPI_ParamInt` - read signed 32bit integer value (dec or hex with 0x prefix)
 - `SCPI_ParamDouble` - read double value
 - `SCPI_ParamNumber` - read double value with or without units or represented by special number (DEF, MIN, MAX, ...). This function is more universal then SCPI_ParamDouble.
 - `SCPI_ParamText` - read text value - may be encapsuled in ""
 - `SCPI_ParamString` - read unspecified parameter not encapsulated in ""
These are the functions, you can use to write results
 - SCPI_ResultInt - write integer value
 - SCPI_ResultDouble - write double value
 - SCPI_ResultText - write text value encapsulated in ""
 - SCPI_ResultString - directly write string value
 - `SCPI_ResultInt` - write integer value
 - `SCPI_ResultDouble` - write double value
 - `SCPI_ResultText` - write text value encapsulated in ""
 - `SCPI_ResultString` - directly write string value
You can use function SCPI_NumberToStr to convert number with units to textual representation and then use SCPI_ResultString to write this to the user.
You can use function `SCPI_NumberToStr` to convert number with units to textual representation and then use `SCPI_ResultString` to write this to the user.