Page 1 of 3

Block Check Character (BCC)

Posted: Wed Mar 08, 2017 5:56 pm
by Roberto Lopez
Hi All,

According Wikipedia, In telecommunications, a block check character (BCC) is a character added to a transmission block to facilitate error detection.

I need to communicate with a fiscal printer and I must send BCC with each command string.

Here is an example from the printer manual:

Code: Select all

[STX]1[ESC]*[FS]0000[FS]0000[FS]0004[FS]3[FS]83[FS]81[FS]2[FS]0[FS]0
[ETX]0054
Where BCC is the last value (0054 in this example)

Has anybody any idea about how to obtain it with Harbour?

Any help is welcome.

TIA.

Re: Block Check Character (BCC)

Posted: Wed Mar 08, 2017 8:45 pm
by edk
Hi Roberto.
Way of calculating the checksum should be described in the programmer's documentation. What's a model of fiscal printer?

Code: Select all

[STX]1[ESC]*[FS]0000[FS]0000[FS]0004[FS]3[FS]83[FS]81[FS]2[FS]0[FS]0
[ETX]0054
is similar to polish Posnet protocol.

Edward.

Re: Block Check Character (BCC)

Posted: Thu Mar 09, 2017 2:43 am
by Roberto Lopez
edk wrote: Wed Mar 08, 2017 8:45 pm Hi Roberto.
Way of calculating the checksum should be described in the programmer's documentation. What's a model of fiscal printer?

Code: Select all

[STX]1[ESC]*[FS]0000[FS]0000[FS]0004[FS]3[FS]83[FS]81[FS]2[FS]0[FS]0
[ETX]0054
is similar to polish Posnet protocol.

Edward.
Edward,

The fiscal printer is not of an international brand.

The description of BCC in the manual (translated from original Spanish) is as follows:
BCC = Four hexadecimales. Binary sum of all prior bytes

Re: Block Check Character (BCC)

Posted: Thu Mar 09, 2017 3:14 am
by CalScot
I've heard of it referred to as a check digit, or in the old telecom days, as the CRC - Cyclic Redundancy Check. Interesting that there are so many names for essentially the same thing - a crude validation of the integrity of the value string that preceded it.
I don't have easy access to documentation right now (brand new computer!!) but it should be possible to write a function to return the binary sum of all prior bytes as a four-hexadecimal string. Checking Harbour's HexaToDec() or DecToHexa() might help, or at least give inspiration that would lead to a solution?
Hope that helps!

Re: Block Check Character (BCC)

Posted: Thu Mar 09, 2017 1:45 pm
by andyglezl
Maybe someting like this...

DecToHexa( BIntODEC( DecToBin( asciisum( "123" ) ) ) )

Re: Block Check Character (BCC)

Posted: Thu Mar 09, 2017 6:41 pm
by Roberto Lopez
Thanks for the answers.

The manual is plenty of examples of commands with numbers ()like the one I've already posted) but not programming examples.

I've already attempted something using Harbour conversion functions, but I was not able to match the results as shown in manual examples.

Re: Block Check Character (BCC)

Posted: Thu Mar 09, 2017 6:47 pm
by Roberto Lopez
andyglezl wrote: Thu Mar 09, 2017 1:45 pm Maybe someting like this...

DecToHexa( BIntODEC( DecToBin( asciisum( "123" ) ) ) )
I've missed asciisum() when searched Harbour guides.

I'll try asciisum and Checksum functions.

http://harbour.edu.pl/lib/tools/ng3ef0c.html

http://harbour.edu.pl/lib/tools/ng4c464.html

By the way... harbour.edu.pl is a very nice site. The style / format is wonderful.

Thanks!

Re: Block Check Character (BCC)

Posted: Thu Mar 09, 2017 6:54 pm
by serge_girard
Hello,


http://harbour.edu.pl/index.html


Is indeed a great site! However (like our DOCS-page) it contains many � TAB chars which need a STRTRAN because HTML doesn't recognize TAB char.

Serge

Re: Block Check Character (BCC)

Posted: Fri Mar 10, 2017 10:06 am
by edk
Hi again.
Sorry for the delay, but I was in the delegation.
The description of BCC in the manual (translated from original Spanish) is as follows:

BCC = Four hexadecimales. Binary sum of all prior bytes
In my opinion the binary sum of all bytes is nothing but the sum of the ASCII codes of each character in the string.
From a mathematical point of view, it does not matter if we add up the number of binary, hexadecimal or decimal notation. The sum should always be the same.
For example:
the sum in binary notation : 101 + 110 = 1011; decimal = 11
the sum in decimal notation: 5 + 6 = 11

I'm surprised the given example, because the sum of all bytes (from [STX] to [ETX]) results in a hexadecimal notation 0554, not 0054.

Example according to andyglezl
DecToHexa (BIntODEC (DecToBin (asciisum ("123"))))
is OK, but shorter is NTOC( AsciiSum ( cSent ), 16, 4, '0' )

Code: Select all

STX:=CHR(2)
ESC:=CHR(27)
FS:=CHR(28)
ETX:=CHR(3)
cSent:=STX+'1'+ESC+'*'+FS+'0000'+FS+'0000'+FS+'0004'+FS+'3'+FS+'83'+FS+'81'+FS+'2'+FS+'0'+FS+'0'+ETX

BCC:=NTOC( AsciiSum ( cSent ), 16, 4, '0' )  // => '0554'
Manufacturer fiscal printer does not provide any libraries to operate the device?

Edward.

Re: Block Check Character (BCC)

Posted: Sun Mar 12, 2017 6:21 am
by mol
I'm using such a function with communication with Polish fiscal printers to calculate CRC:

Code: Select all

function CalculateCRC
	param cString
	local nResult := 0xff
	local nChar
	local i
	local cRetValue
	for i:=1 to len(cString)
		nChar := asc(substr(cString,i,1))
		nResult := numxor(nResult, nChar)
	next i
	
	// conversion to required format - change hex to string
	nChar := nResult % 16
	if nChar > 9
		cRetValue := chr(55+nChar)
	else
		cRetValue := str(nChar,1)
	endif
	nChar := (nResult-nChar)/16
	if nChar > 9
		cRetValue := chr(55+nChar) + cRetValue
	else
		cRetValue := str(nChar,1) + cRetValue
	endif
 return cRetValue