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

Read boolean #2

Closed
iotgopher opened this issue Mar 16, 2018 · 7 comments
Closed

Read boolean #2

iotgopher opened this issue Mar 16, 2018 · 7 comments

Comments

@iotgopher
Copy link

please can you advise if there is a function to read booleans?
@robinson

@robinson
Copy link
Owner

@RTmaster read boolean is similar with read any types of data:
buf := make([]byte, 255)
err := client.AGReadDB(address, start, size, buf)
if err != nil {
t.Fatal(err)
}
here you got the the buf, which is an array of byte, then just get the value from buf:
var s7 gos7.Helper
var result boolean
s7.GetValueAt(buf, 0, &result)
Now you got the result is a boolean.
Cheers, R

@sandeep51989
Copy link

I see there to be a problem with this approach. If the boolean is given in a byte it works. How can we have get booleans at the bit position in a byte. For example if i want to get the boolean value at 180 the position at bit 6.

According to examples given.
s7.GetValueAt(buf, 180, &result)

All the function does is read the record at position byte 180 but how do we get the value at the bit position off the byte.

@robinson
Copy link
Owner

@sandeep51989: more specific: the buf is an array of bytes which stores the return values - maybe: bool, int8, int16...so on. The values arrange from the position 0. The helper: S7.GetValueAt will return an interface of value.

@pizzalord22
Copy link

sorry for reviving this old thread but when i read a byte containing 8 bool bits it returns 1 byte in the buffer array instead of 1 byte for each bool value. so i can not get individual bool values

@petar-dambovaliev
Copy link
Contributor

petar-dambovaliev commented Oct 4, 2019

@pizzalord22
This will give you the specific boolean you want from the byte.
myBool = (byte & (1 << bitPos-1)) != 0;

@petar-dambovaliev
Copy link
Contributor

I have added the boolean support in the helper.
#17

robinson pushed a commit that referenced this issue Dec 16, 2020
Merge pull request #29 from ThinkontrolSY/master
@sunblack110
Copy link

It's good , but how about write a boolean to PLC, do we need to read it in a byte first, then change a bit in the byte, then we write to PLC , it's not a good idea.

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

6 participants