Hi,
I have created a python script which reads data out of a solar charge controller via an rs-485 serial connection. I can read individual registers correctly but if try to read several non-contiguous registers using a for loop then every second reply has an extra starting character (which is the function code). Below is the code I am using
import struct
import socket
import serial
import serial.rs485
from pymodbus3.utilities import compute_crc
from pymodbus3.constants import Defaults
from pymodbus3.factory import ClientDecoder
from pymodbus3.exceptions import ParameterException
from pymodbus3.exceptions import ConnectionException
from transaction_xtra import FifoTransactionManager
from transaction_xtra import DictTransactionManager
from transaction_xtra import ModbusSocketFramer, ModbusBinaryFramer
from transaction_xtra import ModbusAsciiFramer, ModbusRtuFramer
from common import ModbusClientMixin
from sync import BaseModbusClient
from sync import ModbusSerialClient
#---------------------------------------------------------------------------#
configure the client logging
#---------------------------------------------------------------------------#
import logging
import sqlite3
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
input_register_defs = {'PV_array_V_reg' : 0x3100,
'PV_array_I_reg' : 0x3101,
'PV_array_Wl_reg' : 0x3102,
'Batt_V_reg' : 0x3104,
'Batt_I_reg' : 0x3105,
'Batt_Wl_reg' : 0x3106,
'Batt_SOC_reg': 0x311a,
'Daily_kWh_reg' : 0x330c,
'Monthly_kWh_reg' : 0x330e,
'Yearly_kWh_reg' : 0x3310,
'Total_kWh_reg' : 0x3312}
client = ModbusSerialClient(method='rtu',
port='/dev/ttySC0',
timeout=1,
baudrate=115200,
retry_on_empty=1)
client.rs485_mode=serial.rs485.RS485Settings(rts_level_for_tx='True',
rts_level_for_rx='False')
for reg in input_register_defs:
res = client.read_input_registers(input_register_defs[reg], 1, unit=1)
print('client receive is ', ':'.join('{:02x}'.format(x) for x in client.receive(16)))>
I have added some debugging statements to transaction.py and get output as follows:
DEBUG:transaction_xtra:Running transaction 1
DEBUG:transaction_xtra:getting transaction 1
client receive is 02:3c:9f:e8:58 ## correct
DEBUG:transaction_xtra:Running transaction 2
frame size is 6 ## different to transaction 1, this time it is calling check_frame() in transaction.py
data is b'\x01\x04\x01'
DEBUG:transaction_xtra:getting transaction 2
client receive is 04:02:09:ec:be:ed ## response begins with 04 which is the function code throwing everything off by 1 position
DEBUG:transaction_xtra:Running transaction 3
DEBUG:transaction_xtra:getting transaction 3
client receive is 02:17:75:77:27 ## same as transaction 1
DEBUG:transaction_xtra:Running transaction 4
frame size is 6 ## Same as transaction 2
data is b'\x01\x04\x01'
DEBUG:transaction_xtra:getting transaction 4
client receive is 04:02:01:02:39:61
DEBUG:transaction_xtra:Running transaction 5
DEBUG:transaction_xtra:getting transaction 5
client receive is 02:00:0b:f8:f7
DEBUG:transaction_xtra:Running transaction 6
frame size is 6
data is b'\x01\x04\x01'
DEBUG:transaction_xtra:getting transaction 6
client receive is 04:02:3c:d2:28:6d
DEBUG:transaction_xtra:Running transaction 7
DEBUG:transaction_xtra:getting transaction 7
client receive is 02:02:6e:39:bc
DEBUG:transaction_xtra:Running transaction 8
frame size is 6
data is b'\x01\x04\x01'
DEBUG:transaction_xtra:getting transaction 8
client receive is 04:02:09:80:be:c0
DEBUG:transaction_xtra:Running transaction 9
DEBUG:transaction_xtra:getting transaction 9
client receive is 02:9b:8b:92:67
DEBUG:transaction_xtra:Running transaction 10
frame size is 6
data is b'\x01\x04\x01'
DEBUG:transaction_xtra:getting transaction 10
client receive is 04:02:00:17:f9:3e
DEBUG:transaction_xtra:Running transaction 11
DEBUG:transaction_xtra:getting transaction 11
client receive is 02:9b:8b:92:67
I would be very grateful for any suggestions as to what is going on here and while I could just bodge up a way around the issue I'm keen to correct it and not have to deal with it when I come to other devices.
Thanks for your time and please let me know if you need any more info,
cheers
Hugh
Hi,
I have created a python script which reads data out of a solar charge controller via an rs-485 serial connection. I can read individual registers correctly but if try to read several non-contiguous registers using a for loop then every second reply has an extra starting character (which is the function code). Below is the code I am usingI have added some debugging statements to transaction.py and get output as follows:
DEBUG:transaction_xtra:Running transaction 1 DEBUG:transaction_xtra:getting transaction 1 client receive is 02:3c:9f:e8:58 ## correct DEBUG:transaction_xtra:Running transaction 2 frame size is 6 ## different to transaction 1, this time it is calling check_frame() in transaction.py data is b'\x01\x04\x01' DEBUG:transaction_xtra:getting transaction 2 client receive is 04:02:09:ec:be:ed ## response begins with 04 which is the function code throwing everything off by 1 position DEBUG:transaction_xtra:Running transaction 3 DEBUG:transaction_xtra:getting transaction 3 client receive is 02:17:75:77:27 ## same as transaction 1 DEBUG:transaction_xtra:Running transaction 4 frame size is 6 ## Same as transaction 2 data is b'\x01\x04\x01' DEBUG:transaction_xtra:getting transaction 4 client receive is 04:02:01:02:39:61 DEBUG:transaction_xtra:Running transaction 5 DEBUG:transaction_xtra:getting transaction 5 client receive is 02:00:0b:f8:f7 DEBUG:transaction_xtra:Running transaction 6 frame size is 6 data is b'\x01\x04\x01' DEBUG:transaction_xtra:getting transaction 6 client receive is 04:02:3c:d2:28:6d DEBUG:transaction_xtra:Running transaction 7 DEBUG:transaction_xtra:getting transaction 7 client receive is 02:02:6e:39:bc DEBUG:transaction_xtra:Running transaction 8 frame size is 6 data is b'\x01\x04\x01' DEBUG:transaction_xtra:getting transaction 8 client receive is 04:02:09:80:be:c0 DEBUG:transaction_xtra:Running transaction 9 DEBUG:transaction_xtra:getting transaction 9 client receive is 02:9b:8b:92:67 DEBUG:transaction_xtra:Running transaction 10 frame size is 6 data is b'\x01\x04\x01' DEBUG:transaction_xtra:getting transaction 10 client receive is 04:02:00:17:f9:3e DEBUG:transaction_xtra:Running transaction 11 DEBUG:transaction_xtra:getting transaction 11 client receive is 02:9b:8b:92:67
I would be very grateful for any suggestions as to what is going on here and while I could just bodge up a way around the issue I'm keen to correct it and not have to deal with it when I come to other devices. Thanks for your time and please let me know if you need any more info, cheers Hugh