Motor Driver Evaluation Kit NEVB-MTR1-t01-1.0.0
Firmware for NEVB-MTR1-KIT1 for trapezoidal control of BLDC motors using Hall-effect sensors
|
SCPI Parser class. More...
#include <scpi_parser.h>
Classes | |
struct | internal_errors |
Internal errors container. More... | |
Public Member Functions | |
SCPI_Parser () | |
SCPI_Parser class constructor. | |
void | SetCommandTreeBase (char *tree_base) |
Changes the base of the command tree for subsequent RegisterCommand calls. | |
void | SetCommandTreeBase (const char *tree_base) |
SetCommandTreeBase version with RAM string support. | |
void | SetCommandTreeBase (const __FlashStringHelper *tree_base) |
SetCommandTreeBase version with Flash strings (F() macro) support. | |
void | RegisterCommand (char *command, SCPI_caller_t caller) |
Registers a new valid SCPI command and associates a callback function with it. | |
void | RegisterCommand (const char *command, SCPI_caller_t caller) |
RegisterCommand version with RAM string support. | |
void | RegisterCommand (const __FlashStringHelper *command, SCPI_caller_t caller) |
RegisterCommand version with Flash strings (F() macro) support. | |
void | SetErrorHandler (SCPI_caller_t caller) |
Sets the function to be used as the error handler. | |
void | Execute (char *message, Stream &interface) |
Processes an incoming SCPI message and executes the associated command if found. | |
void | ProcessInput (Stream &interface, const char *term_chars) |
Reads a message from a Stream interface and executes it. | |
char * | GetMessage (Stream &interface, const char *term_chars) |
Reads a message from a Stream interface until termination characters are found. | |
void | PrintDebugInfo (Stream &interface) |
Prints debug information about the SCPI parser configuration and status to a Stream interface. | |
Public Attributes | |
ErrorCode | last_error = ErrorCode::NoError |
Variable that holds the last error code. | |
scpi_hash_t | hash_magic_number = 37 |
Magic number used for hashing the commands. | |
scpi_hash_t | hash_magic_offset = 7 |
Magic offset used for hashing the commands. | |
unsigned long | timeout = 10 |
Timeout, in miliseconds, for GetMessage and ProcessInput. | |
Protected Member Functions | |
void | AddToken_ (char *token) |
Add a token to the tokens' storage. | |
scpi_hash_t | GetCommandCode_ (SCPI_Commands &commands) |
Get a hash from a command. | |
Protected Attributes | |
const uint8_t | buffer_length = 64 |
Length of the message buffer. | |
const uint8_t | max_tokens = 20 |
Max number of valid tokens. | |
const uint8_t | max_commands = 20 |
Max number of registered commands. | |
struct SCPI_Parser::internal_errors | setup_errors |
const scpi_hash_t | unknown_hash = 0 |
Hash result for unknown commands. | |
const scpi_hash_t | invalid_hash = 1 |
Hash reserved for invalid commands. | |
uint8_t | tokens_size_ = 0 |
Number of stored tokens. | |
char * | tokens_ [20] |
Storage for tokens. | |
uint8_t | codes_size_ = 0 |
Number of registered commands. | |
scpi_hash_t | valid_codes_ [20] |
Registered commands' hash storage. | |
SCPI_caller_t | callers_ [20+1] |
Pointers to the functions to be called when a valid command is received. | |
scpi_hash_t | tree_code_ = 0 |
TreeBase branch's hash used when calculating hashes (0 for root) | |
uint8_t | tree_length_ = 0 |
TreeBase branch's length (0 for root) | |
char | msg_buffer_ [64] |
Message buffer. | |
uint8_t | message_length_ = 0 |
Length of the readed message. | |
unsigned long | time_checker_ |
Varible used for checking timeout errors. | |
SCPI Parser class.
This class provides the functionality to parse and execute commands based on the Standard Commands for Programmable Instruments (SCPI) syntax. It allows registering commands with associated callback functions and handles the processing of incoming messages from a serial stream.
Definition at line 54 of file scpi_parser.h.
SCPI_Parser::SCPI_Parser | ( | ) |
SCPI_Parser class constructor.
Initializes the SCPI_Parser object. It sets the default error handler to a no-operation function.
Definition at line 56 of file scpi_parser.cpp.
|
protected |
Add a token to the tokens' storage.
Adds a token to the internal tokens' storage.
This private method adds a given token (keyword) to the internal storage used for command parsing. It prevents adding duplicate tokens and checks for potential overflow of the token storage. Query symbols ('?') at the end of the token are removed before storing.
token | A pointer to a null-terminated character array representing the token to add. |
Definition at line 72 of file scpi_parser.cpp.
void SCPI_Parser::Execute | ( | char * | message, |
Stream & | interface ) |
Processes an incoming SCPI message and executes the associated command if found.
This method takes a SCPI message as input, parses it into commands and parameters, and then attempts to find a registered command that matches. If a match is found, the associated callback function is executed, with the parsed commands, parameters, and the communication interface passed as arguments. The message can contain multiple commands separated by semicolons (';').
message | A pointer to a null-terminated character array containing the SCPI message to process. Example: "*IDN?; MEASure:VOLTage?" . |
interface | A reference to a Stream object (e.g., Serial) used for communication. |
Definition at line 376 of file scpi_parser.cpp.
|
protected |
Get a hash from a command.
Gets a hash code for a valid SCPI command.
This private method calculates a hash code for a given SCPI command represented by a SCPI_Commands object. The hash is computed based on the sequence of keywords in the command and the currently set command tree base. If any of the keywords in the command are not registered as known tokens, the function returns unknown_hash
.
commands | A reference to the SCPI_Commands object representing the command keywords. |
unknown_hash
if unregistered tokens are found.Definition at line 109 of file scpi_parser.cpp.
char * SCPI_Parser::GetMessage | ( | Stream & | interface, |
const char * | term_chars ) |
Reads a message from a Stream interface until termination characters are found.
This method reads characters from the provided Stream interface and stores them in an internalinternal buffer until one of the specified termination character sequences is encountered. It also handles communication timeouts and buffer overflows, calling the error handler if either occurs.
interface | A reference to a Stream object (e.g., Serial) to read the message from. |
term_chars | A constant pointer to a null-terminated character array containing the termination characters for a message (e.g., "\r\n"). |
NULL
if no complete message is received due to timeout or no incoming data. Definition at line 444 of file scpi_parser.cpp.
void SCPI_Parser::PrintDebugInfo | ( | Stream & | interface | ) |
Prints debug information about the SCPI parser configuration and status to a Stream interface.
This method prints various internal details of the SCPI parser, including the maximum sizes of internal buffers and arrays, the currently registered tokens and commands with their hash codes and associated handler function addresses, and any setup errors that might have occurred (e.g., buffer overflows). This information is useful for debugging and understanding the parser's internal state.
interface | A reference to a Stream object (e.g., Serial) to print the debug information to. |
Definition at line 529 of file scpi_parser.cpp.
void SCPI_Parser::ProcessInput | ( | Stream & | interface, |
const char * | term_chars ) |
Reads a message from a Stream interface and executes it.
This method continuously reads data from the provided Stream interface until a termination character (or sequence of characters) is detected. Once a complete message is received, it is passed to the Execute method for parsing and execution.
interface | A reference to a Stream object (e.g., Serial) to read the message from. |
term_chars | A constant pointer to a null-terminated character array containing the termination characters for a message (e.g., "\n", "\r\n"). |
Definition at line 424 of file scpi_parser.cpp.
void SCPI_Parser::RegisterCommand | ( | char * | command, |
SCPI_caller_t | caller ) |
Registers a new valid SCPI command and associates a callback function with it.
This method registers a new SCPI command that the parser will recognize. When a matching command is received and parsed, the provided callback function will be executed. The command string can include a query symbol ('?') for query commands. The command is tokenized, and each token is added to the internal token storage if it's not already present. The command's hash is calculated based on the current tree base.
command | A pointer to a null-terminated character array (RAM string) representing the command. Example: "*IDN?" , "MEASure:VOLTage?" , "CONFigure:VOLTage 10.5" . |
caller | A function pointer to the callback function (SCPI_caller_t) to be executed when the command is received. |
Definition at line 277 of file scpi_parser.cpp.
void SCPI_Parser::RegisterCommand | ( | const __FlashStringHelper * | command, |
SCPI_caller_t | caller ) |
RegisterCommand version with Flash strings (F() macro) support.
This overload of RegisterCommand accepts a Flash string helper object, allowing the command string to reside in program memory (Flash), saving RAM.
command | A Flash string helper object (created using the F() macro) representing the command. Example: F("*IDN?") . |
caller | A function pointer to the callback function (SCPI_caller_t). |
Definition at line 335 of file scpi_parser.cpp.
void SCPI_Parser::RegisterCommand | ( | const char * | command, |
SCPI_caller_t | caller ) |
RegisterCommand version with RAM string support.
This overload of RegisterCommand accepts a constant character pointer for the command string, which resides in RAM.
command | A constant pointer to a null-terminated character array (RAM string) representing the command. Example: "*IDN?" . |
caller | A function pointer to the callback function (SCPI_caller_t). |
Definition at line 316 of file scpi_parser.cpp.
void SCPI_Parser::SetCommandTreeBase | ( | char * | tree_base | ) |
Changes the base of the command tree for subsequent RegisterCommand calls.
This method sets a new tree base for organizing SCPI commands hierarchically. The tree base is a sequence of command keywords separated by colons (':'). Subsequent calls to RegisterCommand will have their command hashes calculated relative to this tree base. An empty string ""
resets the tree base to the root.
tree_base | A pointer to a null-terminated character array (RAM string) representing the new tree base. Example: "SYSTem:COMMunication" . |
Definition at line 204 of file scpi_parser.cpp.
void SCPI_Parser::SetCommandTreeBase | ( | const __FlashStringHelper * | tree_base | ) |
SetCommandTreeBase version with Flash strings (F() macro) support.
This overload of SetCommandTreeBase accepts a Flash string helper object, allowing the tree base string to reside in program memory (Flash), saving RAM.
tree_base | A Flash string helper object (created using the F() macro) representing the new tree base. Example: F("SYSTem:LED") . |
Definition at line 255 of file scpi_parser.cpp.
void SCPI_Parser::SetCommandTreeBase | ( | const char * | tree_base | ) |
SetCommandTreeBase version with RAM string support.
This overload of SetCommandTreeBase accepts a constant character pointer for the tree base string, which resides in RAM.
tree_base | A constant pointer to a null-terminated character array (RAM string) representing the new tree base. Example: "SYSTem:LED" . |
Definition at line 237 of file scpi_parser.cpp.
void SCPI_Parser::SetErrorHandler | ( | SCPI_caller_t | caller | ) |
Sets the function to be used as the error handler.
This method allows the user to specify a custom error handling function that will be called when the parser encounters an error, such as an unknown command or a buffer overflow. The error handler function receives the parsed commands, parameters (if available), and the communication interface as arguments.
caller | A function pointer to the error handler callback function (SCPI_caller_t). Example: &myErrorHandler . |
Definition at line 355 of file scpi_parser.cpp.
|
protected |
Length of the message buffer.
Definition at line 104 of file scpi_parser.h.
|
protected |
Pointers to the functions to be called when a valid command is received.
Definition at line 144 of file scpi_parser.h.
|
protected |
Number of registered commands.
Definition at line 140 of file scpi_parser.h.
scpi_hash_t SCPI_Parser::hash_magic_number = 37 |
Magic number used for hashing the commands.
Definition at line 85 of file scpi_parser.h.
scpi_hash_t SCPI_Parser::hash_magic_offset = 7 |
Magic offset used for hashing the commands.
Definition at line 87 of file scpi_parser.h.
|
protected |
Hash reserved for invalid commands.
Definition at line 129 of file scpi_parser.h.
ErrorCode SCPI_Parser::last_error = ErrorCode::NoError |
Variable that holds the last error code.
Definition at line 75 of file scpi_parser.h.
|
protected |
Max number of registered commands.
Definition at line 108 of file scpi_parser.h.
|
protected |
Max number of valid tokens.
Definition at line 106 of file scpi_parser.h.
|
protected |
Length of the readed message.
Definition at line 152 of file scpi_parser.h.
|
protected |
Message buffer.
Definition at line 150 of file scpi_parser.h.
|
protected |
Varible used for checking timeout errors.
Definition at line 154 of file scpi_parser.h.
unsigned long SCPI_Parser::timeout = 10 |
Timeout, in miliseconds, for GetMessage and ProcessInput.
Definition at line 89 of file scpi_parser.h.
|
protected |
Storage for tokens.
Definition at line 138 of file scpi_parser.h.
|
protected |
Number of stored tokens.
Definition at line 136 of file scpi_parser.h.
|
protected |
TreeBase branch's hash used when calculating hashes (0 for root)
Definition at line 146 of file scpi_parser.h.
|
protected |
TreeBase branch's length (0 for root)
Definition at line 148 of file scpi_parser.h.
|
protected |
Hash result for unknown commands.
Definition at line 127 of file scpi_parser.h.
|
protected |
Registered commands' hash storage.
Definition at line 142 of file scpi_parser.h.