PLCnext on LinkedInPLCnext on Instagram  PLCnext on YouTube Github PLCnext CommunityStore PLCnext Community

 

 How to create a Blog Entry

Getting started with Go on PLCnext

This blog will guide you through the first steps on building an application with Go on the PLCnext. For those who are unfamiliar with Go, I like to cite the reference website www.golang.org

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

A particular reason to like Go is that it is easy to create web applications but still compiles to a static binary, hence you don't have to install packages on the PLCnext controller to deploy the program. 


This blog will only handle the compiling for the PLCnext with two small programs. A classic HelloWorld program to be sure the compiling goes well and a small REST API call to show the power of Go. In the making of this blog I've used:

  • An Ubuntu 18.04 LTS machine with VS an Go installed to create the programs
  • WinSCP to transfer the binary to the controller
  • Putty to execute the programs.

Ok, let's get started with our HelloWorld application. Create a new file called HelloWorld.go and paste the code from the HelloWorld example inside the file. Save the file and open your terminal and make sure you're in the right directory.

With the command below you'll build and run your Go code. If everything was installed correctly Hello World should be displayed in your terminal. You can use this command when you're still building your application.

go run HelloWorld.go 

Like i said, it is also possible to build the code to a static binary. This is done with the first command below, the next command runs the newly created binary. If everything goes well, hello world is again shown in your terminal.

go build HelloWorld.go
./HelloWorld

When you try to run this binary on the PLCnext controller you get:

./HelloWorld: cannot execute binary file: Exec format error

This is because the architecture of the Ubuntu machine and PLCnext don't match. Luckily it is fairly easy to create a binary that works for the PLCnext (but take note that this new binary won't run on the Ubuntu machine). When you set the build environment like described bellow. Go will create a binary that is suited for the PLCnext.

env GOOS=linux GOARCH=arm GOARM=7 go build


and for our example it would be 

env GOOS=linux GOARCH=arm GOARM=7 go build HelloWorld.go


When you transfer this file to the controller and run it you should see hello world popping up once again, if so, congratulations. You've written your first Go program for a PLCnext controller! Take a look at the REST API call and modify it to suit your needs!

Problem with the previous step?
Have you made the file executable with chmod +x HelloWorld ?


Hello World example

package main

import "fmt"

func main() {
fmt.Println("hello world")
}
go

REST API Call ( HTTP GET)

package main

import (
"crypto/tls"
"io/ioutil"
"log"
"net/http"
)

func main() {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} // Disable TLS check due to bad certificate
MakeRequest()
}

func MakeRequest() {
resp, err := http.Get("https://192.168.18.10/_pxc_api/api/variables?paths=Arp.Plc.Eclr/MainInstance.bInputs") // change to your IP adress and variables
if err != nil {
log.Fatalln(err)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}

log.Println(string(body))
}