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

fix: isbitset reading bytes in wrong order #17

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packet/readcoilsresponse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ func TestReadCoilsResponse_IsCoilSet(t *testing.T) {
},
{
name: "ok, first byte, second bit, start 1",
whenStartAddress: 1, whenCoilAddress: 2, expect: true},
whenStartAddress: 1, whenCoilAddress: 2, expect: true,
},
{
name: "ok, first byte, second bit, start 100",
whenStartAddress: 100, whenCoilAddress: 101, expect: true,
Expand Down Expand Up @@ -308,7 +309,7 @@ func TestReadCoilsResponse_IsCoilSet(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
given := ReadCoilsResponse{
CoilsByteLength: 2,
Data: []byte{0b10000001, 0b00010010},
Data: []byte{0b00010010, 0b10000001}, // byte0, byte1 - bytes are read left to right
}
result, err := given.IsCoilSet(tc.whenStartAddress, tc.whenCoilAddress)

Expand Down
4 changes: 2 additions & 2 deletions packet/readdiscreteinputsresponse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func TestReadDiscreteInputsResponse_IsInputSet(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
given := ReadDiscreteInputsResponse{
InputsByteLength: 2,
Data: []byte{0b10000001, 0b00010010},
Data: []byte{0b00010010, 0b10000001},
}
result, err := given.IsInputSet(tc.whenStartAddress, tc.whenCoilAddress)

Expand Down Expand Up @@ -369,7 +369,7 @@ func TestReadDiscreteInputsResponse_IsCoilSet(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
given := ReadDiscreteInputsResponse{
InputsByteLength: 2,
Data: []byte{0b10000001, 0b00010010},
Data: []byte{0b00010010, 0b10000001},
}
result, err := given.IsCoilSet(tc.whenStartAddress, tc.whenCoilAddress)

Expand Down
8 changes: 4 additions & 4 deletions packet/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ func ParseRTUResponse(data []byte) (Response, error) {
}
}

// isBitSet checks if N-th bit is set in data. NB: Bits are counted from `startBit` and right to left.
// isBitSet checks if N-th bit is set in data. NB: Bits are counted from `startBit` and left to right (bytes).
func isBitSet(data []byte, startBit uint16, bit uint16) (bool, error) {
targetBit := bit - startBit
targetBit := int(bit) - int(startBit)
if bit < startBit {
return false, errors.New("bit can not be before startBit")
}
if len(data)*8 <= int(targetBit) {
if len(data)*8 <= targetBit {
return false, errors.New("bit value more than data contains bits")
}
nThByte := len(data) - 1 - int(targetBit/8)
nThByte := targetBit / 8
nThBit := targetBit % 8
b := data[nThByte]
return b&(1<<nThBit) != 0, nil
Expand Down
32 changes: 32 additions & 0 deletions packet/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,35 @@ func TestParseRTUResponseWithCRC(t *testing.T) {
})
}
}

func TestIsBitSet(t *testing.T) {
var testCases = []struct {
name string
givenData []byte
whenStartBit uint16
whenBit uint16
expect bool
expectError string
}{
{
name: "ok", // 16 = 0000000000010000
givenData: []byte{16, 0, 0, 128, 0, 33, 192, 128, 29, 0, 0, 32, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 40, 0, 0, 128, 144, 0},
whenStartBit: 6161,
whenBit: 6165, // idx=4 bit, in first byte
expect: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := isBitSet(tc.givenData, tc.whenStartBit, tc.whenBit)

assert.Equal(t, tc.expect, result)
if tc.expectError != "" {
assert.EqualError(t, err, tc.expectError)
} else {
assert.NoError(t, err)
}
})
}
}
Loading