tags:

views:

63

answers:

1

Hi there,

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.

A: 

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.

IanCian