xisto Community

# What Does This Do?

## Recommended Posts

I am working on a program to control Pan/Tilt/Zoom cameras. I have determined that these cameras require 6 bytes of information in hex format. I wrote a program to listen to an RS485 port in my PC and was able to sniff out some commands from another controller. The commands consist of a start string, a cameras id, three command bytes, and a parity byte. Since the camera id can be many different values, I believe the parity value will also change. The folks who make the camera were kind enough to provide me with some information. Specifically, they provided me with a function written in C to calculate the parity byte. However, I'm writing this program in Delphi. Is there anyone who can explain what this code is doing or maybe how to easily get it into another language, like Delphi?

Code:

void parity(unsigned char *cmd){   unsigned int temp0, temp1, temp2;   temp0 = cmd[0] ^ cmd[3];   temp1 = cmd[1] ^ cmd[4];   temp2 = cmd[2] ^ cmd[5];   temp1 <<= 2;   temp2 <<= 1;   temp0 ^= temp1 ^ temp2;   temp1 = temp0 & 0x07;   temp0 >>= 3;   temp1 ^= temp0 & 0x07;   temp0 >>= 3;   temp1 ^= temp0 & 0x07;   temp0 >>= 3;   temp1 ^= temp0 & 0x01;   temp1<<=5;   cmd[5] |= (temp1 & 0xe0);   return;}

I would greatly appreciate any help. As always, I appologize if I have posted this in the wrong forum

Thanks,kvarnerexpress

##### Share on other sites

ok. here's what's happening:

temp0, temp1 temp2 are all variables of type integer. unsigned means that they cannot be negative.

cmd is an array of characters (a string). cmd[x] returns the character at the x position in the string.

i'm pretty sure that the '^' character means 'raised to the power of'. for instance

x^y = x to the power of y

so what is happening is temp0, temp1 and temp2 are characters to the power of other characters.

'<<' is a binary shift operation. it shifts all bits in a variable x bits left (if 'variable << x' is used).
this has the effect of moving the bits in temp0, temp1 and temp2. similarly, '>>' shifts bits to the right.

'&' is a binary 'and' operation: if bit1 and bit2 are both 1, the resultant bit will be 1.
the '0x' prefix means the following numbers are in binary (i'm not really sure on how that works).

'|' means bitwise 'or', this means that if bit1 or bit2 is 1, the resultant bit will be 1.

any operator followed by '=' effectively performs the operation between the variable on the left and on the right of the operator. for instance:
variable += 1;
is the same as
variable = variable + 1;
.

unfortunately, i'm not really skilled enough at C to tell you exactly what the resultant output of the function will be. however, the first word 'void' means that the function you have been given will not directly produce anything. the function works by directly modifying the byte at position [5] in the character array.

hope i've provided enough information here for you to port your code to delphi.

keep up the good work!

##### Share on other sites

ok hey i checked a book i have on C++ this morning.

first thing:
0x is not a prefix for binary numbers, it is a prefix for hexadecimal numbers.

second thing:
turns out that the '^' operator is a bitwise XOR operator (not exponant). this would make more sense, as it provides a better checking mechanism.

this requires a little bit of explanation:
1/ XOR is short for 'exclusive or'. it returns TRUE if one variable or the other is true, but not both.

Mathematically:

var1 XOR var2 == (var1 OR var2) AND NOT (var1 AND var2)
for example,
TRUE XOR FALSE == TRUE

but
TRUE XOR TRUE == FALSE

2/ Bitwise Operators.
Bitwise operators work on the individual bits of a variable, not on the total variable.
For instance, say we had 2 variables 1 byte (4 bits) long. var1 is equal to 9 ('1001' in binary) and var2 is equal to 5 (0101 in binary).

So: var1 AND var2 == var1 * var2 = 45.
(Note: the definition of AND is pretty much A times

If Bitwise AND operations are used, instead of AND-ing the whole variable, each bit is AND-ed.

So: var1 BITWISE_AND var2 is:
10010101----0001
as the first bits are multiplied (giving 0), then the second bits (giving 0), the third bits(also giving 0) and finally the fourth bits (giving 1). hence 0001. (1 in decimal).

this makes much more sense than my previously explained '^' meaning 'exponent'.
ultimately, each bit from each byte in the string is put into a single checksum byte.

sorry bout that. you should now have enough information to port the code over to delphi.

peace out

## Create an account

Register a new account