Skip to content
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
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "CANOPENREST",
"name": "GoCanOpen",
"image": "devcontainer-go-dev:latest",
"runArgs": [
"--name=CANOPENREST_Devcontainer"
"--name=GoCanOpen_Devcontainer"
],
"customizations": {
// Configure properties specific to VS Code.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/jaster-prj/go-canopen
go 1.23.4

require (
github.com/angelodlfrtr/go-can v0.0.4
github.com/google/uuid v1.6.0
github.com/jaster-prj/go-can v0.0.5
github.com/stretchr/testify v1.10.0
gopkg.in/ini.v1 v1.67.0
)
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
github.com/angelodlfrtr/go-can v0.0.4 h1:/Wj3ZQ2YWG2dfoHqZHchfP2sZ/UccbrEBTV0nj/HRKo=
github.com/angelodlfrtr/go-can v0.0.4/go.mod h1:e9NV8485R6+MwVHY1424SxwpCzsIRYd6I8noHvgHzQQ=
github.com/angelodlfrtr/serial v0.0.0-20190912094943-d028474db63c h1:Q5NYSNKJn3QdfE4q7yG6m+r0OLFrsC94KDQPGqj5OkE=
github.com/angelodlfrtr/serial v0.0.0-20190912094943-d028474db63c/go.mod h1:kGJNzwu4M6uVHZPXuE7m6+f3BvYrhFH76a90n69CxEk=
github.com/brutella/can v0.0.1/go.mod h1:NYDxbQito3w4+4DcjWs/fpQ3xyaFdpXw/KYqtZFU98k=
github.com/brutella/can v0.0.2 h1:8TyjZrBZSwQwSr5x3U9KtKzGW8HNE/NpUgsNcYDAVIM=
github.com/brutella/can v0.0.2/go.mod h1:NYDxbQito3w4+4DcjWs/fpQ3xyaFdpXw/KYqtZFU98k=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jaster-prj/go-can v0.0.5 h1:yoaEW7m2PQumU8gps9riQFvZBoPJIliTIBfJubNjSuo=
github.com/jaster-prj/go-can v0.0.5/go.mod h1:oebpIJeUB7V0IFtF2MaDYI/pOkd2xDT5qphlGxoOsGE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
Expand Down
23 changes: 23 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package canopen

import (
"time"

"github.com/jaster-prj/go-can"
)

type ISDOClient interface {
FindName(name string) DicObject
Read(index uint16, subIndex uint8) ([]byte, error)
Send(req []byte, expectFunc networkFramesChanFilterFunc, timeout *time.Duration, retryCount *int) (*can.Frame, error)
SendRequest(req []byte) error
Write(index uint16, subIndex uint8, forceSegment bool, data []byte) error
}

type INode interface {
GetId() int
FindName(name string) DicObject
Send(arbID uint32, data []byte) error
AcquireFramesChanFromNetwork(filterFunc networkFramesChanFilterFunc) *NetworkFramesChan
ReleaseFramesChanFromNetwork(id string)
}
4 changes: 2 additions & 2 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"sync"
"time"

"github.com/angelodlfrtr/go-can"
"github.com/google/uuid"
"github.com/jaster-prj/go-can"
"github.com/jaster-prj/go-canopen/utils"
)

Expand Down Expand Up @@ -192,7 +192,7 @@ func (network *Network) AcquireFramesChan(filterFunc networkFramesChanFilterFunc
frameChan := &NetworkFramesChan{
ID: chanID,
Filter: filterFunc,
C: make(chan *can.Frame),
C: make(chan *can.Frame, 10),
}

// Append network.FramesChans
Expand Down
2 changes: 1 addition & 1 deletion network_frames_chan.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package canopen

import (
"github.com/angelodlfrtr/go-can"
"github.com/jaster-prj/go-can"
)

type networkFramesChanFilterFunc *(func(*can.Frame) bool)
Expand Down
2 changes: 1 addition & 1 deletion nmt_master.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"errors"
"time"

"github.com/angelodlfrtr/go-can"
"github.com/google/uuid"
"github.com/jaster-prj/go-can"
)

