scorpionbasecomm module¶
filename: | scorpionbasecomm.py |
---|---|
author: | roar@tordivel.no |
requirements: | Scorpion 11.0 |
copyright: | 2000-2015 Tordivel AS |
license: | Tordivel AS’ Scorpion Python Module License |
Communication base module for Scorpion. Contains base class for TdvCmd protocol communication modules for difference transport layers, TCP/IP, RS232 etc.
Contains also support class for parsing TdvCmd strings.
The baseclass has internal timer for periodically checking for incoming messages.
On incoming message the execute method is called. This method must be overridden in profile specific protocols for commands not handled by Scorpion standard TdvCmd commands.
- Derived messages to be overridden:
- read - read data from physical layer
- send - send data on physical layer
1.0.0.2, 21nov2015, RL: fixed calling execute even if data=''
1.0.0.1, 16nov2015, RL: created
-
class
scorpionbasecomm.
TdvCmd
(msg)¶ Bases:
object
TdvCmd parser
Parses a text message into TdvCmd command and parameters
TdvCmd format: “cmd;key=value;key=value;...;key=value”
-
msg
¶ string
the origin message
-
cmd
¶ cmd part of msg
-
CMD
¶ cmd part of msg in UPPERCASE
-
params
¶ params part of msg
-
values
¶ params as dictionary
-
VALUES
¶ params as dictionary with keys in UPPERCASE
-
iscmd
(cmd)¶ case insensitive check for command
Parameters: cmd (string) – case insensitive command Returns: equal – cmd is equal Return type: bool
-
has_value
(key)¶ check if cmd has value
Parameters: key (string) – case insensitive value name Returns: contained – whether msg contains key - case insensitive Return type: boolean Example
if cmd.has_value(‘Ready’): ...do some stuff
-
has_values
(keys)¶ check if msg contains values
Parameters: keys (tuple or list) – case insensitive list of keys Returns: contained – whether TdvCmd contains all given keys in list/tuple - case insensitive Return type: boolean Example
if cmd.has_values([‘Ready’,’Status’]): ...do some stuff
-
-
class
scorpionbasecomm.
ScorpionBaseComm
(pollrate=50, verbose=1)¶ Bases:
object
Baseclass for communication
The baseclass must implement a minimum set of ‘virtual’ methods.
The class supports console debug messages.
Parameters: - pollrate (int, optional) – pollrate of checking for incoming data, defalt 50 ms
- verbose (int, optional) – level of console details 0=all, 1=errors, 2=info, 3=detailed
-
verbose
¶ int
level of console details 0=all, 1=errors, 2=info, 3=detailed
-
rx
¶ sting
partial received messsage
-
timer
¶ Timer
internal timer polling for incoming data
-
timestamp
()¶ returns current time as string in format HH:MM:SS
-
printDebug
(msg, level=1)¶ print msg to console if level<=verbose
Parameters: - msg (sting) – the message
- level (int,optional) – verbose level
-
error
(msg)¶ print error message to console
Parameters: msg (sting) – the message
-
info
(msg)¶ prints info message to console
Parameters: msg (string) – the message
-
read
()¶ required virtual method - to be implemented in derived classes
read incoming data if any
if received data is complete message, return data stripped for any stx/etx, else return None
-
send
(data)¶ required virtual method - to be implemented in derived classes
send data over communication media.
Parameters: data (string) – any data, should not contain any protocol stx/etx Returns: success – send result Return type: boolean
-
reply
(send, data)¶ send reply to received data
Parameters: data (string) – any data, should not contain any protocol stx/etx Returns: success – apply result Return type: boolean
-
parse
(data)¶ parse data as TdvCmd
Parameters: data (string) – some text Returns: tuple – splits data into cmd as string and parameters as dictionary Return type: (string,dictionary)
-
execute
(data)¶ virtual method - to be overridden in specialized protocols.
Execute incoming messages. Default implementation call’s Scorpion’s ExecuteCmd(cmd,params).
Parameters: data (string) – complete data/message to be executed, typically TdvCmd message format Note
data should be stripped for any start/stop phrases, only pure data should be executed.
-
checkForData
()¶ periodic check for incoming data
called by internal timer
Returns: received – True if data executed successfully, else False Return type: boolean
-
timerOnTimer
(sender, args)¶ timer callback - checking for incoming data
Parameters: - sender (object) – the method caller
- args (object) – method arguments