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

Register 0 not accepted. #33

Closed
Nemecsek opened this issue Jul 11, 2019 · 2 comments · Fixed by #35
Closed

Register 0 not accepted. #33

Nemecsek opened this issue Jul 11, 2019 · 2 comments · Fixed by #35

Comments

@Nemecsek
Copy link

Nemecsek commented Jul 11, 2019

This could be related to #28.

I am reading a Janitza UMG604.
It provides a register 0 but I cannot read it (I can request any register > 0):

Endian::$defaultEndian = Endian::BIG_ENDIAN;

$fc3 = ReadRegistersBuilder::newReadHoldingRegisters('tcp://X.X.X.X', 
    ->int64(0, "realtime")
    ->int32(4, "sys time")
    ->build();

The error is Address out of bounds and comes from ReadHoldingRegistersResponse.php, line 180:

/**
     * @param $firstWordAddress
     * @return QuadWord
     */
    public function getQuadWordAt(int $firstWordAddress)
    {
        $address = ($firstWordAddress - $this->getStartAddress()) * 2;
        $byteCount = $this->getByteCount();
        if ($address < 0 || ($address + 8) > $byteCount) {
            throw new InvalidArgumentException('address out of bounds');
        }
        return new QuadWord(substr($this->data, $address, 8));
    }

The error happens if we read more than 1 register and the first starts at 0.

These are some variables in getQuadWordAt (getDoubleWordAt for examples 5 and 6, because getQuadWordAt is not called) according to my tests.

It looks like $byteCount is wrong but I couldn't find the issue:

// example 1: SINGLE REGISTER 0: OK
$fc3 = ReadRegistersBuilder::newReadHoldingRegisters('tcp://10.10.3.2:502', 1)
    ->int64( 0, "realtime")
// getStartAddress 0, address 0, bytecount 8, firstWordAddress 0

// example 2: 2 REGISTERS FROM 0: ERROR
// the size of first register has been lost!
$fc3 = ReadRegistersBuilder::newReadHoldingRegisters('tcp://10.10.3.2:502', 1)
    ->int64( 0, "realtime")
    ->int32( 4, "sys time")
// getStartAddress 4, address -8, bytecount 4, firstWordAddress 0

// example 3: 3 REGISTERS FROM 0: ERROR
// the size of first register has been lost!
$fc3 = ReadRegistersBuilder::newReadHoldingRegisters('tcp://10.10.3.2:502', 1)
    ->int64( 0, "realtime")
    ->int32( 4, "sys time")
    ->int16( 6, "day")
// getStartAddress 4, address -8, bytecount 6, firstWordAddress 0

// example 4: 4 REGISTERS FROM 0: ERROR
// the size of first register has been lost!
$fc3 = ReadRegistersBuilder::newReadHoldingRegisters('tcp://10.10.3.2:502', 1)
    ->int64( 0, "realtime")
    ->int32( 4, "sys time")
    ->int16( 6, "day")
    ->int16( 7, "month")
// getStartAddress 4, address -8, bytecount 8, firstWordAddress 0

// example 5: 3 REGISTERS FROM 4: OK
// faulty register has been commented out
$fc3 = ReadRegistersBuilder::newReadHoldingRegisters('tcp://10.10.3.2:502', 1)
//  ->int64( 0, "realtime")  
    ->int32( 4, "sys time")
    ->int16( 6, "day")
    ->int16( 7, "month")
// getStartAddress 4, address 0, bytecount 8, firstWordAddress 4

// example 6: 3 REGISTERS FROM 0, WITH HOLE: ERROR
// the first register has been lost; the error is obviously not related to int64 alone
$fc3 = ReadRegistersBuilder::newReadHoldingRegisters('tcp://10.10.3.2:502', 1)
    ->int32( 0, "dummy 32bit register") 
    ->int16( 6, "day")
    ->int16( 7, "month")
// getStartAddress 6, address -12, bytecount 4, firstWordAddress 0
aldas added a commit that referenced this issue Jul 11, 2019
…dress incorrectly when value is 0.

also fixes case when addresses overlap then quantity was calculated incorrectly
aldas added a commit that referenced this issue Jul 11, 2019
…dress incorrectly when value is 0.

also fixes case when addresses overlap then quantity was calculated incorrectly
aldas added a commit that referenced this issue Jul 11, 2019
1. it calculates start address incorrectly when value is 0.
2. also fixes case when addresses overlap then quantity was calculated incorrectly
@aldas aldas closed this as completed in #35 Jul 11, 2019
aldas added a commit that referenced this issue Jul 11, 2019
1. it calculates start address incorrectly when value is 0.
2. also fixes case when addresses overlap then quantity was calculated incorrectly
@aldas
Copy link
Owner

aldas commented Jul 11, 2019

Hi! thanks reporting the problem. your described problem is in method where addresses are splitted into separate requests.

4157cab#diff-f5ead4071f50c5dfafce5c6459c3ac7dR50

I have made a bug fix. could you checkout master/latest changes see if it helps.

@aldas aldas reopened this Jul 11, 2019
@Nemecsek
Copy link
Author

It works!
Problem has been solved. Very fast, thank you.

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

Successfully merging a pull request may close this issue.

2 participants