Skip to content

unspected crc error #64

Open
Open

Description

@coyool

run as rtu serve
when writing to 233 station, 33000
write a data(<0,eg:-100) to addr 33000 (larger than 2^15), will surely cause "salve failure error"
all code as below:


import threading
import serial
from collections import defaultdict
from umodbus.server.serial import get_server
from umodbus.server.serial.rtu import RTUServer
import threadstop

running = threading.Thread()
comName = 'com31'
port = serial.Serial(comName)
port.timeout = 10
port.baudrate = 9600
port.stopbits = 1
port.parity = 'N'

data_rd = defaultdict(int)
data_wr = defaultdict(int)
link = get_server(RTUServer, port)


@link.route(slave_ids=[233], function_codes=[3], addresses=list(range(33000, 33000 + 1000)))
def read_data_store(slave_id, function_code, address):
    """" Return value of address. """
    # return data_store[address]
    return data_rd[address] + address


@link.route(slave_ids=[233], function_codes=[6, 16], addresses=list(range(33000, 33000 + 1000)))
def write_data_store(slave_id, function_code, address, value):
    """" Set value for address. """
    data_rd[address] = value  #OK
    data_wr[address] = value #ERROR

def poll():
    try:
        link.serve_forever()
    finally:
        link.shutdown()

def connect(comx):
    running = threading.Thread(target=poll)
    running.start()


def reconnect(comx):
    try:
        threadstop(running)
        link.shutdown()
    finally:
        comName = comx
        port = serial.Serial(comName)
        link.shutdown()



if __name__ == '__main__':
    try:
        link.serve_forever()
    finally:
        link.shutdown()

Activity

  1. OrangeTux commented on Dec 17, 2018

    @OrangeTux
    Collaborator

    Thanks for reporting this issue. Could you add a stacktrace?

  2. coyool commented on Dec 18, 2018

    @coyool
    Author

    sorry , I am very new to python, I don't know how to get stacktrace.
    you can get this error simply by putting a value of 33333 into the address 33333 which belong to station 233

  3. coyool commented on Dec 18, 2018

    @coyool
    Author

    I problely found the bug is that:
    when you use struct.pack, you must make sure all the values in the list are between 0~65535(for H pack)
    stand modbus NEVER predefine the type of values( negative or not), so please remove the signed config, and make it more compatble
    \umodbus\functions.py line 728

            val = []
            for d in data:
                tmp = d
                if tmp<0 :
                    tmp = 65536+d
                val.append(tmp)
            return struct.pack(fmt, self.function_code, len(data) * 2, *val)
    
  4. added a commit that references this issue on Dec 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions