本文整理汇总了Python中modbus_tk.LOGGER.debug方法的典型用法代码示例。如果您正苦于以下问题:Python LOGGER.debug方法的具体用法?Python LOGGER.debug怎么用?Python LOGGER.debug使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类modbus_tk.LOGGER
的用法示例。
在下文中一共展示了LOGGER.debug方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_request
# 需要导入模块: from modbus_tk import LOGGER [as 别名]
# 或者: from modbus_tk.LOGGER import debug [as 别名]
def handle_request(self, request_pdu, broadcast=False):
"""
parse the request pdu, makes the corresponding action
and returns the response pdu
"""
# thread-safe
with self._data_lock:
try:
retval = call_hooks("modbus.Slave.handle_request", (self, request_pdu))
if retval is not None:
return retval
# get the function code
(function_code,) = struct.unpack(">B", request_pdu[0])
# check if the function code is valid. If not returns error response
if function_code not in self._fn_code_map:
raise ModbusError(defines.ILLEGAL_FUNCTION)
# if read query is broadcasted raises an error
cant_be_broadcasted = (
defines.READ_COILS,
defines.READ_DISCRETE_INPUTS,
defines.READ_INPUT_REGISTERS,
defines.READ_HOLDING_REGISTERS,
)
if broadcast and (function_code in cant_be_broadcasted):
raise ModbusInvalidRequestError("Function %d can not be broadcasted" % function_code)
# execute the corresponding function
response_pdu = self._fn_code_map[function_code](request_pdu)
if response_pdu:
if broadcast:
call_hooks("modbus.Slave.on_handle_broadcast", (self, response_pdu))
LOGGER.debug("broadcast: %s", get_log_buffer("!!", response_pdu))
return ""
else:
return struct.pack(">B", function_code) + response_pdu
raise Exception("No response for function %d" % function_code)
except ModbusError, excpt:
LOGGER.debug(str(excpt))
call_hooks("modbus.Slave.on_exception", (self, function_code, excpt))
return struct.pack(">BB", function_code + 128, excpt.get_exception_code())
示例2: _handle
# 需要导入模块: from modbus_tk import LOGGER [as 别名]
# 或者: from modbus_tk.LOGGER import debug [as 别名]
def _handle(self, request):
"""handle a received sentence"""
if self._verbose:
LOGGER.debug(get_log_buffer("-->", request))
# gets a query for analyzing the request
query = self._make_query()
retval = call_hooks("modbus.Server.before_handle_request", (self, request))
if retval:
request = retval
response = self._databank.handle_request(query, request)
retval = call_hooks("modbus.Server.after_handle_request", (self, response))
if retval:
response = retval
if response and self._verbose:
LOGGER.debug(get_log_buffer("<--", response))
return response
示例3: _read_registers
# 需要导入模块: from modbus_tk import LOGGER [as 别名]
# 或者: from modbus_tk.LOGGER import debug [as 别名]
def _read_registers(self, block_type, request_pdu):
"""read the value of holding and input registers"""
(starting_address, quantity_of_x) = struct.unpack(">HH", request_pdu[1:5])
if (quantity_of_x <= 0) or (quantity_of_x > 125):
# maximum allowed size is 125 registers in one reading
LOGGER.debug("quantity_of_x is %d", quantity_of_x)
raise ModbusError(defines.ILLEGAL_DATA_VALUE)
# look for the block corresponding to the request
block, offset = self._get_block_and_offset(block_type, starting_address, quantity_of_x)
# get the values
values = block[offset : offset + quantity_of_x]
# write the response header
response = struct.pack(">B", 2 * quantity_of_x)
# add the values of every register on 2 bytes
for reg in values:
fmt = "H" if self.unsigned else "h"
response += struct.pack(">" + fmt, reg)
return response
示例4: execute
# 需要导入模块: from modbus_tk import LOGGER [as 别名]
# 或者: from modbus_tk.LOGGER import debug [as 别名]
#.........这里部分代码省略.........
# SubFuncCode are in starting_address
pdu = struct.pack(">BH", function_code, starting_address)
if len(output_value) > 0:
for j in output_value:
# copy data in pdu
pdu += struct.pack(">B", j)
if not data_format:
data_format = ">" + (len(output_value) * "B")
if expected_length < 0:
# No length was specified and calculated length can be used:
# slave + func + SubFunc1 + SubFunc2 + Data + crc1 + crc2
expected_length = len(output_value) + 6
elif function_code == defines.READ_WRITE_MULTIPLE_REGISTERS:
is_read_function = True
byte_count = 2 * len(output_value)
pdu = struct.pack(
">BHHHHB",
function_code,
starting_address,
quantity_of_x,
defines.READ_WRITE_MULTIPLE_REGISTERS,
len(output_value),
byte_count,
)
for j in output_value:
fmt = "H" if j >= 0 else "h"
# copy data in pdu
pdu += struct.pack(">" + fmt, j)
if not data_format:
data_format = ">" + (quantity_of_x * "H")
if expected_length < 0:
# No lenght was specified and calculated length can be used:
# slave + func + bytcodeLen + bytecode x 2 + crc1 + crc2
expected_length = 2 * quantity_of_x + 5
else:
raise ModbusFunctionNotSupportedError("The {0} function code is not supported. ".format(function_code))
# instantiate a query which implements the MAC (TCP or RTU) part of the protocol
query = self._make_query()
# add the mac part of the protocol to the request
request = query.build_request(pdu, slave)
# send the request to the slave
retval = call_hooks("modbus.Master.before_send", (self, request))
if retval is not None:
request = retval
if self._verbose:
LOGGER.debug(get_log_buffer("-> ", request))
self._send(request)
call_hooks("modbus.Master.after_send", (self,))
if slave != 0:
# receive the data from the slave
response = self._recv(expected_length)
retval = call_hooks("modbus.Master.after_recv", (self, response))
if retval is not None:
response = retval
if self._verbose:
LOGGER.debug(get_log_buffer("<- ", response))
# extract the pdu part of the response
response_pdu = query.parse_response(response)
# analyze the received data
(return_code, byte_2) = struct.unpack(">BB", response_pdu[0:2])
if return_code > 0x80:
# the slave has returned an error
exception_code = byte_2
raise ModbusError(exception_code)
else:
if is_read_function:
# get the values returned by the reading function
byte_count = byte_2
data = response_pdu[2:]
if byte_count != len(data):
# the byte count in the pdu is invalid
raise ModbusInvalidResponseError(
"Byte count is {0} while actual number of bytes is {1}. ".format(byte_count, len(data))
)
else:
# returns what is returned by the slave after a writing function
data = response_pdu[1:]
# returns the data as a tuple according to the data_format
# (calculated based on the function or user-defined)
result = struct.unpack(data_format, data)
if nb_of_digits > 0:
digits = []
for byte_val in result:
for i in xrange(8):
if len(digits) >= nb_of_digits:
break
digits.append(byte_val % 2)
byte_val = byte_val >> 1
result = tuple(digits)
return result