我正在寻找好几天,如果我使用了错误的搜索字符串,我很抱歉。
我想使用“pymodbus.utilities.computeCRC”计算CRC值,以便与使用专有代码的Modbus设备进行通信,因此不能使用默认的“读取寄存器”功能。
正确的CRC是0x34bb
问题是,它只适用于版本1,将十六进制值直接指向computeCRC,但没有“变量”选项正常工作。
import pymodbus.utilities
meins=[]
meins.append('10')
meins.append('33')
meins.append('01')
meins.append('01')
meins1=''
for i in meins:
meins1 = meins1 + "\\x" + i
meins2='\x10\x33\x01\x01'
meins3=bytearray.fromhex("10330101")
crc=hex(pymodbus.utilities.computeCRC('\x10\x33\x01\x01'))
print(crc)
crc=hex(pymodbus.utilities.computeCRC(meins))
print(crc)
crc=hex(pymodbus.utilities.computeCRC(meins1))
print(crc)
crc=hex(pymodbus.utilities.computeCRC(meins2))
print(crc)
crc=hex(pymodbus.utilities.computeCRC(meins3))
print(crc)
输出:
答案 0 :(得分:0)
以下是如何根据需要使用CRC生成器,根据python版本(2.7或3.x),如何根据需要传递输入。
注意python3和pymodbus需要bytestrings来生成CRC。
import pymodbus.utilities
from pymodbus.compat import int2byte
meins=[]
meins.append(0x10)
meins.append(0x33)
meins.append(0x01)
meins.append(0x01)
meins1=b''
for i in meins:
meins1 = meins1 + int2byte(i)
meins2=b'\x10\x33\x01\x01'
meins3=bytearray.fromhex("10330101")
crc=hex(pymodbus.utilities.computeCRC(b'\x10\x33\x01\x01'))
print(crc)
crc=hex(pymodbus.utilities.computeCRC(meins))
print(crc)
crc=hex(pymodbus.utilities.computeCRC(meins1))
print(crc)
crc=hex(pymodbus.utilities.computeCRC(meins2))
print(crc)
crc=hex(pymodbus.utilities.computeCRC(meins3))
print(crc)
0x34bb
0x34bb
0x34bb
0x34bb
0x34bb
import pymodbus.utilities
from pymodbus.compat import int2byte
meins=[]
meins.append(int2byte(0x10))
meins.append(int2byte(0x33))
meins.append(int2byte(0x1))
meins.append(int2byte(0x1))
meins1=''
for i in meins:
meins1 = meins1 + i
meins2='\x10\x33\x01\x01'
meins3=bytearray.fromhex("10330101")
meins3=''.join(str(meins3))
crc=hex(pymodbus.utilities.computeCRC('\x10\x33\x01\x01'))
print(crc)
crc=hex(pymodbus.utilities.computeCRC(meins))
print(crc)
crc=hex(pymodbus.utilities.computeCRC(meins1))
print(crc)
crc=hex(pymodbus.utilities.computeCRC(meins2))
print(crc)
crc=hex(pymodbus.utilities.computeCRC(meins3))
print(crc)
0x34bb
0x34bb
0x34bb
0x34bb
0x34bb
答案 1 :(得分:0)
有关信息,请参阅"丑蟒蛇初学者"我对设备的请求版本。在这种情况下,该请求将电池逆变器的输出功率设置为50瓦。该值正确存储在设备中。
<强>代码强>
import minimalmodbus
import struct
import sys
import string
import serial
import pymodbus.payload
import time
#Wattage to program to the modbus device in hex format
watt = '{0:04X}'.format(50)
#combine the wattage to the rest of the string
string='103F0D'+watt+'01AE01F400960000000000'
#convert to a format computeCRC can work with
stringh=bytearray.fromhex(string)
stringh=''.join(str(stringh))
crc=pymodbus.utilities.computeCRC(stringh)
#convert crc to HEX String
crc='{0:04X}'.format(crc)
#combine Payload with CRC
string=string+crc
#debug:
#print(string)
#convert to a Format ser.write can send
stringh=bytearray.fromhex(string)
stringh=''.join(str(stringh))
ser = serial.Serial('/dev/ttyUSB0', 57600)
ser.write(stringh)
time.sleep(0.1)
anz=ser.inWaiting()
str=ser.read(anz)
for char in str:
mm = int(char.encode('hex'), 16)
print(hex(mm))
# Wait for inverter to accept command
time.sleep(1)
# Status 3E
# Send Status check command
ser.write("\x10\x3E\x01\x01\xA5\x78")
# wait for answer
time.sleep(0.1)
# how many bytes came back
anz=ser.inWaiting()
# read all bytes
str=ser.read(anz)
# extract the 2 bytes with the current load setting
str1=str[13]+str[14]
# convert
p=int(str1.encode('hex'), 16) / 10
print(p)
<强>输出强>
0x10
0x3f
0x1
0x1
0xf4
0xb8
50
字节是
0xf4 0xb8 - CRC
50 - 从设备读取的值