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))
}

Comments  

# mboers 2020-04-06 17:22
Excellent post!
And to think, I always thought "Go" was the board-game mastered by Alpha-Go. :)
Question: Can Go call simple functions in an ANSI-C library?
# beld02 2020-04-06 18:02
Well, I'm certainly no expert in Go as I'm just Go-ing on this journey as well. If I understand your question well, you've made some functions in and ANSI-C and want to call them in Go? I can only point you to a post that I found on the web https://www.activestate.com/blog/porting-library-cli-app-c-go/ maybe the answer is in there ;)
# mboers 2020-04-07 06:22
Thanks Dries. I was thinking more of this kind of thing (which I also found via google):
https://golang.org/cmd/cgo/
I ask because it is possible to access the PLC's I/O using ANSI-C function calls, as demonstrated in the SampleRuntime example on Github. This could be another way to read/write I/O data from a Go program.
# beld02 2020-04-07 07:17
Interesting, yet another new opportunity for the PLCnext! I'm adding it on my bucket-list!