Link to home
Start Free TrialLog in
Avatar of jyotishb
jyotishb

asked on

C++ problem

Hi,
I m trying to make an C++ application to interface with a simple pinpad device connected to RS232 port.

Now the general message string format is
<STX>command<ETX><LRC>

I have used CreateFile to open the port and set all the parameters as mantioned int the manual, and i m getting a valid value to the Handle.

now, one of the command to accept key pad from the user is Z42. Now the pinpad accepts ASCII text.

What will be the exact packet string  for the command Z42. and how would i calculate the LRC code?

Can anyone help me plsss. Thanks in advance.
Avatar of jkr
jkr
Flag of Germany image

See http://en.wikipedia.org/wiki/Longitudinal_redundancy_check on how to calculate an LRC

An adaption of that algorithm in C++ would be

char calc_lrc(char* sequence) {

    char LRC = 0;

    while(*sequence) { LRC^= *sequence; sequence++;}

    return LRC;
}

And you'd use that like

char cmd[] = "<STX>Z42<ETX>";
char LRC = calc_lrc(cmd);
char send_buf[255];

sprintf("%s%c", cmd, LRC);
Avatar of jyotishb
jyotishb

ASKER

Hi,
Just wanted to make sure if we can specify <STX> directly?

cos Hex of STX is 0x02. can we do it like

command[]="Z42";
STX=(char)0x02;
ETX=(char)0x03;
then sprintf(cmd,"%c%s%c",STX, command, ETX);

or is the same as ur code?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank u very much
Just for the records, I'd actually make that

char calc_lrc(char* sequence) {

    char LRC = 0;

    while(*sequence++) { LRC^= *sequence;}

    return LRC;
}

But, I admit that this is less 'readable' ;o)