C++ problem

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

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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);
jyotishbAuthor Commented:
Just wanted to make sure if we can specify <STX> directly?

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

then sprintf(cmd,"%c%s%c",STX, command, ETX);

or is the same as ur code?
>>Just wanted to make sure if we can specify <STX> directly?

Yes, you can do that just the way you wrote or - even simpler - using escapes, e.g.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jyotishbAuthor Commented:
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)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.