Ejemplo n.º 1
0
        private void ReadRegisters(ConnectorsObjectHandled par)
        {
            for (int i = 0; i < par.Connectors.Length; i++)
            {
                byte[] message = new byte[8];
                message[0] = 1;
                message[1] = 0x04;
                message[2] = (byte)(par.Connectors[i].StartAddress >> 8);
                message[3] = (byte)par.Connectors[i].StartAddress; //out 64
                message[4] = 0;
                message[5] = 1;                                    //de 8 en 8
                byte[] CRC = new byte[2];
                GetCRC(message, ref CRC);
                message[6] = CRC[0];
                message[7] = CRC[1];
                byte[] response = new byte[7];



                bool result = ReadWritePort(message, response, par.Connectors[i].Station);

                if (result)
                {
                    par.Connectors[i].ConnectValue = SetValueResponseRegisters(response);
                }
                else
                {
                    Console.WriteLine("Error Read Registers");
                }
            }
        }
Ejemplo n.º 2
0
 private void WriteCoil(ConnectorsObjectHandled par)
 {
     for (int i = 0; i < par.Connectors.Length; i++)
     {
         par.Connectors[i].Write(par.ObjectValue);
     }
 }
Ejemplo n.º 3
0
        private void ReadCoils(ConnectorsObjectHandled par)
        {
            if (par.Connectors != null && par.Connectors.Length > 0)
            {
                if (par.Connectors[0].Station.Port.Open())
                {
                    par.Connectors[0].Station.Port.DiscardOutBuffer();
                    par.Connectors[0].Station.Port.DiscardInBuffer();

                    byte[] message   = new byte[8];
                    int    contCoils = (par.Connectors[par.Connectors.Length - 1].StartAddress - par.Connectors[0].StartAddress) + 1;

                    message[0] = par.Connectors[0].Station.ID;
                    message[1] = 0x01;
                    message[2] = (byte)(par.Connectors[0].StartAddress >> 8);
                    message[3] = (byte)par.Connectors[0].StartAddress; //out 64
                    message[4] = 0;
                    message[5] = (byte)contCoils;                      //de 8 en 8

                    byte responceQuantity;
                    if (contCoils % 8 == 0)
                    {
                        responceQuantity = (byte)(contCoils / 8);
                    }
                    else
                    {
                        responceQuantity = (byte)((contCoils / 8) + 1);
                    }

                    byte[] CRC = new byte[2];
                    GetCRC(message, ref CRC);
                    message[6] = CRC[0];
                    message[7] = CRC[1];

                    byte[] response = new byte[5 + responceQuantity];



                    bool result = ReadWritePort(message, response, par.Connectors[0].Station);
                    if (result)
                    {
                        int count = par.Connectors[0].StartAddress;
                        for (int i = 0; i < responceQuantity; i++)
                        {
                            bool[] resValue = SetValueResponseCoils(response, 3 + i);

                            for (int j = 0; j < resValue.Length; j++)
                            {
                                //var val = HelperGetConnectorAddress(par.Connectors, count);
                                //if (val != null) val.ConnectValue = resValue[j];
                                if (_ConnectorAddress.ContainsKey(count))
                                {
                                    _ConnectorAddress[count].ConnectValue = resValue[j];
                                }

                                count++;
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error Read Coils");
                    }
                }
            }
        }