vlitomsk Posted September 15, 2010 Report Posted September 15, 2010 Hi guys. I've question about calculating checksum in binary format. Authors of 'VisionCommunication.pdf' said: Calculate the accumulated sum of the bytes in the vector. I suppose, vector is full binary query containing header, details and footer sections. Is it true? And what kind of sum have I to calculate? ASCII sum of full query, or maybe length of query? Can you help me?
External Moderators Ash Neilson Posted September 15, 2010 External Moderators Report Posted September 15, 2010 Hi guys. I've question about calculating checksum in binary format. Authors of 'VisionCommunication.pdf' said: Calculate the accumulated sum of the bytes in the vector. I suppose, vector is full binary query containing header, details and footer sections. Is it true? And what kind of sum have I to calculate? ASCII sum of full query, or maybe length of query? Can you help me? Hi Mate, I assume you are referring to the PComB Communication Protocol, which uses the Binary based data. You actually need to have two Checksums! The first checksum is the last 2 bytes of the PComB Header (Which itself is 24 bytes long including the checksum). The Second checksum is located in the Footer and is the checksum of the Details. Here Below I have broken up the PComB Message to show you where the Checksums sit and what they are a Checksum of: Header: ======== 0 = / 1 = _ 2 = O 3 = P Bytes 0 to 5 are the STX ("/_OPLC") 4 = L 5 = C 6 = <Unit ID of PLC> 7 = 254 (Always 254) 8 = 1 (Always 1) 9 = <Message Key> (You probably won't need to use this, so leave it as 0) 10 = 0 11 = 0 12 = <Command Code> 13 = <Sub Command Code> (Again probably not required by you so leave as 0) 14 = - 15 = | 16 = | Bytes 14 to 19 are usually the 4 bytes of the Address and 2 bytes of the Elements Count 17 = | These can sometimes be "Command Details" relating to the PComB Command Code 18 = | 19 = - 20 = LSB of Detail Length | Bytes 20 to 21 are the Length of the Bytes in the Detail Section 21 = MSB of Detail Length | 22 = LSB of Checksum | Bytes 22 to 23 are the Checksum of this Header 23 = MSB of Checksum | The Checksum of the Header that sits in Bytes 22 and 23, is calculated using all the Bytes of the Header (From Byte 0 to Byte 21). Details: ======== This section of the PComB Message will contain byte data specific to the Command. Refer to the Unitronics PCOM Protocol PDF for information on the Details Section. Footer: ======== This section contains just three Bytes. Two Bytes are the Checksum of the Details Section, and the last Byte is the ETX. 0 = LSB of Details Checksum 1 = MSB of Details Checksum 2 = \ Byte 2 is the ETX ("\") When calculating the Details Checksum, make sure you are using EVERY SINGLE Byte in the Details Section. The Header Section must not be included in the Details Checksum. Both the Header Checksum and Details Checksum are calculated using a Two' Complement calculation. Below I have listed the Visual Basic, C# and C/C++ Syntax for calculating a PComB Checksum. 1 - Calculate the Sum of the Byte Values in the Section (Meaning add all the "Bytes Values" together to make one big number) 2 - Take this Sum of Byte Values and Calculate the Modulo of 65536 ("Byte Values" MOD 65536) - This makes the Checksum's Maximum Value a Two-Byte Unsigned 16-Bit Integer 3 - Two's Complement of the Calculated Modulo Value - Invert ("NOT") the Value and Add "1" For the below examples, the Sum of the Byte Values is "<BytesSum>". Visual Basic (The Result 'checksum' is a UShort): ================================================= checksum = (Not (<BytesSum> Mod 65536)) + 1 C# (The Result 'checksum' is a UInt16): ======================================== checksum = (~(<BytesSum> % 65536)) + 1; C / C++: ========= checksum = (~(<BytesSum> % 65536)) + 1; Hopefully this is the information you were after! If you are still having trouble, maybe post some of your code so I can help explain things better Cheers, Ash Neilson 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now