Here is how I am calculating crc16

For more advanced meter data users. Learn from others and ask questions about how to read meters, how to use the EKM Push Data, and how to display the meter data in useful ways.
Post Reply
jamesee
Posts: 9
Joined: Mon Apr 21, 2014 11:04 pm

Here is how I am calculating crc16

Post by jamesee »

After reading all the posts here, I thought I'd do a bit more research on the crc16 used by EKM.

The code that follows is a function that I wrote for the Xojo development environment http://www.xojo.com which I am using to create the applications for managing my meters - and eventually my home heating and pv solar power system.

I thought it might be of interest and maybe even of use to others here. I was initially baffled to discover that there are various versions of crc16 in existence, but fortunately I found a website (see notes in the code) that enabled me to quickly find out that EKM is using the MODBUS standard version. The only other wrinkle was dealing with the 7-bit issue. In that case I found that you need to first calculate the 8-bit MODBUS crc16 and then set the top bit of each of the two bytes in the crc checksum to zero. Thereafter - plain sailing!

Code: Select all


  '________________________________________________________________________________
  
  ' this function EKMcrc16 receives a string of bytes (s2) and returns EKM's 2-byte crc16
  
  '_________________________________________________________________________________
  
  'EKM's crc16 identified at MODBUS version of crc16 using
  'http://www.lammertbies.nl/comm/info/crc-calculation.html
  
  'coding algorithm was derived from a VB version here
  'http://modbus.control.com/thread/1026164935
  
  'and also from a Pascal version of the same algorithm on the same website
  
  '___________________________________________________________________________________
  
  dim i, j, k, crc1 as Uint16
  dim b, b1, b2 as Uint8
  
  crc1 = val("&hFFFF") ' FFFF is the initial value of crc for MODBUS crc16 calculation
  
  for i = 1 to lenB(s2)
    
    b = ascB(mid(s2, i, 1))
    crc1 = crc1 xor b
    
    for j = 1 to 8
      
      k = crc1 and 1
      crc1 =Bitwise.ShiftRight(crc1,1)
      if k>0 then crc1 = Bitwise.BitXor(crc1, val("&hA001")) 'A001 is the divisor for MODBUS crc16 calculation
      
    next j
  next i
  
  'crc1 is the MODBUS crc16 for the given string expressed as Uint16, a 16 bit positive integer
  
  crc1 = crc1 and &h7F7F  'mask to make bit 8 of each byte zero  for EKM metering data
  
  'separate into two integers expressed as 2 x Unit8
  
  b1 = crc1\256
  b2 = crc1 mod 256
  
  'function returns a two-byte string for matching with EKM's crc16
  
  return chrB(b2) + chrB(b1) 'convert to bytes and reverse the order for matching with EKM meter string
jamesee :D
Jameson
Posts: 860
Joined: Fri Nov 04, 2011 7:42 pm
Location: Santa Cruz, CA
Contact:

Re: Here is how I am calculating crc16

Post by Jameson »

Thanks jamesee!!

This is very cool, we really appreciate you sharing your methods. I will add this to my "CRC info dump" that I hand to those that ask about CRC :lol:

Interesting about xojo, this is what we are using to develop the EKM Dash (we mostly settled on it because it compiles for Mac, Windows, and Linux). If you would like to share what you have we would love to see it.

Thanks again.
Jameson
EKM METERING
http://www.ekmmetering.com
831.425.7371
jamesee
Posts: 9
Joined: Mon Apr 21, 2014 11:04 pm

Re: Here is how I am calculating crc16

Post by jamesee »

Hi Jameson

Thanks for the encouraging words. I've been using Xojo and its predecessor REALbasic for a number of years. I have developed a variety of applications including database apps. The multi-platform capability is a big plus.

Here's a link to a little demo app that you might like to try.

