How to build a Modbus driver in Go?

This article serves as a practical guide into how to build a Modbus driver. I use the Go (Golang) programming language because I like it . However, the principles outlined should apply to any stack.

I happen to be a software engineer who works in SCADA. What does that mean?  it means that a lot of my days involve making various pieces of complex software work in harmony with industrial devices like one big happy family. In order to make software talk to industrial devices, we use protocols that both the devices and the software can understand. Some protocols are fairly complex and some are fairly easy, some are frustrating and some are fun. Amongst them Modbus is the most popular. read more

A Class Factory in Golang (Google Go)

Do you know what a class factory is and why is it useful?  Ever considered implementing a class factory in Golang (Google Go) even though Go is not considered fully object oriented in nature? Are you curious about some awesome features in Go other than it’s impressive concurrency capabilities? If any of the these questions interests you, then reading on may be worth your while. This article covers implementing the factory design pattern in Go.

If that’s not your first time visiting my site, then you probably know by now that the Go programming language is a personal favorite of mine and here’s why. I love software design and I love Go. Because I love software design, I had to learn about design patterns. And one of the most popular design patterns is the concept of factory design patterns. In the OOP world, that translates to class factories. To make diving through the tutorial easier, here are the links to the main sections: read more

A dive into the industrial software industry

This article serves to provide a decent overview of the industrial software industry. This industry is the gate to the intelligent solutions that drive the success of industrial operations in factories, plants, pipelines, electrical grids, and similar facilities. Let’s start the journey by covering SCADA which is the main component of any industrial software.

What is scada?

A SCADA (Supervisory Control And Data Acquisition) system simply refers to the layers of software and infrastructure that sit between a digital screen, and any control or measuring device. SCADA is used to issue control commands to remote devices like valves or pumps, read values from those devices, and analyze device data when needed. SCADA systems can be small in order to manage field devices that are in close proximity inside a single facility , or expand to handle devices that are spread across vast geographical areas (like sensors on oil pipes). SCADA exists almost everywhere in this day and age. read more

Life is not fair!!

Do you think good always wins over evil?? Do bad people who do nasty things end up getting punished in life?? How about people who go through life crises? Are they all guilty of something? did they cause the agony of other human beings and hence they are suffering in return? In short, do you think life is fair??

life is not fair

As a kid, I enjoyed fairy tales, stories of how the good always prevails and how evil always loses. I had my own theory that any person who commits an act of evil will always lose, it’s just a matter of time because life is fair, life just has to be fair , or else why bother caring?! As a teenager, I got more curious about the world, I started to read about cultures and history. Slowly, I came to the realization that the history of our world is full of stories of tyranny, war and injustice and guess what?  Not all tyrants end up suffering in their lives, probably after, but not in life. Some of them die peacefully in their warm clean beds maybe in the company of a grandchild or two, like any other friendly grandpa out there!! My ultimate realization as a teenager was that if history is to teach us one thing -other than the fact that human beings love to repeat the same mistakes over and over –  is that life is simply not fair!! read more

A practical guide to protocol buffers (Protobuf) in Go (Golang)

The following article targets an audience with interest in software design, protocol buffers or computer programming in general, the article is fairly technical and assumes some knowledge in programming concepts.

Protocol buffers and Golang are two very recent fascinations of mine ever since I saw them in action. As an engineer, I can see a lot of power that can be harvested by leveraging either or both technologies which is probably why they are all over the place in Google’s infrastructure. This article serves as a practical tutorial into how to use protocol buffers in Go with some diving into the language features. Because this article is rather long, I decided to divide it into different sections:

Why Protocol Buffers?

