Another Delphi to Java transplantation

I am porting an old application written in Delphi to Java.
I have some trouble using this function, it calculates the CRC of the transmitted message.

This is the original code:

if(ReceivedMessage[slot].Message[1] = $FD) and
(ReceivedMessage[ slot].MessageLength in [1..16]) and
(ReceivedMessage[slot].Message[counter] = $FF) then
begin
index := 2;
ReceivedMessage[slot].DataReady := TRUE;

for counter := 1 to ReceivedMessage[slot].MessageLength do
begin
Inc(index);
< br /> if ReceivedMessage[slot].Message[index] <$F8 then
ReceivedMessage[slot].Data[counter] := ReceivedMessage[slot].Message[index]
else
if ReceivedMessage[slot].Message[index] = $F8 then
begin
Inc(index);

ReceivedMessage[slot].Data[counter] := ReceivedMessage[slot ].Message[index] or $F0;
end
else
ReceivedMessage[slot].DataReady := FALSE; // Invalid data
end;

if ReceivedMessage[slot].DataReady = TRUE then
begin
Inc(index );

if ReceivedMessage[slot].Message[index] <$F8 then
ReceivedMessage[slot].CRC := ReceivedMessage[slot].Message[index] shl 8
else
if ReceivedMessage[slot].Message[index] = $F8 then
begin
Inc(index);

ReceivedMessage[slot].CRC := (ReceivedMessage[slot].Message[index] or $F0) shl 8;
end;

Inc(index);

if ReceivedMessage[slot]. Message[index] <$F8 then
ReceivedMessage[slot].CRC := ReceivedMessage[slot].CRC or ReceivedMessage[slot].Message[index]
else
if ReceivedMessage[slot] .Message[index] = $F8 then
begin
Inc(index);

ReceivedMessa ge[slot].CRC := ReceivedMessage[slot].CRC or ReceivedMessage[slot].Message[index] or $F0;
end;

This is my Java code:

if(array[1]==0xFD && (array[2]>0 && array[2]<17) && array[pos]==(byte)0xFF)< br /> {
index=2;
for(int counter=1;counter{
index++;
if( array[index]<0xF8)
data[counter]=array[index];
else
if(array[index]==0xF8)
{
index++ ;
data[counter]=(byte)(array[index] | 0xF0);
}
else
return 0xFC;
}


index++;
short crc=0x0000;

if(array[index]<0xF8)
crc=(short) (array[index]<< 8);
else
if(array[index]==0xF8)
{
index++;
crc=(short)((array[index] | 0xF0 ) << 8);
}
index++;
if(array[index]<0xF8)
crc=(short) (crc | array[index]);
else
if(array[index]==0xF8)
{
index+ +;
crc=(short)(crc | array[index] | 0xF0);
}

msgcrc=new byte[] {(byte)(crc >> 8 & 0xff),(byte)(crc & 0xff));

My function returns the transmitted CRC code most of the time, but sometimes it fails and returns the last two bytes of the message. At the end of the message The 3 bytes are the CRC code (2 bytes) and the end of the message 0xff byte.

Does it help?

Thank you,
Pedro

Remember:

Java bytes are signed. So:

byte test = (byte)255;
System.out.println(test);

will output: -1

and

byte test = (byte)255;
System.out. println(test == 255);

An error will be printed, but

byte test = (byte)255;
System.out. println((test & 255) == 255);

will do what I think you want to achieve (print as true in this case).

To get nothing Symbol (byte) value, please use (array [index]& 255).

You will have to use 0xff for masking everywhere, otherwise you will get a sign extended integer of byte value greater than 127 .

For comparison, you can also always perform conversion to bytes (e.g. if(test ==(byte)255)) but I think a single conversion should be adhered to, so I recommend masking with &0xff. < /p>

I am porting an old application written in Delphi to Java.
I have some trouble using this function, it calculates the CRC of the transmitted message .

This is the original code:

if(ReceivedMessage[slot].Message[1] = $FD) and
(ReceivedMessage[slot].MessageLength in [1..16]) and
(ReceivedMessage[slot].Message[counter] = $FF) then
begin
index := 2;< br /> ReceivedMessag e[slot].DataReady := TRUE;

for counter := 1 to ReceivedMessage[slot].MessageLength do
begin
Inc(index);
< br /> if ReceivedMessage[slot].Message[index] <$F8 then
ReceivedMessage[slot].Data[counter] := ReceivedMessage[slot].Message[index]
else
if ReceivedMessage[slot].Message[index] = $F8 then
begin
Inc(index);

ReceivedMessage[slot].Data[counter] := ReceivedMessage[slot ].Message[index] or $F0;
end
else
ReceivedMessage[slot].DataReady := FALSE; // Invalid data
end;

if ReceivedMessage[slot].DataReady = TRUE then
begin
Inc(index);

if ReceivedMessage[slot].Message[index] <$F8 then
ReceivedMessage[slot].CRC := ReceivedMessage[slot].Message[index] shl 8
else
if ReceivedMessage[slot].Message[index] = $F8 then
begin
Inc(index);

ReceivedMessage[slot].CRC := (ReceivedMessage[slot]. Message[index] or $F0) shl 8;
end;

Inc(index);

if ReceivedMessage[slot].Message[index] <$ F8 then
ReceivedMessage[slot].CRC := ReceivedMessage[slot].CRC or ReceivedMessage[slot].Message[index]
else
if ReceivedMessage[slot].Message[index] = $F8 then
begin
Inc(index);

ReceivedMessage[slot].CRC := ReceivedMessage[slot].CRC or ReceivedMessage[slot].Message[index] or $F0;
end;

This is my Java code:

if(array[1]==0xFD && (array[ 2]>0 && array[2]<17) && array[pos]==(byte)0xFF)
{
index=2;
for(int counter=1;counter< splMsg.nbytes+1;counter++)
{
index++;
if(array[index]<0xF8)
data[count er]=array[index];
else
if(array[index]==0xF8)
{
index++;
data[counter]=(byte) (array[index] | 0xF0);
}
else
return 0xFC;
}


index++;
short crc=0x0000;

if(array[index]<0xF8)
crc=(short) (array[index]<<8);
else
if (array[index]==0xF8)
{
index++;
crc=(short)((array[index] | 0xF0) << 8);
}
index++;
if(array[index]<0xF8)
crc=(short) (crc | array[index]);
else
if(array[index] ==0xF8)
{
index++;
crc=(short)(crc | array[index] | 0xF0);
}

msgcrc= new byte[] {(byte)(crc >> 8 & 0xff),(byte)(crc & 0xff)};

My function returns the transmitted CRC code most of the time, but sometimes it fails And return the last two bytes of the message. The last 3 bytes of the message are the CRC code (2 bytes) and the end of the message 0xff bytes.

Does it help?

Thank you,
Pedro

Remember:

Java bytes are signed .So:

byte test = (byte)255;
System.out.println(test);

will output: -1

and

byte test = (byte)255;
System.out.println(test == 255);

An error will be printed, but

byte test = (byte)255;
System.out.println((test & 255) == 255);

Will do what I think you want to achieve (print true in this case).

To get an unsigned (byte) value, use (array [ index]& 255).

You will have to use 0xff for masking everywhere, otherwise you will get sign extended integers with byte values ​​greater than 127.

For comparison, you It is also possible to always perform conversion to bytes (e.g. if (test == (byte) 255)) but I think a single conversion should be adhered to, so I recommend masking with &0xff.

p>

Leave a Comment

Your email address will not be published.