Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. sponsors. rss.
 From: Just Gioni
  Where is Just Gioni?
 drobeta
 Romania
 Just Gioni
 Tags
Subject: crc calculate
Thread ID: 230691 Message ID: 230691 # Views: 51 # Ratings: 0
Version: Visual FoxPro 9 SP2 Category: General VFP Topics
Date: Monday, June 08, 2009 2:03:05 PM         
   


I need to communication with the external device ,in the communication I need to calculate CRC and I don’t know how to do this in vfp9.0

Plase help my
CRC formula??????


BLOCK FORMAT

All bytes in the block are ASCII characters except the two control bytes CRC.
A sucessful transmission of a block from the ECR includes transmitting of:
- control character for beginning of a block BEG ( 0A HEX or ^J)
- data bytes
- control symbol for end of block END ( 0D HEX or ^M)
- 2 control bytes CRC.

and receiving of a control character for acknowledging the transmission ACK ( 06 HEX or ^F)

Unsuccessful transmission of a block will occur when after the transmission of the data from the block:
- in the incoming stream there is a period of 6 bytes, when no byte is received. ( If the received symbols are not ACK, NAK or a procedure for releasing the line in in-line or on-line mode, the ECR will continue to listen to teh line).
- a character for unsuccessful transmission NAK (15 HEx or ^U) is received. After NAK is received the ECR makes up to 8 attempts to transmit the block. If all attempts fail the ECR will:

- in in-line mode discontinue the execution of the command.
- in on-line mode after an unsucessful transmission the ECR will exit the waiting state for ACK and:
- after an unsuccessful transmission type 1 the ECR will remain in the wait state for next call. During the next call the ECR will try to transmit the same block. This will be repeated up to 8 times. After the 8-th call the ECR will exit the wait state and during the next session with teh PC will transmit a new message( a block with a new sequential number).
- After an unsuccessful transmission of a block from type 2 the ECR will cancel its attempts to transmit the block. During the next session with teh PC it will transmit a new block with a new sequential number.
The block lenght depends on the transmitted data, but does not exceed 128 bytes when the block is transmitted from the PC to the ECR and 256 bytes when the block is transmitted from the ECR to the PC.

ALGORITHM FOR CRC CALCULATION

The two CRC bytes are calculated according to the formula x^15 + 1. In the calculation are included all data bytes plus the byte for block end. Every byte passes through the calculation register from teh MSB to LSB.
Three working bytes are used - S1, S0 and TR
S1 - Most significant byte from the CRC ( it is transmitted immediatelly after END)
S0 - Least significant byte from the CRC ( It is transmitted after S1)
TR - the current transmitted byte in the block.

The CRC is calculated as follows:
1. S1 and S0 are zeroed
2. TR is loaded with the current transmitted byte. The byte is transmitted.
3. Points 3.1 and 3.2 are executed 8 times:
3.1. S1, S0 and TR are shifted one bit to the left.
3.2. If the carry bit from S1 is 1, the MSB of S1 and LSB of S0 are inverted.
Points 2 and 3 are executed for all bytes, included in the calculation of the CRC - from the first byte after BEG up to and including byte END.
4. TR is loaded with 0 and point 3 is executed
5. TR is loaded with 0 and point 3 is executed
6. Byte S1 is transmitted
7. Byte S0 is transmitted

ALGORITHM FOR CRC CHECK ON RECEIVING

Three working bytes are used S1, S0 and RC

S1 - Most significant byte from the CRC ( it is received immediately after END)
S0 - Least significant byte from the CRC ( transmitted after S1)
RC - the current received byte in the block ( beginning from the first byte after BEG and ending 2 bytes after END).

The CRC is obtained as follows:
1. S1 and S0 are zeroed
2. RC is loaded with the current received byte
3. Points 3.1 and 3.2 are executed 8 times:
3.1. S1, S0 and RC are shifted 8 times to the left
3.2. if the MSB of S1 is 1 then MSB of S1 and LSB of S0 are inverted.
Points 2 and 3 are executed for all bytes, included in the calculation of the CRC - from the first byte after BEG up to and including 2 bytes after END.
S1 and S0 must be 0.


The successful receipt of a block includes the transmission of an ACK symbol after confirming the CRC.
For unsuccessful receipt of a block when the connection ECR/PC was successful the following cases can be distinguished:
- in on-line mode for 1 sec there was no any BEG ;
- in in-line mode for 10 sec there was no any BEG commands;
- 8 times BEG command is received but the receipt of a blcok was unsuccessful.

It is recommended that the PC driver transmits 4 bytes FF after transmission of a block.
If during this period there was no any interrupt from the RxD line due to ACK, it is necessary that after a period of 55 msec the block is transmitted again.

-send to device

Request: 12/04/2009 1:03:48 PM.54364 (+54.7344 seconds)

FF FF FF FF FF FF FF FF FF FF 13 13 13 FF FF FF ÿÿÿÿÿÿÿÿÿÿ...ÿÿÿ
FF FF FF FF FF FF FF FF FF FF FF FF 11 11 30 31 ÿÿÿÿÿÿÿÿÿÿÿÿ..01
FF FF FF FF FF
ÿÿÿÿÿ

-recive from device

Answer: 12/04/2009 1:03:48 PM.59064 (+0.0000 seconds)

0A 30 30 30 30 30 30 3B 37 3B 41 3B 31 3B 31 3B .000000;7;A;1;1;
35 3B 3B 3B 33 3B 0D 17 6E 5;;;3;..n

“17 6E” This is the CRC

Thank you!



COMPLETE THREAD
crc calculate Posted by Just Gioni @ 6/8/2009 2:03:05 PM
RE: crc calculate Posted by Anders Altberg @ 6/8/2009 2:35:58 PM
RE: crc calculate Posted by Just Gioni @ 6/8/2009 2:57:18 PM
RE: crc calculate Posted by Anders Altberg @ 6/8/2009 3:34:44 PM
RE: crc calculate Posted by Just Gioni @ 6/9/2009 8:00:18 AM