Jamod Control of Ifos Successful

November 03, 2009 | 2 Minute Read

I am finally able to demonstrate direct jamod control of Ifos.

Long story short: I built this embedded device that I wanted to string together with others of its kind, then control via Modbus. I wrote a Modbus server to run on the device, and up until now have been controlling it (quite successfully) using a quick-and-dirty Ruby script. For various reasons, though, I thought it would be better to control the device using Java. Actually, the main reason was jamod: it’s a mature Modbus library portable to any SBC I might choose as an embedded Modbus master and web application server.

As it turns out, my first attempt at using jamod highlighted a basic flaw in my calculation of a checksum. I naturally assumed the checksum would be computed on the actual bytes of each ASCII message, but when my device repeatedly dropped jamod messages due to checksum errors, I went back to the spec (emphasis mine):

§ ¶3: The LRC is calculated by adding together successive 8–bit bytes of the message, discarding any carries, and then two’s complementing the result. It is performed on the bytes of the message, before the encoding of each byte in the two ASCII characters corresponding to the hexadecimal representation of each nibble. The computation does not include the ‘colon’ character that begins the message, and does not include the CRLF pair at the end of the message.

This is really stupid. It means that a message, once received, must be decoded before it can be determined to be corrupt or not. If instead the message bytes were encoded and then checksummed, the receiver could calculate a running LRC and discard corrupt data the instant transmission was complete. Why not decode only data you know is not corrupt?

Anyway, it’s not what I think; it’s what the spec says. I went ahead and updated the code, verified it calculated the LRC correctly, fired up jamod again, and immediately discovered jamod itself computed the LRC wrong. The irony! The fix was straightforward—jamod failed to 2s-complement the final sum—and it had even been bugged, although evidently not released.

The upshot: I’m now controlling Ifos from Java, which means I can finally start on the master control web application. Shipping is a feature, and I see it on the horizon.