bit twiddling in Java

D

Duncan Lyall

Hi

I am currently writing a driver for a chip and pin credit card
terminal in Java. It's all about byte arrays, serial comms and start
and stop bits and it is good fun, however, as most of my experience is
at a much higher level I'm struggling with some of the concepts that
must be familiar to those who do this sort of thing all the time.

I have implemented the data link layer and I'm currently trying to get
my head around the session layer, the protocol at this level defines
two acks ACK1 (0x05 or 0000 0101) and ACK0 (0x06 or 0000 0110)
there are two corresponding control characters whose 0th bits are 0
and 1.

It goes something like this

send first message with control set to 0
expect ACK0
next message is sent with control = 1
expect ACK1
next message set control = 0
expect ACK 0
next message set control = 1
expect ACK 1
etc etc,

An out of sequence ACK is an implied NACK

The documentation is 'concise' to say the least but it does make an
explicit reference to the two ACKs, something along the lines of "the
two ACK characters have been arranged so that there is a two bit
difference between them".

THE QUESTION(S):
What is the significance of the two bit difference in the ACKs ?. Is
there some clever algorithm to keep track of the next expected
ACK/control character ?

I know this is not a Java question per se so if you think it doesn't
belong here please advise a better place to post.

TIA

Duncan Lyall
 
D

dar7yl

Duncan Lyall said:
Hi

I am currently writing a driver for a chip and pin credit card
terminal in Java.

I sincerely hope you are doing this for a legitimate purpose. I'd hate to
see you get hurt by vigilante mobs bent on revenge for you ripping credit
card info off of poor unsuspecting grandmothers.
It's all about byte arrays, serial comms and start
and stop bits and it is good fun, however, as most of my experience is
at a much higher level I'm struggling with some of the concepts that
must be familiar to those who do this sort of thing all the time.

I have implemented the data link layer and I'm currently trying to get
my head around the session layer, the protocol at this level defines
two acks ACK1 (0x05 or 0000 0101) and ACK0 (0x06 or 0000 0110)
there are two corresponding control characters whose 0th bits are 0
and 1. ....
The documentation is 'concise' to say the least but it does make an
explicit reference to the two ACKs, something along the lines of "the
two ACK characters have been arranged so that there is a two bit
difference between them".

THE QUESTION(S):
What is the significance of the two bit difference in the ACKs ?.

This is so that random 1-bit errors can be detected. If you are expecting
an ACK0, and received an ACK1, that means that two bits had to have been
reversed. that's a 1 in 4 chance.
(assuming the error affects those two bits only, the probability of error
reduces significantly if you take into consideration all of the bits)

see the GREY code ( http://www.nist.gov/dads/HTML/graycode.html ).
Is there some clever algorithm to keep track of the next expected
ACK/control character ?

yes:
<code>
static final int ACK0 = 5;
static final int ACK1 = 6;
static final int DIFFBITS = 3; // these bits change

// you might want a sync function to reset this.
private int next_expected = ACK0 ;

private int expected()
{
int result = next_expected;
next_expected ^= DIFFBITS ; // exclusive OR operation
return next_expected;
}

boolean expect( int byte )
{
return (byte == expected() );
}

</code>

regards,
Dar7yl
 
D

Duncan Lyall

I sincerely hope you are doing this for a legitimate purpose. I'd hate to
see you get hurt by vigilante mobs bent on revenge for you ripping credit
card info off of poor unsuspecting grandmothers.

Of course
This is so that random 1-bit errors can be detected.
...

OK, thanks for that, works well.

Cheers
Duncan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top