Reading ground pound BCD decoding in PB
I briefly describe the PB reading scale operation process in "Reading Weight" in PB, and some netizens reflect the reading and decoding of the BCD code is not very clear. Here, this part of the content is attached to explain the BCD decoding process.
Know BCD encoding
BCD coding is a digital compression storage code. Everyone knows that one byte has 8 bits, and the numbers are 0 to 9 only need to use 4 bits. If you use one byte to store a number, there will be certain waste. In particular, in the transmission process, it has come to compression, so BCD coding is generated.
BCD encoding points 8 bits of one byte into two parts and low 4 bits, which means that one word energy can store two numbers. So the BCD encoding process is the process of compressing the digital compression, compacting the numbers of the two bytes into one byte. Conversely, the decoding is to split a number of numbers into two digits separately (most of the processing is processed by byte).
Example:
The encoding process, the number 69 is baccd (Note: BCD encoding is lower, will not be commented later).
1. Convert 6, 9 into binary reputation: 6 (00000110) 9 (0000100)
2. Combine 69 into one byte, take 6, 9 binary encoding low, 4 digits, according to the principle of low position, put the 9 low four positions of the lower four positions to the new byte Binary coding is 10010110;
3. Complete the encoding process, the BCD coding result of 69 is 10010110.
Decoding process: decoding 69's BCD code 1001010.
1. Split the high 4 bits of 10010110 with the low 4-position to obtain two binary numbers 1001 and 0110;
2. The front addition of 1001 and 0110, respectively, 4 bits 0000, respectively, two 8-bit binary number 00001001001, 00000110;
3. Because the low position is before, we will set the two binary numbers to 0000110 000010001;
4. Convert the binary number to decoding results to be 69 (correctly decoded).
How to decode BCD code in PB
Everyone knows that there is a binary type of variables in the PB, but can not be able to press the operation, then how do we decode BCD-encoded numbers?
I think everyone will think of the ASCII code, yes, it is her. ASCII is the value stored in the computers, and she is not a binary number of binary numbers that are not 01 in PB.
BCD decoding needs to split a height of 4 digits and low 4 digits, then how can we use a decimal ASCII code?
Because the PB does not provide a bit operation, we can only write a function to do some simple processing, how do you handle it?
Method 1: We write a function to convert the decimal ASCII (single byte) into a binary string, of course, you have to write a function of converting a binary string to 10 credit numbers, interested friends can try it.
Method 2: In the last content I wrote, it has been referred to in hexadecimal to complete the conversion. Everyone carefully studied that it is not difficult to find that hexadecimal is equivalent to converting one byte of the content of 4 and low, and if you don't believe you, you can check it yourself. This way we only need to write a conversion function, convert the decimal number to a hex string. After the conversion, the two character positions of the hex string are converted (because the encoding is low), then the value is directly converted to the numerical type to obtain the value after the code is obtained. (Note: There is a conversion function after the online netizen written by online. The method of understanding, we are easy to handle when we read the Weight of the BCD code.
1. Read the serial port (now there is a USB port) with binary type BLOB;
2. Mandatory converting BLOB into string so that binary stream is encoded in 8-bit storage format, that is, the BCD code is in this string, but it is a compressed format;
3. Segment each byte of the String, then translated into ASCII coding, and function ASC in PB;
4. Location of String Medium State Bit, Verify Locket, Digital Bits, and the like according to the specific device;
5. Locate the digital bits for BCD decoding, the method is as above;
6. Complete the decoding to get the weighing data.
annex:
/ / =========================================================================================================================================================================================== ==============================
// function: of_hex ()
/ / -------------------------------------------------------------------------------------------- -----------------------------
// Description: Convert Integer to 16-Binding strings
/ / -------------------------------------------------------------------------------------------- -----------------------------
// AAGUMENT: INTEGER PSSL
/ / -------------------------------------------------------------------------------------------- -----------------------------
// Return: String 16 Bringing string
/ / -------------------------------------------------------------------------------------------- -----------------------------
// log: Excerpt by Tubx 2004.03.10
/ / =========================================================================================================================================================================================== ============================= String vs, vstmp, vsret
Integer Vi1, Vilen, I, Vimod, VIY, VISL
Vilen = len (String (PSSL))
Char vc_he [6]
Vimod = Mod (PSSL, 16)
Visl = PSSL
IF vimod> = 0 THEN
vStmp = string (Vimod)
IF vstmp = '10 'Then vStmp =' a '
IF vstmp = '11 'Then VSTMP =' b '
IF vstmp = '12 'TEN VSTMP =' c '
IF vStmp = '13 'TEN vStmp =' D '
IF vStmp = '14 'TEN vStmp =' e '
IF vStmp = '15 'TEN VSTMP =' F '
VC_HE [1] = VSTMP
END IF
For i = 1 to Vilen 1
viy = truncate (VISL / 16, 0)
IF viy> 0 THEN
vStmp = String (VIY)
IF vstmp = '10 'Then vStmp =' a '
IF vstmp = '11 'Then VSTMP =' b '
IF vstmp = '12 'TEN VSTMP =' c '
IF vStmp = '13 'TEN vStmp =' D '
IF vStmp = '14 'TEN vStmp =' e '
IF vStmp = '15 'TEN VSTMP =' F '
VC_HE [i 1] = VSTMP
END IF
IF viy = 0 THEN EXIT
VISL = VIY
NEXT
VSRET = ''
IF isnull (VC_HE [6]) or vc_he [6] = '' THEN
vsret = vsret
Else
vsret = vsret vc_he [6]
END IF
IF isnull (vc_he [5]) or vc_he [5] = '' THEN
vsret = vsret
Else
vsret = vsret vc_he [5]
END IF
IF isnull (vc_he [4]) or vc_he [4] = '' THEN
vsret = vsretelse
vsret = vsret vc_he [4]
END IF
IF isnull (vc_he [3]) or vc_he [3] = '' THEN
vsret = vsret
Else
VSRET = VSRET VC_HE [3]
END IF
IF isnull (vc_he [2]) or vc_he [2] = '' THEN
vsret = vsret
Else
vsret = vsret vc_he [2]
END IF
IF isnull (vc_he [1]) or vc_he [1] = '' THEN
vsret = vsret
Else
vsret = vsret vc_he [1]
END IF
Return vsret