修复DB100ReaderThread线程和write_genetic操作PLC线程打架。原因是write_genetic没有加线程锁
This commit is contained in:
@ -251,90 +251,6 @@ class Snap7Client:
|
|||||||
self.connected = False
|
self.connected = False
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# def read_generic(self, db_number, offset, data_type, count=1):
|
|
||||||
# """
|
|
||||||
# 通用读取接口,支持多种数据类型
|
|
||||||
# Args:
|
|
||||||
# db_number: DB块编号
|
|
||||||
# offset: 起始偏移量(字节或位,对于bool类型)
|
|
||||||
# data_type: 数据类型 ('bool', 'byte', 'int', 'word', 'real', 'dint', 'dword')
|
|
||||||
# count: 要读取的数据个数
|
|
||||||
# Returns:
|
|
||||||
# 解析后的数据(单个值或值列表),失败返回None
|
|
||||||
# """
|
|
||||||
# if not self.connected and not self.connect():
|
|
||||||
# self.logger.warning(f"Read failed: not connected to {self.ip}")
|
|
||||||
# return None
|
|
||||||
#
|
|
||||||
# try:
|
|
||||||
# if data_type == 'bool':
|
|
||||||
# # 对于bool,offset是位偏移
|
|
||||||
# byte_offset = offset // 8
|
|
||||||
# bit_offset = offset % 8
|
|
||||||
# # 计算需要读取的字节数
|
|
||||||
# last_bit = bit_offset + count - 1
|
|
||||||
# last_byte = last_bit // 8
|
|
||||||
# total_bytes = last_byte - byte_offset + 1
|
|
||||||
#
|
|
||||||
# # 读取原始字节数据
|
|
||||||
# data = self.read_db(db_number, byte_offset, total_bytes)
|
|
||||||
# if data is None:
|
|
||||||
# return None
|
|
||||||
#
|
|
||||||
# # 解析bool值
|
|
||||||
# result = []
|
|
||||||
# for i in range(count):
|
|
||||||
# current_bit = bit_offset + i
|
|
||||||
# byte_idx = current_bit // 8
|
|
||||||
# bit_idx = current_bit % 8
|
|
||||||
# result.append(bool(data[byte_idx] & (1 << bit_idx)))
|
|
||||||
#
|
|
||||||
# return result[0] if count == 1 else result
|
|
||||||
#
|
|
||||||
# elif data_type == 'byte':
|
|
||||||
# data = self.read_db(db_number, offset, count)
|
|
||||||
# if data is None:
|
|
||||||
# return None
|
|
||||||
# return [data[i] for i in range(count)] if count > 1 else data[0]
|
|
||||||
#
|
|
||||||
# elif data_type in ['int', 'word']:
|
|
||||||
# total_bytes = 2 * count
|
|
||||||
# data = self.read_db(db_number, offset, total_bytes)
|
|
||||||
# if data is None:
|
|
||||||
# return None
|
|
||||||
#
|
|
||||||
# result = []
|
|
||||||
# for i in range(count):
|
|
||||||
# if data_type == 'int':
|
|
||||||
# result.append(get_int(data, i * 2))
|
|
||||||
# else: # word
|
|
||||||
# result.append(get_word(data, i * 2))
|
|
||||||
# return result[0] if count == 1 else result
|
|
||||||
#
|
|
||||||
# elif data_type in ['dint', 'dword', 'real']:
|
|
||||||
# total_bytes = 4 * count
|
|
||||||
# data = self.read_db(db_number, offset, total_bytes)
|
|
||||||
# if data is None:
|
|
||||||
# return None
|
|
||||||
#
|
|
||||||
# result = []
|
|
||||||
# for i in range(count):
|
|
||||||
# if data_type == 'dint':
|
|
||||||
# result.append(get_dint(data, i * 4))
|
|
||||||
# elif data_type == 'dword':
|
|
||||||
# result.append(get_dword(data, i * 4))
|
|
||||||
# else: # real
|
|
||||||
# result.append(get_real(data, i * 4))
|
|
||||||
# return result[0] if count == 1 else result
|
|
||||||
#
|
|
||||||
# else:
|
|
||||||
# self.logger.error(f"Unsupported data type: {data_type}")
|
|
||||||
# return None
|
|
||||||
#
|
|
||||||
# except Exception as e:
|
|
||||||
# self.logger.error(f"Error reading {data_type} from DB{db_number} offset {offset}: {e}")
|
|
||||||
# return None
|
|
||||||
|
|
||||||
def write_generic(self, db_number, offset, data_type, value):
|
def write_generic(self, db_number, offset, data_type, value):
|
||||||
"""
|
"""
|
||||||
通用写入接口,支持多种数据类型
|
通用写入接口,支持多种数据类型
|
||||||
@ -351,6 +267,7 @@ class Snap7Client:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
with self.lock:
|
||||||
if data_type == 'bool':
|
if data_type == 'bool':
|
||||||
# 对于bool,offset是位偏移
|
# 对于bool,offset是位偏移
|
||||||
byte_offset = offset // 8
|
byte_offset = offset // 8
|
||||||
@ -544,7 +461,6 @@ class Snap7Client:
|
|||||||
if db_number == 100:
|
if db_number == 100:
|
||||||
print(f"从缓存文件中读取{db_number}的数据")
|
print(f"从缓存文件中读取{db_number}的数据")
|
||||||
db100_raw = self.read_db100_from_file()
|
db100_raw = self.read_db100_from_file()
|
||||||
|
|
||||||
if db100_raw is not None:
|
if db100_raw is not None:
|
||||||
try:
|
try:
|
||||||
if data_type == 'bool':
|
if data_type == 'bool':
|
||||||
@ -658,6 +574,7 @@ class Snap7Client:
|
|||||||
return [data[i] for i in range(count)] if count > 1 else data[0]
|
return [data[i] for i in range(count)] if count > 1 else data[0]
|
||||||
|
|
||||||
elif data_type in ['int', 'word']:
|
elif data_type in ['int', 'word']:
|
||||||
|
|
||||||
total_bytes = 2 * count
|
total_bytes = 2 * count
|
||||||
data = self.read_db(db_number, offset, total_bytes)
|
data = self.read_db(db_number, offset, total_bytes)
|
||||||
if data is None:
|
if data is None:
|
||||||
@ -687,7 +604,6 @@ class Snap7Client:
|
|||||||
result.append(get_real(data, i * 4))
|
result.append(get_real(data, i * 4))
|
||||||
return result[0] if count == 1 else result
|
return result[0] if count == 1 else result
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.logger.error(f"Unsupported data type: {data_type}")
|
self.logger.error(f"Unsupported data type: {data_type}")
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user