Пример #1
0
        public bool SendFc16(byte address, ushort start, ushort registers, short[] values)
        {
            //Ensure port is open:
            if (sp.IsOpen)
            {
                //Clear in/out buffers:
                sp.DiscardOutBuffer();
                sp.DiscardInBuffer();
                //Message is 1 addr + 1 fcn + 2 start + 2 reg + 1 count + 2 * reg vals + 2 CRC
                byte[] message = new byte[9 + 2 * registers];
                //Function 16 response is fixed at 8 bytes
                byte[] response = new byte[8];

                //Add bytecount to message:
                message[6] = (byte)(registers * 2);
                //Put write values into message prior to sending:
                for (int i = 0; i < registers; i++)
                {
                    message[7 + 2 * i] = (byte)(values[i] >> 8);
                    message[8 + 2 * i] = (byte)(values[i]);
                }
                //Build outgoing message:
                BuildMessage(address, (byte)16, start, registers, ref message);

                //Send Modbus message to Serial Port:
                try
                {
                    sp.Write(message, 0, message.Length);
                    GetResponse(ref response);
                }
                catch (Exception err)
                {
                    modbusStatus = "Error in write event: " + err.Message;
                    return(false);
                }
                //Evaluate message:
                if (CheckResponse(response))
                {
                    modbusStatus = "Write successful";
                    return(true);
                }
                else
                {
                    modbusStatus = "CRC error";
                    return(false);
                }
            }
            else
            {
                modbusStatus = "Serial port not open";
                return(false);
            }
        }
Пример #2
0
        protected override async Task <byte[]> SendRequest(byte[] request, int reqLen)
        {
            //double slient_interval = 1000 * 5 * ((double)1 / (double)config.BaudRate);
            byte[] response = null;

            m_semaphore_connection.Wait();

            if (m_serialPort != null && m_serialPort.IsOpen())
            {
                try
                {
                    m_serialPort.DiscardInBuffer();
                    m_serialPort.DiscardOutBuffer();
                    Task.Delay(m_silent).Wait();
                    m_serialPort.Write(request, 0, reqLen);
                    response = ReadResponse();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Something wrong with the connection, disposing...");
                    Console.WriteLine(e.Message);
                    m_serialPort.Dispose();
                    m_serialPort = null;
                    Console.WriteLine("Connection lost, reconnecting...");
                    await ConnectSlave();
                }
            }
            else
            {
                Console.WriteLine("Connection lost, reconnecting...");
                await ConnectSlave();
            }

            m_semaphore_connection.Release();

            return(response);
        }