CRC sample program

N

Thread Starter

nptman

I am trying to communicate to a serial device that has a data string with a calculated CRC. I am able to parse all useable data but can not seem to calculate the CRC for verification. It is an CRC-CCITT reverse algorithm, 8 bit accumulator. It uses an odd polynomial and start seed. I have looked at a Modbus example of CRC but can't seem to see how to make it work. Has anyone seen any generic crc samples in RSlogix5000? Any help would be appreciated.
 
K
I am not sure what you are trying to do exactly, however I would suggest that you write the code using structured text. There are plenty of examples for vb/vba on crc, I have written various modbus functions with vba.

Regards
Kevin
 
E

Eddie Willers

There is a sample program installed with RSLogix 5000 for Modbus RTU over the serial port; it includes a CRC calculation subroutine.

I haven't tried to re-purpose it but I have a project coming up that does exactly that. I'm going to implement a protocol that includes a CRC (but isn't Modbus) using the 1734-232ASC module.
 
Thanks Kevin,

Unfortunately, I have a requirement to use ladder logic. What I am trying to do is calculate the CRC code at the end of a data string I receive.
 
L

Lynn August Linse

Don't think in terms of CRC-CCITT etc - you'll find it HIGHLY misused and will lead you into frustration. I have seen the Modbus CRC described about a dozen different ways, often wrong. I have seen people publish incorrect polynomials, claiming they are for Modbus.

Just find an exact example known to work and use it. For example, the Modbus/RTU 'serial' doc at modbus.org includes an 8-bit example:
http://modbus-ida.org/docs/PI_MBUS_300.pdf

If you find this routine not working, then add parenthesis everywhere because compiler differences in precedence is the primary source of failure I've seen. Since the RSLogix is 32-bit, you may also been to do explicit masking down to 16-bit in places.

FYI - the CRC for Rockwell DF1 is exactly the same as Modbus/RTU, accept for Modbus/RTU starts with a CRC seed of 0xFFFF and DF1 starts with 0x0000. So I do assume the RSLogix systems do have a CRC routine, which if you can pre-seed can be used for both DF1 and Modbus/RTU.
 
E

Eddie Willers

Listen to Lynn: he's an absolute expert on this.

I looked at the Modbus CRC routine inside RA's ControlLogix example implementation of Modbus on the serial port. It's not pretty, and it's not internally documented well (it was meant to be plugged in and not altered).

But if you look closely at the CRC lookup tables and C implementation in the PI_MBUS_300 document Lynn mentioned, you'll see that the variable names RA used are identical to the variable names in the document.

You can re-use the Modbus Master calculation loop and calling routine from that program to calculate a CRC in pure ladder logic.
 
E

Eddie Willers

I modified the CRC calculation in RA's Modbus Master example to function on a string and it works correctly.

It's just in ladder logic. Ten rungs or so total.

You would need to alter the seed value (very easy, it's in the first rung) and re-calculate the CRC tables for your specific polynomial.

Send me a message via the forum if you want this example sent.
 
Hi ntpman,

do you manage to program the CRC16-CCITT? Could you share the program with me because I am also struggling and don't know how to program it in RSlogix5000. Appreciate your help on this matter.
 
Hi Eddie,

Referring to your reply here it seems that you got the solution. I'm also struggling with this problem. Can you share it with me please how the rungs for CRC CCITT works?.. It would be very helpful.

> I modified the CRC calculation in RA's Modbus Master example to function on a string and it works correctly.
 
Hello!

I write because I am implementing a system of communication between 2 devices which conducted the program only one of them. I want my screen display for data values &#8203;&#8203;that sends me the other device. The other device driving a start bit, source address, destination address, 5 bits of data, 9600 baud, no parity bit and stop bit. Works with RS-485 protocol with RTU. I have not worked with these protocols, I'm looking for online information about it but nothing concrete ... Upon receiving data enable me never active PIR1,RCIF for now I have the correct UART configuration, as in the other device. I hope I can help. Here is part of my code:<pre>
RECEP

BTFSS PIE1,RCIE
GOTO Exit
BCF STATUS,RP0
BTFSS PIR1,RCIF

;finalizar comprobación de interrupción por recepción
GOTO Fin_RECEP

BTFSC RCSTA,OERR

;OERR: bit de error por desbordamiento
GOTO SAL_Overrun ;Sale de la interrupción

BTFSS RCSTA,FERR

;FERR: Bit de error enmarcado

;Subrutina chequeo de desbordamiento
GOTO RECEP_Overrun

;Recepción de datos
MOVF RCREG,W

GOTO Exit ;Recuperar el estado inicial del PIC y sale de la interrupción

RECEP_Overrun ;Recepción de datos

.
.
.
SAL_Overrun ;Sale de la interrupción
.
.
.

EXIT

retfie

;Comienza el programa

;Configuración de registros
BSF STATUS,RP0
MOVLW K_ADCON1 ;Ninguna entrada analógica
MOVWF ADCON1

MOVLW K_OPTION_REG ;PRescaler asig (1/64)
;RB0/INT, pull-up desactivado
MOVWF OPTION_REG ;Pull-up desactivado, TMR0 con PRE 2

BCF STATUS,RP0

MOVLW RECARGA_TMR0
MOVWF TMR0

MOVLW Baud_Rate_Constant ;(D'25')
MOVWF SPBRG ;Vel de comunic a 9600 baudios
BSF TXSTA,BRGH ;Altas velocidades

BCF TXSTA,SYNC
BCF TXSTA,TX9
BSF TXSTA,TXEN

BCF STATUS,RP0
BCF RCSTA,RX9
BSF RCSTA,SPEN

CLRF PIR1
CLRF PIR2

BSF STATUS,RP0
CLRF PIE1
CLRF PIE2
BCF STATUS,RP0

CLRF INTCON
BSF INTCON,T0IE
BSF INTCON,PEIE
BSF INTCON,GIE

;Habilito flag de interrup por recepción
MOVF RCREG,W
MOVF RCREG,W

BCF RCSTA,CREN
BSF RCSTA,CREN
BSF STATUS,RP0
BSF PIE1,RCIE

.
.
.
CODIGO

.
.
.

FIN</pre>
 
I modified the CRC calculation in RA's Modbus Master example to function on a string and it works correctly.

It's just in ladder logic. Ten rungs or so total.

You would need to alter the seed value (very easy, it's in the first rung) and re-calculate the CRC tables for your specific polynomial.

Send me a message via the forum if you want this example sent.
Anyway I can get the modified sample code? I'm struggling through a CRC16 xmodem communication AOI which is not working.
 
Top