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 1062901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:42:06+00:00 2026-05-16T18:42:06+00:00

I am a newbie to modbus and need some help. I am trying to

  • 0

I am a newbie to modbus and need some help. I am trying to connect using modbus and serial communication. so far i managed to send data but i am unable to get any. the following is my code.

Building packet

        private byte[] BuildPacket(int meter_address,int function,int table_name,int table_offset,int high_byte, int low_byte)
    {
        try
        {
            byte[] packet = new byte[6];
            packet[0] = Convert.ToByte(meter_address);
            packet[1] = Convert.ToByte(function);
            packet[2] = Convert.ToByte(table_name);
            packet[3] = Convert.ToByte(table_offset);
            packet[4] = Convert.ToByte(high_byte);
            packet[5] = Convert.ToByte(low_byte);

            byte[] checksum = DoCheckSum(packet);

            byte[] sendPacket = new byte[8];
            sendPacket[0] = packet[0];
            sendPacket[1] = packet[1];
            sendPacket[2] = packet[2];
            sendPacket[3] = packet[3];
            sendPacket[4] = packet[4];
            sendPacket[5] = packet[5];
            sendPacket[6] = checksum[0];
            sendPacket[7] = checksum[1];

            return sendPacket;

        }
        catch (Exception)
        {

            throw;
        }
    }

Checksum for modbus

        try
        {
            ushort CRCFull = 0xFFFF;
            byte CRCHigh = 0xFF, CRCLow = 0xFF;
            char CRCLSB;

            for (int i = 0; i < (packet.Length); i++)
            {
                CRCFull = (ushort)(CRCFull ^ packet[i]);

                for (int j = 0; j < 8; j++)
                {
                    CRCLSB = (char)(CRCFull & 0x0001);
                    CRCFull = (ushort)((CRCFull >> 1) & 0x7FFF);

                    if (CRCLSB == 1)
                        CRCFull = (ushort)(CRCFull ^ 0xA001);
                }
            }
            byte[] crcByte = new byte[2];
            crcByte[1] = CRCHigh = (byte)((CRCFull >> 8) & 0xFF);
            crcByte[0] = CRCLow = (byte)(CRCFull & 0xFF);
            return crcByte;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Connection through serial and modbus

 public void ConnectSerialModBus(string COM, int baud)
    {
        SerialPort port = new SerialPort(COM, baud, Parity.None, 8, StopBits.One);
            if (!(port.IsOpen))
            {
                byte[] sendPacket = BuildPacket(3, 4, 11, 0, 1, 200);
                port.Open();
                port.RtsEnable = false;
                port.Handshake = Handshake.None;
                //SEND PACKET TO DEVICE
                port.Write(sendPacket, 0, sendPacket.Length);

                #region RECEIVE DATA FROM SERIAL
                //MAKE PROCESS STOP FOR 5sec
                Thread.Sleep(3000);
                port.DiscardOutBuffer();
                port.DiscardInBuffer();
                port.RtsEnable = true;
                int size = port.ReadBufferSize;

                byte[] readingbyte = new byte[size];

                port.Read(readingbyte, 0, readingbyte.Length);
                string reading = Encoding.GetEncoding("Windows-1252").GetString(readingbyte);

                port.Close();
                port.Dispose();
                #endregion
            }
    }

The problem is when it comes to reading the response, the program gets stuck. if possible please help me out figure what is wrong with it.

  • 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-05-16T18:42:07+00:00Added an answer on May 16, 2026 at 6:42 pm

    found a solution to the problem, the problem was with the thread.sleep. was giving it 3secs which is too much for the rtf to receive the packet. changed to 10ms and worked fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie question since I'm not up to speed using maven at all. I'm trying
Newbie Warning I have a simple but vexing problem trying to disable an NSButton.
Newbie question here... I need to select an JDBC driver to connect an application
Newbie question... I am using silverlight to POST data to my GAE application class
Newbie question here but for some reason I cant figure this out. I have
Newbie to web-services, so asking few basic questions I have a some data around
Newbie question I know... But can I build apps for Snow Leopard using the
newbie programmer and lurker here, hoping for some sensible advice. :) Using a combination
Newbie to C++ but I am a Java programmer. Trying to learn C++ now.
Newbie in testing. I generated a test case using Selenium, and then exported it

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.