ฉันกำลังย้ายแอปพลิเคชันเก่าที่เขียนด้วย Delphi ไปยัง Java ฉันประสบปัญหากับฟังก์ชันนี้ ซึ่งคำนวณ CRC ของข้อความที่ส่ง
นี่คือรหัสต้นฉบับ:
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);
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;
และนี่คือโค้ด Java ของฉัน:
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[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)};
ฟังก์ชันของฉันส่งคืนรหัส CRC ที่ส่งเกือบครั้ง แต่บางครั้งก็ล้มเหลวและส่งคืนข้อความสองไบต์สุดท้าย 3 ไบต์สุดท้ายของข้อความคือรหัส CRC (2 ไบต์) และไบต์ท้ายข้อความ 0xff
ความช่วยเหลือใด ๆ ?
ขอบคุณเปโดร
byte
หรือไม่ - person Neet   schedule 15.02.2013Another Delphi to Java porting issue
ได้ไหม - person Jerry Dodge   schedule 16.02.2013