Put very simply, a protocol buffer or protobuf encodes and decodes data so that multiple applications written in different programming languages can exchange a large number of messages quickly and reliably without overloading the network. From my experience, the beautiful thing about Protocol buffers is that the more data you tend to send, the more performance you’d get if you compare with other methods of sending the same data load, which can be really helpful if you have a lot of data that you need to send across the network between two nodes. In practice what protocol buffer libraries do is that they compress the messages to send in a serialized binary format by providing you the tools to encode the messages at the source and decode the messages at the destination. Protocol buffers currently are supported in multiple programming languages as outlined in https://code.google.com/p/protobuf/wiki/ThirdPartyAddOns however the level of support varies.

Since protobuf is only concerned with the encoding, compression, decompression and decoding of a data, you need to take care of how to send it. Of course, The most obvious choice is using sockets which I will cover here, however you can get more creative and try a more fancy transport method like zeromq for example.

Why Go?

Go (or Golang) is a relatively new programming language developed by Google to replace some complex C/C++ components utilized in Google’s application layer. What sets Go apart; is the fact that it was built from the grounds up to provide performance that is destined in the future to compete with very powerful languages like C/C++ while supporting a relatively simple syntax that resembles dynamic languages but at the same time not as confusing as languages like Haskell or Ocaml. Go is garbage collected, however it doesn’t rely on virtual machines to achieve that. It compiles everything down to the machine level, you simply choose the type of platform (Windows, Mac..etc) that you’d like the binary to run on when you build, and then the compiler will produce a single binary that works on that platform which makes it native and cross platform at the same time. The way Go approaches common topics like OOP and threading is different than its predecessors, which makes it a testimony on how the future programing language should be like. Personally, I like to view Go as a magic tool to obtain super powers, and I am sure we all love super powers. The fact that I can write a piece of code that is simple in syntax, and deploy it anywhere without having to install any libraries, virtual machines or frameworks on the target machine is like how the green lantern has the ability to do anything anywhere whenever he feels like it using a mere green ring.

Go , in my opinion, is perfect for microservice architectures which we will be seeing a lot of in the future. A microservice architecture is an architecture at which you divide the responsibilities of your application to smaller services that only focus on specific tasks. These services can then communicate between themselves to obtain the information they need to produce results.

Source Code

Tutorial Overview

This tutorial is my effort to learn and share how to use protocol buffers in Go. I will try to cover some of the Go language interesting features in the process, however an awesome place to start learning Go is here . We’ll do that by building the following pieces:

1- A Go TCP client that can read data from a csv file then sends it to a TCP server using protocol buffers

2- A Go TCP server that can accept messages from multiple clients and then utilizes Protobuf libraries to decipher the messages before writing them to a CSV file

In order to demo protocol buffer support for another programming language, I included a Python client that uses protocol buffers which can also send data to the Go server.

Let’s start.

Preparing your development environment

First step is to setup your development environment. In order to do that, we need to:

1- Install Go =>

a. Go to https://code.google.com/p/go/wiki/Downloads?tm=2

b. Pick the file that corresponds to your development environment operating system

c. Ensure that a GOPATH environmental variable is defined to point to your Go workspace ( the folder at which you will save your Go code)

d. Ensure that your GOPATH root has three main folders: bin, pkg and src.

e. If you are confused about how to setup your Go environment, check the following article: http://golang.org/doc/code.html

2- Install Python 2.7.6 (For Windows users only) =>

a. Go to https://www.python.org/download/releases/2.7.6/

b. Pick the file that corresponds to your operating system

c. Ensure that your PATH environmental variable points to the Python.exe file.

3- Get Protobuf =>

a. Install both protobuf compiler and source files from https://code.google.com/p/protobuf/downloads/list

b. Install hg Mercurial at http://mercurial.selenic.com/downloads

c. To get the Go protocol buffer libraries, type go get -u code.google.com/p/goprotobuf/… from your command console.

d. To get  and build the Python Protobuf libraries, check the README.txt file in the Python folder included in the Protobuf for build and install instructions. Typically it involves typing Python setup.py install

Preparing your Protobuf message

Before you write any code, you need to first define the Protobuf message that will include the data you need to be serialized and sent. Here is what you need to do:

1- Design the shape and structure of the message based on the data that you need serialized

