Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8783999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:58:19+00:00 2026-06-13T20:58:19+00:00

EDITED & SOVLED (below) I’m using Java for Android trying to send the byte

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:58:21+00:00Added an answer on June 13, 2026 at 8:58 pm

    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 };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDITED: Java httpPost into .asp form I am trying to understand .asp a little
Edited at the request of commenters. I hope this is compliant. First post! Trying
(Edited for clarification) My (non-OSGi) application build is in Gradle, and I am trying
EDITED I'm using Twitter Bootstrap and need to move navbar right( not float:right, but
Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
Edited my code to use WebClient...still doesnt work string hhtmlurl = /Thumbnail.aspx?productID=23&Firstname=jimmy&lastnight=smith; string strFileName
edited: This is what i need: sendpost = function(a,b,c){ return jQuery.post('inc/operations.php', {a:b}, c, json);
EDITED * * Hello I am new to PHP and trying to figure out
EDITED: this first version was a false example with false assumptions! See below for
Edited: Look at comments below. Short version: Screen simply flashes when I try to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.