var NMTStates = map[int]string{
Expand Down
38 changes: 38 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package canopen

import "errors"

// Node is a canopen node
type Node struct {
// Each node has an id, which is ArbitrationID & 0x7F
Expand All @@ -23,6 +25,42 @@ func NewNode(id int, network *Network, objectDic *DicObjectDic) *Node {
return node
}

// GetId returns Node ID
func (node *Node) GetId() int {
return node.ID
}

// FindName gets DicObject from ObjectDic
func (node *Node) FindName(name string) DicObject {
if node.ObjectDic == nil {
return nil
}
return node.ObjectDic.FindName(name)
}

// Send sends Frame with arbitration ID by connected network
func (node *Node) Send(arbID uint32, data []byte) error {
if node.Network == nil {
return errors.New("Network not defined")
}
return node.Network.Send(arbID, data)
}

// AcquireFramesChanFromNetwork gets new Channel for given FilterFunc
func (node *Node) AcquireFramesChanFromNetwork(filterFunc networkFramesChanFilterFunc) *NetworkFramesChan {
if node.Network == nil {
return nil
}
return node.Network.AcquireFramesChan(filterFunc)
}

// ReleaseFramesChanFromNetwork free channel with given id from network
func (node *Node) ReleaseFramesChanFromNetwork(id string) {
if node.Network != nil {
node.Network.ReleaseFramesChan(id)
}
}

// SetNetwork set node.Network to the desired network
func (node *Node) SetNetwork(network *Network) {
node.Network = network
Expand Down
2 changes: 1 addition & 1 deletion pdo_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"sync"
"time"

"github.com/angelodlfrtr/go-can"
"github.com/google/uuid"
"github.com/jaster-prj/go-can"
)

const (
Expand Down
65 changes: 35 additions & 30 deletions sdo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"time"

"github.com/angelodlfrtr/go-can"
"github.com/jaster-prj/go-can"
)

const (
Expand All @@ -26,29 +26,29 @@ const (

// SDOClient represent an SDO client
type SDOClient struct {
Node *Node
Node INode
RXCobID uint32
TXCobID uint32
SendQueue []string
}

func NewSDOClient(node *Node) *SDOClient {
func NewSDOClient(node INode) *SDOClient {
return &SDOClient{
Node: node,
RXCobID: uint32(0x600 + node.ID),
TXCobID: uint32(0x580 + node.ID),
RXCobID: uint32(0x600 + node.GetId()),
TXCobID: uint32(0x580 + node.GetId()),
SendQueue: []string{},
}
}

// SendRequest to network bus
func (sdoClient *SDOClient) SendRequest(req []byte) error {
return sdoClient.Node.Network.Send(sdoClient.RXCobID, req)
return sdoClient.Node.Send(sdoClient.RXCobID, req)
}

// FindName find an sdo object from object dictionary by name
func (sdoClient *SDOClient) FindName(name string) DicObject {
if ob := sdoClient.Node.ObjectDic.FindName(name); ob != nil {
if ob := sdoClient.Node.FindName(name); ob != nil {
ob.SetSDO(sdoClient)
return ob
}
Expand Down Expand Up @@ -83,12 +83,22 @@ func (sdoClient *SDOClient) Send(
retryCount = &rtc
}

framesChan := sdoClient.Node.Network.AcquireFramesChan(expectFunc)
var expectSdoFunc networkFramesChanFilterFunc
if expectFunc != nil {
expectSdoFilterFunc := func(frm *can.Frame) bool {
arbitrationId := frm.ArbitrationID
if arbitrationId != sdoClient.TXCobID {
return false
}
return (*expectFunc)(frm)
}
expectSdoFunc = &expectSdoFilterFunc
}
framesChan := sdoClient.Node.AcquireFramesChanFromNetwork(expectSdoFunc)
defer sdoClient.Node.ReleaseFramesChanFromNetwork(framesChan.ID)

// Retry loop
remainingCount := *retryCount
var frm *can.Frame

for {
if remainingCount == 0 {
break
Expand All @@ -100,32 +110,27 @@ func (sdoClient *SDOClient) Send(

timer := time.NewTimer(*timeout)

select {
case <-timer.C:
// Double timeout for each retry
newTimeout := *timeout * 2
timeout = &newTimeout
case fr := <-framesChan.C:
frm = fr
loop := true
for {
if !loop {
break
}
select {
case <-timer.C:
// Double timeout for each retry
newTimeout := *timeout * 2
timeout = &newTimeout
loop = false
case fr := <-framesChan.C:
return fr, nil
}
}

timer.Stop()
remainingCount--

if frm != nil {
break
}
}

// Release data chan
sdoClient.Node.Network.ReleaseFramesChan(framesChan.ID)

// If no frm, timeout execeded
if frm == nil {
return nil, errors.New("timeout execeded")
}

return frm, nil
return nil, errors.New("timeout execeded")
}

// Read sdo
Expand Down
2 changes: 1 addition & 1 deletion sdo_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/binary"
"errors"

"github.com/angelodlfrtr/go-can"
"github.com/jaster-prj/go-can"
)

type SDOReader struct {
Expand Down
Loading