Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unspected crc error #64

Open
coyool opened this issue Dec 16, 2018 · 3 comments
Open

unspected crc error #64

coyool opened this issue Dec 16, 2018 · 3 comments

Comments

@coyool
Copy link

coyool commented Dec 16, 2018

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()
@OrangeTux
Copy link
Collaborator

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

@coyool
Copy link
Author

coyool commented Dec 18, 2018

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

@coyool
Copy link
Author

coyool commented Dec 18, 2018

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)

hmaerki pushed a commit to hmaerki/fork-uModbus-obsolete that referenced this issue Dec 31, 2023
…ature/support-mip-installation

Support mip installation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants