Skip to content

Commit

Permalink
load symbols and memory from string
Browse files Browse the repository at this point in the history
  • Loading branch information
jpodeszfa committed Oct 31, 2022
1 parent 2823b30 commit f243003
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 38 deletions.
15 changes: 13 additions & 2 deletions ab.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"fmt"
"math/rand"
"net"
"os"
"sync"
"time"
)
Expand Down Expand Up @@ -44,8 +45,8 @@ type PLC struct {
Timeout time.Duration
}

// Init initialize library. Must be called first.
func Init(eds string) (*PLC, error) {
// Init initializes library. Must be called first.
func Init(eds []byte) (*PLC, error) {
var p PLC
p.Class = make(map[int]*Class)
p.tags = make(map[string]*Tag)
Expand All @@ -61,6 +62,16 @@ func Init(eds string) (*PLC, error) {
return &p, nil
}

// InitEDS initializes library. Must be called first.
func InitEDS(eds string) (*PLC, error) {
edsB, err := os.ReadFile(eds)
if err != nil {
return nil, err
}

return Init(edsB)
}

func (p *PLC) debug(args ...interface{}) {
if p.Verbose {
fmt.Println(args...)
Expand Down
2 changes: 1 addition & 1 deletion c/libplcconnector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func call(service int, status int, tag *plc.Tag) {

//export plcconnector_init
func plcconnector_init() {
p, _ = plc.Init("")
p, _ = plc.Init(nil)
}

//export plcconnector_set_verbose
Expand Down
28 changes: 11 additions & 17 deletions eds.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,26 @@ func (p *PLC) getEDSInt(section string, item string) (int, error) {
return 0, errNotFound
}

func (p *PLC) loadEDS(fn string) error {
func (p *PLC) loadEDS(eds []byte) error {
var (
orig_f []byte
f []byte
err error
gz = false
f []byte
err error
gz = false
)

if fn == "" {
orig_f = defEDS
} else {
orig_f, err = os.ReadFile(fn)
if err != nil {
return err
}
if len(eds) == 0 {
eds = defEDS
}

if len(orig_f) >= 10 && orig_f[0] == 0x1f && orig_f[1] == 0x8b {
if len(eds) >= 10 && eds[0] == 0x1f && eds[1] == 0x8b {
gz = true
f, err = loadGzip(orig_f)
f, err = loadGzip(eds)
if err != nil {
return nil
}
} else {
f = make([]byte, len(orig_f))
copy(f, orig_f)
f = make([]byte, len(eds))
copy(f, eds)
}

p.eds = make(map[string]map[string]string)
Expand Down Expand Up @@ -262,7 +256,7 @@ func (p *PLC) loadEDS(fn string) error {
gz.Close()
in.data = buf.Bytes()
} else {
in.data = orig_f
in.data = eds
}

chksum := 0
Expand Down
6 changes: 3 additions & 3 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,20 @@ func main() {
if len(os.Args) >= 2 {
eds = os.Args[1]
}
p, err := plc.Init(eds)
p, err := plc.InitEDS(eds)
if err != nil {
fmt.Println(err)
return
}

if len(os.Args) >= 3 {
err = p.ImportJSON(os.Args[2])
err = p.ImportSymbols(os.Args[2])
if err != nil {
fmt.Println(err)
return
}
if len(os.Args) >= 4 {
err = p.ImportMemoryJSON(os.Args[3])
err = p.ImportMemory(os.Args[3])
if err != nil {
fmt.Println(err)
return
Expand Down
41 changes: 26 additions & 15 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,10 @@ type JS struct {
Templates map[string]jsTemplates `json:"templates"`
}

// ImportJSON .
func (p *PLC) ImportJSON(file string) error {
data, err := os.ReadFile(file)
if err != nil {
return err
}
// UseSymbols .
func (p *PLC) UseSymbols(symbols string) error {
var db JS
err = json.Unmarshal(data, &db)
err := json.Unmarshal([]byte(symbols), &db)
if err != nil {
return err
}
Expand Down Expand Up @@ -117,19 +113,24 @@ func (p *PLC) ImportJSON(file string) error {
return nil
}

// ImportSymbols .
func (p *PLC) ImportSymbols(file string) error {
data, err := os.ReadFile(file)
if err != nil {
return err
}
return p.UseSymbols(string(data))
}

type memJS struct {
Rx []uint8 `json:"rx"`
Read bool `json:"read"`
}

// ImportMemoryJSON .
func (p *PLC) ImportMemoryJSON(file string) error {
data, err := os.ReadFile(file)
if err != nil {
return err
}
// UseMemory .
func (p *PLC) UseMemory(memory string) error {
db := make(map[string]memJS)
err = json.Unmarshal(data, &db)
err := json.Unmarshal([]byte(memory), &db)
if err != nil {
return err
}
Expand All @@ -145,7 +146,7 @@ func (p *PLC) ImportMemoryJSON(file string) error {
if c.Read {
if len(c.Rx) != len(tag.data) {
if p.Verbose {
fmt.Println(file, "data length mismatch", n)
fmt.Println("memory JSON data length mismatch", n)
}
continue
// return errors.New("data length mismatch " + n)
Expand All @@ -156,3 +157,13 @@ func (p *PLC) ImportMemoryJSON(file string) error {

return nil
}

// ImportMemory .
func (p *PLC) ImportMemory(file string) error {
data, err := os.ReadFile(file)
if err != nil {
return err
}

return p.UseMemory(string(data))
}

0 comments on commit f243003

Please sign in to comment.