Sending 0xFF and Calculating CRC with signed bytes - WriteSingleCoil & ModBUS & Java & Android -

StackOverflow https://stackoverflow.com/questions/13232031

문제

EDITED & SOVLED (below)

I'm using Java for Android trying to send the byte 255 (0xFF in WriteSingleCoil function) to a ModBUS server device.

Device is not runnig, I don't know if because of not able to interpretate the signed byte -1 or because of I'm wrong calculating the CRC. I don't know how to calculate CRC for negative bytes.

Summarizing: I don't know how to send function 05 Write Single Coil with 0xFF value for switch on the coil for from Java to ModBUS server.

Can anyone help me please?

SOLUTION:

" iIndex = ucCRCLo ^ b: operations like this must be written as iIndex = (ucCRCLo ^ b)&0xff because the & will cast ucCRCLo, b and the result to int, which is 32 bits while short is 16 so you will have a lot of extra bits set to 1 "

This answer helped me. Thanks a lot to TheDayOfcondor

But also my huge problem was the usual problem in Java with signed bytes. My CRC calculating function is doing it right for unsigned bytes, but it give errors if I pass inside signed bytes. The trick for work with bytes for ModBUS comunication is work in the whole App with shorts as bytes, for have the range 0-255, even calculating trames and CRC. And only in the last step, when sending trame to ModBUS sever, cast them to bytes again. This is running.

Hope it will helps to someone in future.

EXPLAINING PROBLEM:

I'm trying to set ON a coil to ModBUS with function 05, this is explaining of function:

Request

I'm tryiing to set ON the coil on address 1:

This hex: 0A 05 00 01 ff 00 DC 81

This byte array: 10 5 0 1 255 0 220 129

10: The Slave Address (10 = 0A hex)

05: The Function Code (Force Single Coil)

0001: The Data Address of the coil. (coil# 1 = 01 hex)

FF00: The status to write ( FF00 = ON, 0000 = OFF )

DC81: The CRC (cyclic redundancy check) for error checking.

The thing is that Java is using signed bytes, so I can't put 255 on my byte array. I understand I should put -1, but then I can't calculate CRC correctly, because of I have a couple of precalculated array of bytes for get the CRC but the function send a negative index.

So: I don't know if I'm doing right trying to send -1, if I have an alternative for sending 255, neither how to calculate CRC for -1.

This is function for calculate CRC:

public short[] GenerateCRC (byte[] pMsg) {
    short ucCRCHi = 0xFF;
    short ucCRCLo = 0xFF;
    int iIndex;

    for (byte b : pMsg)
    {
        iIndex = ucCRCLo ^ b;

        try {
            ucCRCLo = (short)(ucCRCHi ^ aucCRCHi[ ( iIndex ) ]);
            ucCRCHi = aucCRCLo[ ( iIndex ) ];
        } catch (Exception e) {
            Log.e(LOGTAG, "GenerateCRC: " + e.toString(), e);
            e.printStackTrace();
        }
    }

        short[]result= new short[2];
        result0]= ucCRCHi;
        result1]= ucCRCLo;

        return result;
}
도움이 되었습니까?

해결책

The question is not very clear - however, the most common problem dealing with bytes is the fact Java does not have unsigned bytes, and boolean operation are always between int

The best way to deal with bytes is to use integers, and-ing every operation with 0xff. Also use >>> for the shift right (it is the unsigned version)

Example:

byte b= (byte)(255 & 0xff) // gives you the "unsigned byte"

byte b= (byte) ((b<<2)0xff ) // shift left must be truncated

If you post your code to calculate the CRC I can have a look into it

The best way to define a byte array without using negative numbers is like this:

byte[]={ (byte)0xff, (byte)0xff, (byte)0xff };
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top