-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
4 changed files
with
195 additions
and
64 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,87 @@ | ||
package can | ||
|
||
// --------------------------------------------------------------------------------------- | ||
// imports | ||
// --------------------------------------------------------------------------------------- | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// --------------------------------------------------------------------------------------- | ||
// types | ||
// --------------------------------------------------------------------------------------- | ||
|
||
type Frame struct { | ||
Id int | ||
Length int | ||
Data [8]byte | ||
|
||
Timestamp time.Time | ||
} | ||
|
||
// --------------------------------------------------------------------------------------- | ||
// public functions | ||
// --------------------------------------------------------------------------------------- | ||
|
||
func ParseFrame(cmd string) (*Frame, error) { | ||
msg := Frame{} | ||
|
||
if len(cmd) < 5 { | ||
return nil, errors.New("cmd not long enough") | ||
} | ||
|
||
id, err := strconv.ParseUint(cmd[1:4], 16, 16) | ||
if err != nil { | ||
return nil, err | ||
} | ||
msg.Id = int(id) | ||
|
||
length, err := strconv.ParseUint(cmd[4:5], 10, 8) | ||
if err != nil { | ||
return nil, err | ||
} | ||
msg.Length = int(length) | ||
|
||
if len(cmd) < 5+msg.Length*2 { | ||
return nil, errors.New("cmd not long enough") | ||
} | ||
|
||
for i := 0; i < msg.Length; i++ { | ||
d, err := strconv.ParseUint(cmd[5+2*i:5+2*i+2], 16, 8) | ||
if err != nil { | ||
return nil, err | ||
} | ||
msg.Data[i] = byte(d) | ||
} | ||
|
||
return &msg, nil | ||
} | ||
|
||
// --------------------------------------------------------------------------------------- | ||
// public members | ||
// --------------------------------------------------------------------------------------- | ||
|
||
func (f *Frame) ToUint64() uint64 { | ||
u := uint64(0) | ||
|
||
for i := 0; i < f.Length; i++ { | ||
|
||
u |= uint64(f.Data[i]) << (i * 8) | ||
} | ||
|
||
return u | ||
} | ||
|
||
func (f *Frame) String() string { | ||
str := fmt.Sprintf("%x [%d] ", f.Id, f.Length) | ||
|
||
for i := 0; i < f.Length; i++ { | ||
str += fmt.Sprintf("%02x ", f.Data[i]) | ||
} | ||
|
||
return str | ||
} |
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,31 @@ | ||
package can | ||
|
||
// --------------------------------------------------------------------------------------- | ||
// imports | ||
// --------------------------------------------------------------------------------------- | ||
|
||
import "testing" | ||
|
||
// --------------------------------------------------------------------------------------- | ||
// tests | ||
// --------------------------------------------------------------------------------------- | ||
|
||
func TestFrame_ToUint64(t *testing.T) { | ||
msg := Frame{Length: 2, Data: [8]byte{1, 0, 0, 0, 0, 0, 0, 0}} | ||
if msg.ToUint64() != 1 { | ||
t.Errorf("ToUint64() should be 1 but was %d", msg.ToUint64()) | ||
return | ||
} | ||
|
||
msg = Frame{Length: 2, Data: [8]byte{4, 0, 0, 0xff, 0, 0, 0, 0}} | ||
if msg.ToUint64() != 4 { | ||
t.Errorf("ToUint64() should be 0 but was %d: length attribut not considered?", msg.ToUint64()) | ||
return | ||
} | ||
|
||
msg = Frame{Length: 2, Data: [8]byte{4, 1, 0, 0, 0, 0, 0, 0}} | ||
if msg.ToUint64() != 260 { | ||
t.Errorf("ToUint64() should be 260 but was %d: length attribut not considered?", msg.ToUint64()) | ||
return | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.