Hi all,
We met a issue on reading string tag value from S7-1500 PLC with Ignition S7-1500 driver, the data in the PLC is Chinese character store in DB, and ignition get the string as garbled text. We tried to decode and encode with different character set(UTF8, iso-8859-1, gbk), but failed to get the right Chinese string as recipe name from equipment.
Below is the custom script we using for the string extraction: defined a function to decode the string
def decode_siemens_gbk(raw_string):
# 1. 安全检查:空值处理 Check if it is null
if raw_string is None or str(raw_string).strip() == "":
return ""
# 2. 如果传入的已经是 unicode 对象 if the raw string is unicode
if isinstance(raw_string, unicode):
try:
# 尝试用 iso-8859-1 还原底层字节流(针对 2_V7 这种真正的单字节乱码)
# Try using iso-8859-1 to restore the underlying byte stream
raw_bytes = raw_string.encode('iso-8859-1')
return raw_bytes.decode('gbk', 'ignore')
except UnicodeEncodeError:
# 【触发报错的地方】如果含有 latin-1 无法编码的字符,说明里面已经有真正的中文字符
# If it contains characters that latin-1 cannot encode, it means there are already real Chinese characters inside
try:
# 尝试直接用 gbk 重新解码(以防万一)Try directly decoding with gbk
return raw_string.encode('utf-8').decode('gbk', 'ignore')
except Exception:
# 如果怎么转都错,说明这个字符串在 Ignition 里已经是正确的中文了,直接返回原值
#If no matter how you convert it, it means the string is already in correct Chinese in Ignition,
#and it returns the original value directly
return raw_string
# 3. 如果传入的是标准 str (字节流) If the input is a standard str (byte stream)
else:
try:
return str(raw_string).decode('gbk', 'ignore')
except Exception:
return str(raw_string)
Chinese garbled text.txt (1.6 KB)
Do you have any experience on this topic, or any suggestion to help us? Thanks for your support and kindness in advance!




