Having trouble making a two’s complement of a byte

So, I’m working with Insteon device control code, and I’ve got code that works PRETTY WELL, except for the weird case where it doesn’t. I’m trying to make the correct checksum for i2cs, which is described at the bottom of the page here: http://madreporite.com/insteon/i2cs.html

My code, which works great most of the time, is here: https://github.com/ocdtrekkie/HAController/blob/master/modInsteon.vb#L236

It explodes when the sum of data(6) and data(7) is greater than 255. Such as when I am trying to set a thermostat to 78 degrees, which puts a 108 in (6) and 156 in (7). I tried adding the And 255 from the original example, thinking it would cut down the result to something that fit in a byte (which would make sense as to why it is there), but it didn’t fix the arithmetic overflow exception I am getting in this code.

Extra trivia: 108 is the command to set the “cooling point” temperature on the thermostat. 156 goes in the data(7) field because it’s twice the temperature I’m trying to set it to, Insteon counts in half-degrees. And I’m only adding fields 6 and 7, aka, cmd1 and cmd2, rather than all of the data fields suggested in Madreporite’s site, because I don’t have any code which uses any of the other data fields.

submitted by /u/ocdtrekkie
[link] [comments]

Leave a Reply