Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Industrial Technology

Build Your First Go Application on PLCnext: A Beginner’s Guide

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

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:

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


Industrial Technology

  1. Connext DDS 101: A Beginner’s Guide to Getting Started
  2. A Practical Guide to Ceramic 3D Printing: Techniques, Materials, and Design Tips
  3. VUnit 101: A Practical Guide to Automated VHDL Verification
  4. Getting Started with TJBot: Build, Configure, and Voice‑Control Your Own AI Robot
  5. AI in Insurance: A Practical Starter Guide for Real-World Implementation
  6. Master Permanent Mold Casting: A Beginner's Guide to Durable, High-Volume Metal Parts
  7. Touch Switch Circuits: A Beginner’s Guide to Simple Tactile Sensors
  8. Optimize Cat® Machinery: The Ultimate My.Cat.com Guide
  9. Deploying Node-RED on PLCnext with Docker: A Step‑by‑Step Guide
  10. How to Convert Your Tormach CNC into a 2D Scanner: A Beginner's Guide