[Link removed - it's not working properly - please see my post below of 28 August for an alternative]

All the best

jamesee :D
Last edited by jamesee on Wed Aug 27, 2014 9:32 pm, edited 2 times in total.
Jameson
Posts: 860
Joined: Fri Nov 04, 2011 7:42 pm
Location: Santa Cruz, CA
Contact:

Re: Here is how I am calculating crc16

Post by Jameson »

Thanks for posting the demo link. I may be missing something but so far am unable to open it on a Mac. Do I need to open it through xojo? (I only have RealBaisc on this computer)

I will attach a screenshot of the "Contents" I get when I unzip.
Screenshot 2014-08-25 09.59.46.png
Screenshot 2014-08-25 09.59.46.png (72.54 KiB) Viewed 16656 times
Thanks again!!
Jameson
EKM METERING
http://www.ekmmetering.com
831.425.7371
jamesee
Posts: 9
Joined: Mon Apr 21, 2014 11:04 pm

Re: Here is how I am calculating crc16

Post by jamesee »

Hi Jameson

You shouldn't need any Xojo or RB software. This is an application file/package so you should be able to just put it on your Mac and double-click to launch it. (I think you can also launch it by double-clicking on the Omnimeter file in the Mac OS folder - but that's a rather round about way to go).

Hope this helps.

All the best.

jamesee
Jameson
Posts: 860
Joined: Fri Nov 04, 2011 7:42 pm
Location: Santa Cruz, CA
Contact:

Re: Here is how I am calculating crc16

Post by Jameson »

:oops: Im still not able to open this. When I try to open the file "EKM-Omnimeter I v.3 sample reader" on Mac OSX 10.8.5 it opens my Terminal program and then stops. That is all I see. As far as I can tell your software does not start on this computer.

Please double check the file "EKM-Omnimeter I v.3 sample reader" that you have in Dropbox.

Thanks for your help!!
Jameson
EKM METERING
http://www.ekmmetering.com
831.425.7371
jamesee
Posts: 9
Joined: Mon Apr 21, 2014 11:04 pm

Re: Here is how I am calculating crc16

Post by jamesee »

Hi Jameson

I'm sorry this is giving you so much trouble. I checked that the latest version of Xojo, which I am using will work on your OS. It should be OK back to OS X 10.7 so that's not the problem.

I think DropBox may be causing our problem, so I'll try something a bit different. This gives the application file an extra layer of wrapping.

https://www.dropbox.com/s/q324un9kxnafi ... o.zip?dl=0

Clicking on this link should download a folder called "Mac OS X (Cocoa Intel)" into your downloads folder. If instead it gives you a zip file called "Meter reader demo" then double click on that to unpack the folder "Mac OS X (Cocoa Intel)".

Inside the folder "Mac OS X (Cocoa Intel)" you should find an application file called "EKM-Omnimeter I v.3 sample reader for Mac". Copy or drag this file to your desktop and double-click on it. The app should now open for you.

I sincerely hope this works for you this time.

All the best

jamesee

Post script. I've now found a better and more conventional means of app sharing on DropBox. It uses a disk image file:

https://www.dropbox.com/s/c6eqei61om3ee ... g.zip?dl=0

Happy hunting!

Jamesee :D
rschramm
Posts: 1
Joined: Fri Feb 13, 2015 10:52 pm

Re: Here is how I am calculating crc16

Post by rschramm »

Jamsee, can you provide any information on your xojo application? In particular, are you communicating modbus protocol? And if so, did you find a plugin or do you have src code example you can share.
Thanks, rschramm
jamesee
Posts: 9
Joined: Mon Apr 21, 2014 11:04 pm

Re: Here is how I am calculating crc16

Post by jamesee »

Hi rschramm

I am not using the MODBUS protocol directly. My meters are connected to the EKM iSerial device(s) which are in turn connected to my LAN, so the communications implemented in my application are TCP/IP. As to the meter read requests, these are modelled on jameson's protocols documented elsewhere on this forum. The only MODBUS element in my application is the protocol used for calculating crc16.

I am happy to share my source code with you, if this is relevant to your needs. Please let me know.

I am currently in the process of implementing a PV system and heat pumps for home and water heating. The EKM meters will be at the heart of the system control.

All the best

Jamesee
Post Reply