Home AMX User Forum AMX General Discussion

TAP ( TELOCATOR Alphanumeric Protocol) checksum calculation

I am trying to convert a BASIC program to AMX for a checksum calculation. Let me know if you have done similarly. I have posted my code block and the protocol pdf for reference. Below is the BASIC program sample.

REM - This sample BASIC program converts the checksum value "sum" into the
REM - three characters which are sent as part of the TAP protocol. The variables
REM - d1, d2 and d3 contain the three digits which are to be added to the
REM - transmitted data block. "INT" is the integer function which returns the
REM - integer portion of a number. This function is required if the variables
REM - are floating point numbers. If they are declared as integers then the INT
REM - function is not required. This BASIC program may easily be converted to
REM - other programming languages.
REM -
sum = 379
REM -
REM - Following the checksum example in the TAP Specification Document:
REM - <STX> 1 2 3 <CR> A B C <CR> <ETX> the checksum value is 379.
REM - The following code will create the three characters to be transmitted
REM - in order to represent this checksum.
REM -
d3 = 48 + sum - INT(sum / 16) * 16
sum = INT(sum / 16)
d2 = 48 + sum - INT(sum / 16) * 16
sum = INT(sum / 16)
d1 = 48 + sum - INT(sum / 16) * 16
REM -
REM - Print the three character checksum in decimal and ASCII
REM -
PRINT "d1="; d1, "d2="; d2, "d3="; d3
PRINT "d1$="; CHR$(d1), "d2$="; CHR$(d2), "d3$="; CHR$(d3)

Comments

  • zack.boydzack.boyd Posts: 94
    I think you want something like...
    INTEGER i
    INTEGER sum
    CHAR checksum[3]
    CHAR printableChecksum[6]
    
    sum = 0
    
    FOR(i = 1;i <= LENGTH_STRING(theCommand);i = i + 1){
        sum = TYPE_CAST(sum + theCommand[i])
    }
    
    checksum = TYPE_CAST(sum)
    
    printableChecksum = ITOHEX(TYPE_CAST(checksum))
    
    
  • ajish.rajuajish.raju Posts: 185
    I got the sum part done already, the last section which deals with encoded bit is where i am stuck.
  • ajish.rajuajish.raju Posts: 185
    Thanks Zack. Your code works perfectly.
Sign In or Register to comment.