Jul 26, 2008

What Does This Do?

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

What Does This Do?

kvarnerexpress
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:

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

 

 

 


Reply

switch
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
CODE
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:
CODE
variable += 1;
is the same as
CODE
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! biggrin.gif

 

 

 


Reply

switch
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:
CODE
var1 XOR var2 == (var1 OR var2) AND NOT (var1 AND var2)

for example,
CODE
TRUE XOR FALSE == TRUE

but
CODE
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 cool.gif

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

So: var1 BITWISE_AND var2 is:
CODE
1001
0101
----
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 biggrin.gif

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics
Looking for What, Does, This, Do?

Searching Video's for What, Does, This, Do?
advertisement



What Does This Do?



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE