Coder Social home page Coder Social logo

miguelvalentine / goplc Goto Github PK

View Code? Open in Web Editor NEW
24.0 6.0 9.0 87 KB

A Lightweight Ethernet/IP API written to interface with Rockwell ControlLogix/CompactLogix Controllers.

License: MIT License

Go 100.00%
go golang ethernet ethernet-ip go-ethernet rockwell allen-bradley plc automation

goplc's Introduction

Repositorie moved to go-cip

golang version MIT license GitHub stars

goplc

A golang based API(Originator) for interfacing with Rockwell Control/CompactLogix PLCs.

NOTE: goplc running in a single go route and never block your process, you need to block it by other way.

Thanks

node-ethernet-ip project gave me a lot of inspiration.

The API

The Basics

Hello world

import (
    "github.com/MiguelValentine/goplc"
)

func test(){
    //IP Slot Config
    plc, _ := NewOriginator("1.1.1.1", 1, nil)
    plc.OnConnected = func() {
        log.Printf("%+v\n",plc.Controller)
        // &{VendorID:1 DeviceType:14 ProductCode:53 Major:32 Minor:11 Status:12400 SerialNumber:1881423856 Version:32.11 Name:Emulator R32.11}
    }
    
    _ = plc.Connect()
}

Config

import (
    "github.com/MiguelValentine/goplc"
)

func test(){
    cfg := DefaultConfig()

    //Reconnection Interval  default will not automatically reconnect
    cfg.ReconnectionInterval = time.Second

    //Logger  default no log
    cfg.Logger = log.New(os.Stdout, "", log.LstdFlags)

    //default 0xAF12
    cfg.Port = 1111

    plc, _ := NewOriginator("1.1.1.1", 1, cfg)
    plc.OnConnected = func() {
        log.Printf("%+v\n",plc.Controller)
        // &{VendorID:1 DeviceType:14 ProductCode:53 Major:32 Minor:11 Status:12400 SerialNumber:1881423856 Version:32.11 Name:Emulator R32.11}
    }
    
    _ = plc.Connect()
}
ListAllTags
import (
    "github.com/MiguelValentine/goplc"
)

func testListAllTags() {
    cfg := DefaultConfig()
    plc, _ := NewOriginator("1.1.1.1", 1, cfg)
    plc.OnConnected = func() {
        plc.ListAllTags(0)
        //  2020/08/24 17:20:10 Map:Local : (0x1069)
        //  2020/08/24 17:20:10 Task:MainTask : (0x1070)
        //  2020/08/24 17:20:10 Program:MainProgram : (0x1068)
        //  2020/08/24 17:20:10 TESTA : DINT(0xc4)
        //  2020/08/24 17:20:10 TESTB : (0xafce)
        //  2020/08/24 17:20:10 TESTC : DINT(0xc4)
        //  2020/08/24 17:20:10 TESTD : REAL(0xca)
        //  2020/08/24 17:20:10 TESTE : (0x8fce)
    }

    _ = plc.Connect()
}

ReadAndWriteTag

NOTE: Currently, the Tag Class only supports Atomic datatypes(SINT, INT, DINT, REAL, BOOL) and String Read(not support string write).support for ARRAY, and UDTs are in the plans and coming soon!

import (
    "github.com/MiguelValentine/goplc"
    "github.com/MiguelValentine/goplc/tag"
)

func testTag() {
    tagA := tag.NewTag("TESTA")

    //OnData will be called every time you sync
    //Try to use OnChange if not necessary
    tagA.OnData = func(value interface{}) {
        log.Println(value)
        //1237
        //1238
    }

    plc, _ := NewOriginator("1.1.1.1", 1, nil)
    plc.OnConnected = func() {
        log.Printf("%+v\n", plc.Controller)
        plc.ReadTag(tagA).Then(func() {
            tagA.SetValue(int32(1238))
            plc.WriteTag(tagA).Then(func() {
                plc.ReadTag(tagA)
            })
        })
    }

    _ = plc.Connect()
}

TagGroup

import (
    "github.com/MiguelValentine/goplc"
    "github.com/MiguelValentine/goplc/tag"
    "github.com/MiguelValentine/goplc/tagGroup"
)

func testTagGroup() {
    cfg := DefaultConfig()

    tagA := tag.NewTag("TESTA")
    tagA.OnChange = func(value interface{}) {
        log.Println(value)
    }
    tagB := tag.NewTag("TESTB")
    tagB.OnChange = func(value interface{}) {
        log.Println(value)
    }
    tg := &tagGroup.TagGroup{}
    tg.Add(tagA)
    tg.Add(tagB)

    plc, _ := NewOriginator("1.1.1.1", 1, cfg)
    plc.OnConnected = func() {
        plc.ReadTagGroup(tg)
    }

    _ = plc.Connect()
}

Sync

import (
    "github.com/MiguelValentine/goplc"
    "github.com/MiguelValentine/goplc/tag"
    "github.com/MiguelValentine/goplc/tagGroup"
)

func testTagGroup() {
    cfg := DefaultConfig()

    tagA := tag.NewTag("TESTA")
    tagA.OnChange = func(value interface{}) {
        log.Printf("%s => %f\n", tagA.Name(), value)
    }

    tagB := tag.NewTag("TESTB")
    tagB.OnChange = func(value interface{}) {
        log.Printf("%s => %f\n", tagB.Name(), value)
    }
    tg := &tagGroup.TagGroup{}
    tg.Add(tagA)
    tg.Add(tagB)

    plc, _ := NewOriginator("1.1.1.1", 1, cfg)
    plc.OnConnected = func() {
        //setTimeInterval
        plc.ReadTagGroupInterval(tg, time.Millisecond*50)
    }

    _ = plc.Connect()
}

License

This project is licensed under the MIT License - see the LICENCE file for details

goplc's People

Contributors

miguelvalentine avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.