2- Write a proto file that describes your message

3- Use the Protocol Buffer compiler file (Protoc) to convert the Proto file to a library specific to your programming language of choice so that you can include it in your code

4- For a practical example about how to go about Preparing your Protobuf message, check the section below. Otherwise for detailed information about how to write the Proto file, check https://developers.google.com/protocol-buffers/docs/proto

Creating and using the Proto file for our project

Let’s illustrate preparing a Protobuf message by preparing a message for our project:

Imagine that I need to serialize and send a message called “TestMessage” ,which includes information identifying the client that sent it. The client information needed is the client name, the client ID and the client type. I also want my message to include a list of items, such that each item has an ID, a name, a value and a type. Now for the purpose of of this tutorial, I want the package or library name that includes all of this to be called “ProtobufTest”.

Here’s a sketch for how the TestMessage looked like inside my head, let’s assume 5 items are included in the message for now:

clip_image001

1- To define a message called “TestMessage” in a proto file, it’s as simple as typing

message TestMessage{
    //add your message body here
}

2- We agreed that the package or library name is to be called “ProtobufTest”, this can be defined by using the “package” keyword in the Proto file, and don’t forget the semicolon:

package ProtobufTest;

message TestMessage    {
   //add your message body here
}

3- Let’s say that the item type can only be one of three types: TypeX, TypeY or TypeZ. This can be achieved by using an enum type as follows:

enum ItemType {
  TypeX = 0;
  TypeY = 1;
  TypeZ = 2;
}

4- Now assuming that the only fields that must be filled in the message are the client ID, the client name , and each item ID. The Proto file will end up looking like this:

package ProtobufTest; message TestMessage { required string clientName = 1; required int32 clientId = 2; optional string description = 3 [default = "NONE"]; repeated MsgItem messageitems = 4; enum ItemType { TypeX = 0; TypeY = 1; TypeZ = 2; } message MsgItem { required int32 id = 1; optional string itemName = 2; optional int32 itemValue = 3; optional ItemType itemType = 4; } } read more

I was bored!!

I was really bored a couple of years ago, why? Ummm, let me think, was it because of the lack of something to do? don’t think so, I had tons of things that I could have done!! Was it for the lack of someone to talk to? Absolutely not, I had and still have amazing people in my life. Was it because of the lack of travel? Nope, I travelled quite a bit!! Come to think about it, I felt like there was the whole world to conquer out there, places yet to visit, lives yet to live, awesome people yet to meet, so much fun yet to have, even video games to play but I still felt really bored!! read more

The gift and the curse of being yourself!!

I am currently sitting in a tiny plane cruising from Seattle to Edmonton. I decided to write about something not technical. Who am I you ask?? Well, I am just a guy who decided to write about a fancy topic while looking over the clouds from above!! Sometimes, I try to imagine how my great great grandparents would react a couple hundred years ago if they were given the opportunity to watch the skies, the mountains, the seas and the whole frickin’ world from above. For them, it would have felt like temporary possessing divine powers. For us, it’s just another day. For me a simple observation like that represents how magnificent the human race is!! read more

What is scaling an application?

Simple Definition:

Scaling an application simply means making a piece of software capable of handling amounts of data that is more than the software current ability. This is typically done by either redesigning the piece of software or making the hardware it runs on more powerful.

Examples:

A bookstore is using a software program that stores the customer information and the books they purchased. The store is growing in popularity to the point where within one year; the number of customers grew ten times. The software program cannot absorb and store the customer information fast enough due to the growing number of customers. The software engineer will need to “Scale” the bookstore software application to be able to handle the increasing number of customers without delays. read more

What is SCADA?

Simple Definition:

SCADA is the layer that sits between a computer screen and any control or monitoring device.

Examples:

In a factory, SCADA is what allows the factory operators and engineers control the temperatures and pressures of the production lines.

In a power generation facility, SCADA is what allows the operators or the engineers control and monitor the power that gets generated