Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating LRC value for serial communications...

Status
Not open for further replies.

SnyAc

Programmer
Jul 25, 2001
272
US
Does anyone know how you can calculate the LRC number for a string of data involved in serial communication across a modem. I'm doing fine using MSCOMM for the actual communication but I need to be able to verify that the data is correct thru the LRC.

Thanks in advance for your help

Andy Snyder
 
You can make your own LRC with the sys function 2007.

This function returns the checksum value of a character expression.

SYS(2007, cExpression)

For example:

? sys(2007,'Hello') && Returns: 56026

Let me know if it worked,

Charl
 
Andy,
The critical part of an LRC is whether you control both ends. If you do, then you can indeed use Charl's technique, otherwise you need to know the "exact" one expected (there are a number of different variants). See if you can get a technical definition of the LRC expected. If you can't I've written a few different ones that work in different situations.

Rick
 
Thanks for your responses.... what I am working with is a credit card authorization system that I am converting from FP2.6 to Visual. The BIN library that I used for my modem communication in the 2.6 version did an XOR on the string and returned one byte as the LRC.... e.g. '123' returned an LRC of '0' (asc(48))

I've been trying to figure out how they are doing the xor in this bin library and if you have a clue, I'd love to hear it...lol

Thanks again - Andy
 
The following should give you what you need. (Note: While written in VFP 7.0, I just checked, and it seems to work just fine in 5.0, and I'll assume 6.0 as well.

Usage:
? asc(lrc("123"))

* Program....: LRC.PRG
* Version....: 1.0
* Author.....: Richard G Bean
* Date.......: 02/13/02
* Notice.....: Copyleft 2002 ** Melange Computer Services, Inc **
* Compiler...: Visual FoxPro 07.00.0000.9465 for Windows
* Abstract...: Computes LRC (Longitudinal Redundancy Check)
* for string passed in
* Returns a single character
* Changes....:

LPARAMETERS p_cString

IF PCOUNT()<1 OR TYPE(&quot;p_cString&quot;) <> &quot;C&quot;
RETURN CHR(0)
ENDIF
LOCAL lnLRC, lnii

lnLRC = 0

FOR lnii = 1 TO LEN(p_cString)
lnLRC = BITAND(0xFF, ;
BITXOR(lnLRC, ASC(SUBSTR(p_cString,lnii,1))))
ENDFOR
RETURN CHR(lnLRC)

*: EOP * LRC.PRG *
 
Rick's the man....

Thanks again... that bit of code works perfectly....

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top