Vrekrer SCPI parser  0.5
A simple SCPI parser for small Arduino projects.
SCPI_Parser Class Reference

Main class of the Vrekrer_SCPI_Parser library. More...

#include <Vrekrer_scpi_parser.h>

Public Types

enum class  ErrorCode { NoError , UnknownCommand , Timeout , BufferOverflow }
 SCPI Error codes. More...
 

Public Member Functions

 SCPI_Parser ()
 SCPI_Parser constructor. More...
 
void SetCommandTreeBase (char *tree_base)
 Change the TreeBase for the next RegisterCommand calls. More...
 
void SetCommandTreeBase (const char *tree_base)
 SetCommandTreeBase version with RAM string support. More...
 
void SetCommandTreeBase (const __FlashStringHelper *tree_base)
 SetCommandTreeBase version with Flash strings (F() macro) support. More...
 
void RegisterCommand (char *command, SCPI_caller_t caller)
 Registers a new valid command and associate a procedure to it. More...
 
void RegisterCommand (const char *command, SCPI_caller_t caller)
 RegisterCommand version with RAM string support. More...
 
void RegisterCommand (const __FlashStringHelper *command, SCPI_caller_t caller)
 RegisterCommand version with Flash strings (F() macro) support. More...
 
void SetErrorHandler (SCPI_caller_t caller)
 Set the function to be used by the error handler. More...
 
void Execute (char *message, Stream &interface)
 Process a message and execute it a valid command is found. More...
 
void ProcessInput (Stream &interface, const char *term_chars)
 Gets a message from a Stream interface and execute it. More...
 
char * GetMessage (Stream &interface, const char *term_chars)
 Gets a message from a Stream interface. More...
 
void PrintDebugInfo ()
 Prints registered tokens and command hashes to the serial interface.
 

Data Fields

ErrorCode last_error = ErrorCode::NoError
 Variable that holds the last error code.
 

Protected Member Functions

void AddToken_ (char *token)
 Add a token to the tokens' storage.
 
SCPI_HASH_TYPE GetCommandCode_ (SCPI_Commands &commands)
 Get a hash from a valid command. More...
 

Protected Attributes

uint8_t tokens_size_ = 0
 
char * tokens_ [SCPI_MAX_TOKENS]
 
uint8_t codes_size_ = 0
 
SCPI_HASH_TYPE valid_codes_ [SCPI_MAX_COMMANDS]
 
SCPI_caller_t callers_ [SCPI_MAX_COMMANDS+1]
 
SCPI_HASH_TYPE tree_code_ = 0
 
char msg_buffer_ [SCPI_BUFFER_LENGTH]
 
uint8_t message_length_ = 0
 
unsigned long time_checker_
 

Detailed Description

Main class of the Vrekrer_SCPI_Parser library.

Member Enumeration Documentation

◆ ErrorCode

SCPI Error codes.

Enumerator
NoError 

No error.

UnknownCommand 

Unknown command received.

Timeout 

Timeout before receiving the termination chars.

BufferOverflow 

Message buffer overflow.

Constructor & Destructor Documentation

◆ SCPI_Parser()

SCPI_Parser::SCPI_Parser ( )

SCPI_Parser constructor.

Example:
SCPI_Parser my_instrument;

Member Function Documentation

◆ Execute()

void SCPI_Parser::Execute ( char *  message,
Stream &  interface 
)

Process a message and execute it a valid command is found.

Parameters
messageMessage to be processed.
interfaceThe source of the message.

Commands and parameters are extracted from the message,
if a valid command is found, its associated procedure is executed.
The command' tokens and parameters, and the interface is passed to the executed procedure.

See also
GetMessage

◆ GetCommandCode_()

SCPI_HASH_TYPE SCPI_Parser::GetCommandCode_ ( SCPI_Commands commands)
protected

Get a hash from a valid command.

Parameters
commandsKeywords of a command
Returns
hash

Returnn 0 if the command contains keywords not registered as tokens.
The hash is calculated including the TreeBase hash.

See also
SetCommandTreeBase

◆ GetMessage()

char * SCPI_Parser::GetMessage ( Stream &  interface,
const char *  term_chars 
)

Gets a message from a Stream interface.

Parameters
interfaceA Stream interface like Serial or Ethernet.
term_charsTermination chars e.g. "\r\n".
Returns
the read message if the term_chars are found, otherwise NULL.

Reads the available chars in the interface, if the term_chars are found the message is returned, otherwise the return is NULL.
Subsequent calls to this function continues the message reading.
The message is discarded, and the error handler is called if:
A timeout occurs (10 ms without new chars)
The message buffer overflows

◆ ProcessInput()

void SCPI_Parser::ProcessInput ( Stream &  interface,
const char *  term_chars 
)

Gets a message from a Stream interface and execute it.

See also
GetMessage
Execute

◆ RegisterCommand() [1/3]

void SCPI_Parser::RegisterCommand ( char *  command,
SCPI_caller_t  caller 
)

Registers a new valid command and associate a procedure to it.

Parameters
commandNew valid command.
callerProcedure associated to the valid command.

◆ RegisterCommand() [2/3]

void SCPI_Parser::RegisterCommand ( const __FlashStringHelper *  command,
SCPI_caller_t  caller 
)

RegisterCommand version with Flash strings (F() macro) support.

Example:
my_instrument.RegisterCommand(F("*IDN?"), &Identify);

◆ RegisterCommand() [3/3]

void SCPI_Parser::RegisterCommand ( const char *  command,
SCPI_caller_t  caller 
)

RegisterCommand version with RAM string support.

Example:
my_instrument.RegisterCommand("*IDN?", &Identify);
For lower RAM usage use the Flash strings version.

◆ SetCommandTreeBase() [1/3]

void SCPI_Parser::SetCommandTreeBase ( char *  tree_base)

Change the TreeBase for the next RegisterCommand calls.

Parameters
tree_baseTreeBase to be used.
An empty string "" sets the TreeBase to root.

◆ SetCommandTreeBase() [2/3]

void SCPI_Parser::SetCommandTreeBase ( const __FlashStringHelper *  tree_base)

SetCommandTreeBase version with Flash strings (F() macro) support.

Example:
my_instrument.SetCommandTreeBase(F("SYSTem:LED"));

◆ SetCommandTreeBase() [3/3]

void SCPI_Parser::SetCommandTreeBase ( const char *  tree_base)

SetCommandTreeBase version with RAM string support.

Example:
my_instrument.SetCommandTreeBase("SYSTem:LED");
For lower RAM usage use the Flash strings version.

◆ SetErrorHandler()

void SCPI_Parser::SetErrorHandler ( SCPI_caller_t  caller)

Set the function to be used by the error handler.

Example:
my_instrument.SetErrorHandler(&myErrorHandler);