コード例 #1
0
ファイル: ModBusSerialMaster.cs プロジェクト: zanxi/OPU
        public void WriteComModbus(writeComm wr)
        {
            try
            {
                ushort[] b = (ushort[])wr.value;
                writeBuffer[0] = (byte)(wr.unitId & 0xff);
                writeBuffer[1] = 0x10;
                writeBuffer[2] = (byte)((wr.address >> 8) & 0xff);
                writeBuffer[3] = (byte)(wr.address & 0xff);
                int countbyte = formula.ushort2byte(b, writeBuffer, 7);
                int countregs = countbyte / 2;
                writeBuffer[4] = (byte)((countregs >> 8) & 0xff);
                writeBuffer[5] = (byte)(countregs & 0xff);
                writeBuffer[6] = (byte)(countbyte & 0xff);
                int[] crc = formula.calculateCRC(writeBuffer, 0, countbyte + 7);
                writeBuffer[countbyte + 7] = (byte)(crc[0] & 0xff);
                writeBuffer[countbyte + 8] = (byte)(crc[1] & 0xff);

                port.DiscardInBuffer();
                port.Write(writeBuffer, 0, countbyte + 9);
                Thread.Sleep(25);
            }
            catch (Exception err)
            {
                Util.errorFD();
                Util.errorMessage("ModbuserialMaster ", err.Message);
                device.status = err.HResult;
                device.ready  = false;
            }
        }
コード例 #2
0
ファイル: DriverModBus.cs プロジェクト: zanxi/OPU
 override public void setValue(int ivar, object value) // фунуция инициализации драйвера Модбас
 {
     for (int i = 0; i < registers.Count; i++)
     {
         if (ivar == registers[i].varcontext)
         {
             regModBus reg = registers[i];
             if (device.master)
             {
                 if (reg.regtype == 1 || reg.regtype == 2)
                 {
                     return;
                 }
             }
             lock (block)
             {
                 writeComm wr = new writeComm();
                 wr.reg     = reg.regtype;
                 wr.len     = reg.len;
                 wr.type    = reg.type;
                 wr.address = reg.address;
                 wr.unitId  = reg.unitId;
                 wr.value   = value;
                 killOld(wr, writeQuvery);
                 writeQuvery.Add(wr);
             }
             return;
         }
     }
 }
コード例 #3
0
ファイル: DriverModBus.cs プロジェクト: zanxi/OPU
 private void killOld(writeComm wr, List <writeComm> writeQuvery) // скрытая фунция обнуления данных полученных от данного драйвера
 {
     //lock (SpinLock.block3) // блокировка доступа к коллеции сигналов до завершения работы над ним одним из потоков драйверов
     {
         for (int i = 0; i < writeQuvery.Count; i++)
         {
             writeComm w = writeQuvery[i];
             if ((wr.unitId == w.unitId) && (wr.reg == w.reg) && (wr.address == w.address))
             {
                 writeQuvery.RemoveAt(i);
                 return;
             }
         }
     }
 }
