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

os.getpid() doesn't fit into word, problem with sequence number #73

Open
jk987 opened this issue Apr 14, 2022 · 0 comments
Open

os.getpid() doesn't fit into word, problem with sequence number #73

jk987 opened this issue Apr 14, 2022 · 0 comments

Comments

@jk987
Copy link

jk987 commented Apr 14, 2022

File "C:\\Python27\\lib\\site-packages\\pycomm\\ab_comm\\slc.py", line 242, in read_tag
    self._last_sequence = pack_uint(Base._get_sequence())
File "C:\\Python27\\lib\\site-packages\\pycomm\\cip\\cip_base.py", line 63, in pack_uint
    return struct.pack(\'<H\', n)
error: ushort format requires 0 <= number <= USHRT_MAX

Method Base._get_sequence() uses os.getpid() but this can be (and is) higher than 65535.

Btw. I don't like this logic in Base._get_sequence() much:

        if Base._sequence < 65535:
            Base._sequence += 1
        else:
            Base._sequence = getpid()

I would understand that if sequence number overflows then it should start from zero again, shouldn't it?
So, in Base.__init__() I recommend to put:

        if Base._sequence == 0:
            Base._sequence = getpid() & 0xFFFF
        else:
            Base._sequence = Base._get_sequence()

and in Base._get_sequence():

        if Base._sequence < 65535:
            Base._sequence += 1
        else:
            Base._sequence = 0

Could be written also like Base._sequence = (Base._sequence + 1) & 0xFFFF which would handle even negative numbers if such thing can happen.

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

1 participant