-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d38c77
commit 31d1e56
Showing
2 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package lgxtypes | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
) | ||
|
||
type CONTROL struct { | ||
LEN int32 | ||
POS int32 | ||
EN bool // bit 31 | ||
EU bool // bit 30 | ||
DN bool // bit 29 | ||
EM bool // bit 28 | ||
ER bool // bit 27 | ||
UL bool // bit 26 | ||
IN bool // bit 25 | ||
FD bool // bit 24 | ||
} | ||
|
||
func (t CONTROL) Pack(w io.Writer) int { | ||
|
||
var CtrlWord uint32 | ||
if t.EN { | ||
CtrlWord |= 1 << 31 | ||
} | ||
if t.EU { | ||
CtrlWord |= 1 << 30 | ||
} | ||
if t.DN { | ||
CtrlWord |= 1 << 29 | ||
} | ||
if t.EM { | ||
CtrlWord |= 1 << 28 | ||
} | ||
if t.ER { | ||
CtrlWord |= 1 << 27 | ||
} | ||
if t.UL { | ||
CtrlWord |= 1 << 26 | ||
} | ||
if t.IN { | ||
CtrlWord |= 1 << 25 | ||
} | ||
if t.FD { | ||
CtrlWord |= 1 << 24 | ||
} | ||
|
||
err := binary.Write(w, binary.LittleEndian, CtrlWord) | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
err = binary.Write(w, binary.LittleEndian, t.LEN) | ||
if err != nil { | ||
return 4 | ||
} | ||
err = binary.Write(w, binary.LittleEndian, t.POS) | ||
if err != nil { | ||
return 8 | ||
} | ||
|
||
return 12 | ||
} | ||
|
||
func (t *CONTROL) Unpack(r io.Reader) (int, error) { | ||
var CtrlWord uint32 | ||
err := binary.Read(r, binary.LittleEndian, &CtrlWord) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
t.EN = CtrlWord&(1<<31) != 0 | ||
t.EU = CtrlWord&(1<<30) != 0 | ||
t.DN = CtrlWord&(1<<29) != 0 | ||
t.EM = CtrlWord&(1<<28) != 0 | ||
t.ER = CtrlWord&(1<<27) != 0 | ||
t.UL = CtrlWord&(1<<26) != 0 | ||
t.IN = CtrlWord&(1<<25) != 0 | ||
t.FD = CtrlWord&(1<<24) != 0 | ||
|
||
err = binary.Read(r, binary.LittleEndian, &(t.LEN)) | ||
if err != nil { | ||
return 4, err | ||
} | ||
|
||
err = binary.Read(r, binary.LittleEndian, &(t.POS)) | ||
if err != nil { | ||
return 8, err | ||
} | ||
|
||
return 12, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package gologix_tests | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/danomagnum/gologix" | ||
"github.com/danomagnum/gologix/lgxtypes" | ||
) | ||
|
||
func TestControl(t *testing.T) { | ||
var ctrl lgxtypes.CONTROL | ||
|
||
client := gologix.NewClient("192.168.2.241") | ||
err := client.Connect() | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
defer func() { | ||
err := client.Disconnect() | ||
if err != nil { | ||
t.Errorf("problem disconnecting. %v", err) | ||
} | ||
}() | ||
|
||
wants := []lgxtypes.CONTROL{ | ||
{LEN: 8563, POS: 3324, EN: true, EU: false, DN: false, EM: false, ER: false, UL: false, IN: false, FD: false}, | ||
{LEN: 0, POS: 0, EN: false, EU: true, DN: false, EM: false, ER: false, UL: false, IN: false, FD: false}, | ||
{LEN: 0, POS: 0, EN: false, EU: false, DN: true, EM: false, ER: false, UL: false, IN: false, FD: false}, | ||
{LEN: 0, POS: 0, EN: false, EU: false, DN: false, EM: true, ER: false, UL: false, IN: false, FD: false}, | ||
{LEN: 0, POS: 0, EN: false, EU: false, DN: false, EM: false, ER: true, UL: false, IN: false, FD: false}, | ||
{LEN: 0, POS: 0, EN: false, EU: false, DN: false, EM: false, ER: false, UL: true, IN: false, FD: false}, | ||
{LEN: 0, POS: 0, EN: false, EU: false, DN: false, EM: false, ER: false, UL: false, IN: true, FD: false}, | ||
{LEN: 0, POS: 0, EN: false, EU: false, DN: false, EM: false, ER: false, UL: false, IN: false, FD: true}, | ||
} | ||
|
||
for i := range wants { | ||
//have, err := gologix.ReadPacked[udt2](client, "Program:gologix_tests.ReadUDT2") | ||
err = client.Read(fmt.Sprintf("Program:gologix_tests.TestControl[%d]", i), &ctrl) | ||
if err != nil { | ||
t.Errorf("problem reading %d. %v", i, err) | ||
return | ||
} | ||
|
||
compareControl(fmt.Sprintf("test %d", i), wants[i], ctrl, t) | ||
|
||
b := bytes.Buffer{} | ||
_ = gologix.Pack(&b, gologix.CIPPack{}, ctrl) | ||
var ctrl2 lgxtypes.CONTROL | ||
_, err = gologix.Unpack(&b, gologix.CIPPack{}, &ctrl2) | ||
if err != nil { | ||
t.Errorf("problem unpacking %d: %v", i, err) | ||
} | ||
|
||
compareControl(fmt.Sprintf("rebuild test %d", i), ctrl, ctrl2, t) | ||
} | ||
|
||
} | ||
|
||
func compareControl(name string, want, have lgxtypes.CONTROL, t *testing.T) { | ||
|
||
if have.LEN != want.LEN { | ||
t.Errorf("%s LEN mismatch. Have %d want %d", name, have.LEN, want.LEN) | ||
} | ||
if have.POS != want.POS { | ||
t.Errorf("%s POS mismatch. Have %d want %d", name, have.POS, want.POS) | ||
} | ||
if have.EN != want.EN { | ||
t.Errorf("%s EN mismatch. Have %v want %v", name, have.EN, want.EN) | ||
} | ||
if have.EU != want.EU { | ||
t.Errorf("%s EU mismatch. Have %v want %v", name, have.EU, want.EU) | ||
} | ||
if have.DN != want.DN { | ||
t.Errorf("%s DN mismatch. Have %v want %v", name, have.DN, want.DN) | ||
} | ||
if have.EM != want.EM { | ||
t.Errorf("%s EM mismatch. Have %v want %v", name, have.EM, want.EM) | ||
} | ||
if have.ER != want.ER { | ||
t.Errorf("%s ER mismatch. Have %v want %v", name, have.ER, want.ER) | ||
} | ||
if have.UL != want.UL { | ||
t.Errorf("%s UL mismatch. Have %v want %v", name, have.UL, want.UL) | ||
} | ||
if have.IN != want.IN { | ||
t.Errorf("%s IN mismatch. Have %v want %v", name, have.IN, want.IN) | ||
} | ||
if have.FD != want.FD { | ||
t.Errorf("%s FD mismatch. Have %v want %v", name, have.FD, want.FD) | ||
} | ||
|
||
} |