コード例 #4
0
        void cleanQuery()
        {
            // создание дубликата writeQuveryDuble и освобождение исходной коллеции
            List <writeComm> writeQuveryDuble = new List <writeComm>();

            lock (block)
            {
                for (int i = 0; i < writeQuvery.Count; i++)
                {
                    writeQuveryDuble.Add(writeQuvery[i]);
                }
                writeQuvery.Clear();
            } //lock



            // вначале разбираем очередь на запись


            for (int i = 0; i < writeQuveryDuble.Count; i++)
            {
                writeComm wr = writeQuveryDuble[i];
                wr.address++;
                if (wr.reg == 0)
                {
                    if (wr.len == 1)
                    {
                        slave.DataStore.CoilDiscretes[wr.address] = (bool)wr.value;
                    }
                    else
                    {
                        bool[] b = (bool[])wr.value;
                        for (int j = 0; j < wr.len; j++)
                        {
                            slave.DataStore.CoilDiscretes[wr.address + j] = b[j];
                        }
                    }
                }
                if (wr.reg == 1)
                {
                    if (wr.len == 1)
                    {
                        slave.DataStore.InputDiscretes[wr.address] = (bool)wr.value;
                    }
                    else
                    {
                        bool[] b = (bool[])wr.value;
                        for (int j = 0; j < wr.len; j++)
                        {
                            slave.DataStore.InputDiscretes[wr.address + j] = b[j];
                        }
                    }
                }
                if (wr.reg == 2)
                {
                    if (wr.type == 2 || wr.type == 3)     //2 байтный int
                    {
                        if (wr.len == 1)
                        {
                            slave.DataStore.InputRegisters[wr.address] = (ushort)wr.value;
                        }
                        else
                        {
                            ushort[] b = (ushort[])wr.value;
                            for (int j = 0; j < wr.len; j++)
                            {
                                slave.DataStore.InputRegisters[wr.address + j] = b[j];
                            }
                        }
                    }
                    if (wr.type >= 4 && wr.type <= 7)     //4 байтный int
                    {
                        if (wr.len == 1)
                        {
                            ushort lowOrderValue  = BitConverter.ToUInt16(BitConverter.GetBytes((int)wr.value), 0);
                            ushort highOrderValue = BitConverter.ToUInt16(BitConverter.GetBytes((int)wr.value), 2);
                            slave.DataStore.InputRegisters[wr.address]     = lowOrderValue;
                            slave.DataStore.InputRegisters[wr.address + 1] = highOrderValue;
                        }
                        else
                        {
                            int[] b = (int[])wr.value;
                            for (int j = 0; j < wr.len; j++)
                            {
                                ushort lowOrderValue  = BitConverter.ToUInt16(BitConverter.GetBytes(b[j]), 0);
                                ushort highOrderValue = BitConverter.ToUInt16(BitConverter.GetBytes(b[j]), 2);
                                slave.DataStore.InputRegisters[wr.address + (j * 2)]     = lowOrderValue;
                                slave.DataStore.InputRegisters[wr.address + (j * 2) + 1] = highOrderValue;
                            }
                        }
                    }
                    if (wr.type >= 8 && wr.type <= 9)     //4 байтный float
                    {
                        if (wr.len == 1)
                        {
                            byte[] b              = BitConverter.GetBytes((float)wr.value);
                            ushort lowOrderValue  = BitConverter.ToUInt16(b, 0);
                            ushort highOrderValue = BitConverter.ToUInt16(b, 2);
                            slave.DataStore.InputRegisters[wr.address]     = lowOrderValue;
                            slave.DataStore.InputRegisters[wr.address + 1] = highOrderValue;
                        }
                        else
                        {
                            float[] bb = (float[])wr.value;
                            for (int j = 0; j < wr.len; j++)
                            {
                                byte[] b              = BitConverter.GetBytes(bb[j]);
                                ushort lowOrderValue  = BitConverter.ToUInt16(b, 0);
                                ushort highOrderValue = BitConverter.ToUInt16(b, 2);
                                slave.DataStore.InputRegisters[wr.address + (j * 2)]     = lowOrderValue;
                                slave.DataStore.InputRegisters[wr.address + (j * 2) + 1] = highOrderValue;
                            }
                        }
                    }
                }
                if (wr.reg == 3)
                {
                    if (wr.type == 2 || wr.type == 3)     //2 байтный int
                    {
                        if (wr.len == 1)
                        {
                            slave.DataStore.HoldingRegisters[wr.address] = (ushort)wr.value;
                        }
                        else
                        {
                            ushort[] b = (ushort[])wr.value;
                            for (int j = 0; j < wr.len; j++)
                            {
                                slave.DataStore.HoldingRegisters[wr.address + j] = b[j];
                            }
                        }
                    }
                    if (wr.type >= 4 && wr.type <= 7)     //4 байтный int
                    {
                        if (wr.len == 1)
                        {
                            ushort lowOrderValue  = BitConverter.ToUInt16(BitConverter.GetBytes((int)wr.value), 0);
                            ushort highOrderValue = BitConverter.ToUInt16(BitConverter.GetBytes((int)wr.value), 2);
                            slave.DataStore.HoldingRegisters[wr.address]     = lowOrderValue;
                            slave.DataStore.HoldingRegisters[wr.address + 1] = highOrderValue;
                        }
                        else
                        {
                            int[] b = (int[])wr.value;
                            for (int j = 0; j < wr.len; j++)
                            {
                                ushort lowOrderValue  = BitConverter.ToUInt16(BitConverter.GetBytes(b[j]), 0);
                                ushort highOrderValue = BitConverter.ToUInt16(BitConverter.GetBytes(b[j]), 2);
                                slave.DataStore.HoldingRegisters[wr.address + (j * 2)]     = lowOrderValue;
                                slave.DataStore.HoldingRegisters[wr.address + (j * 2) + 1] = highOrderValue;
                            }
                        }
                    }
                    if (wr.type >= 8 && wr.type <= 9)     //4 байтный float
                    {
                        if (wr.len == 1)
                        {
                            byte[] b              = BitConverter.GetBytes((float)wr.value);
                            ushort lowOrderValue  = BitConverter.ToUInt16(b, 0);
                            ushort highOrderValue = BitConverter.ToUInt16(b, 2);
                            slave.DataStore.HoldingRegisters[wr.address]     = lowOrderValue;
                            slave.DataStore.HoldingRegisters[wr.address + 1] = highOrderValue;
                        }
                        else
                        {
                            float[] bb = (float[])wr.value;
                            for (int j = 0; j < wr.len; j++)
                            {
                                byte[] b              = BitConverter.GetBytes(bb[j]);
                                ushort lowOrderValue  = BitConverter.ToUInt16(b, 0);
                                ushort highOrderValue = BitConverter.ToUInt16(b, 2);
                                slave.DataStore.HoldingRegisters[wr.address + (j * 2)]     = lowOrderValue;
                                slave.DataStore.HoldingRegisters[wr.address + (j * 2) + 1] = highOrderValue;
                            }
                        }
                    } //if
                }     //if
            }         //for
                 //writeQuvery.Clear();
        }