Protocol Implementation

Some time ago I experienced my first time (except from once at university) where I had to implement part of the specification of a protocol. In this case it was the Modbus/TCP protocol that was partly required. For this implementation some pretty low level programming was involved. The fact is, that in our software we just use a library for the Modbus protocol, but to keep our tests light-weight and to verify the correctness of this library as well I did not want to use this library in my unit tests. I remember during university to really hate the whole messing around with particular bits at specific locations, as I just wanted something more high level that would do all the work for me, but that has changed apparently.

The work that needed to be done to implement the protocol consists of two different parts:

  1. Setting individual bits in a byte
  2. Setting/extracting the correct bytes

Setting individual bits in a byte involved a lot of bitwise operators to make sure only the single bit in the byte is altered. To see this working correctly was very satisfying.

Extracting and setting the correct bytes was where I got very deep in the Modbus protocol. Perhaps because it is a very simple protocol I actually enjoyed it to get this right. Writing tests that depend on no external library, but only uses standard Java sockets to send the actual messages was very interesting. I had to make sure I read all of the message based on the message size that is part of the protocol. Getting the response correct was another challenge where deep understanding of the protocol was required.

All in all, I have grown fond of this deep understanding of protocols and getting very low level to implement them as efficient as possible. Having a high level API that abstracts this low level messing around is essential to have readable code, getting this API right and doing all the low level stuff is something I really liked.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s