public async Task <Tuple <ushort[], bool[]> > ReadRegistersAndCoilsAsync(int regAddress, int regLength, int coilAddr, int coilLength)
        {
            ushort[] regData;
            bool[]   coilData;
            if (this.CheckConnection())
            {
                try {
                    using TcpClient client = new TcpClient(this.IpAddress, this.Port);
                    ModbusIpMaster master = ModbusIpMaster.CreateIp(client);
                    if (this.SlaveAddress != 0)
                    {
                        regData = await master.ReadHoldingRegistersAsync(this.SlaveAddress, (ushort)regAddress, (ushort)regLength);

                        coilData = await master.ReadCoilsAsync(this.SlaveAddress, (ushort)coilAddr, (ushort)coilLength);
                    }
                    else
                    {
                        regData = await master.ReadHoldingRegistersAsync((ushort)regAddress, (ushort)regLength);

                        coilData = await master.ReadCoilsAsync((ushort)coilAddr, (ushort)coilLength);
                    }
                    client.Close();
                    master.Dispose();
                    return(new Tuple <ushort[], bool[]>(regData, coilData));
                } catch {
                    return(null);
                }
            }
            return(null);
        }
 public async Task <bool[]> ReadCoilsAsync(int address, int length)
 {
     bool[] regData;
     if (this.CheckConnection())
     {
         try {
             using TcpClient client = new TcpClient(this.IpAddress, this.Port);
             ModbusIpMaster master = ModbusIpMaster.CreateIp(client);
             if (this.SlaveAddress != 0)
             {
                 regData = await master.ReadCoilsAsync(this.SlaveAddress, (ushort)address, (ushort)length);
             }
             else
             {
                 regData = await master.ReadCoilsAsync((ushort)address, (ushort)length);
             }
             client.Close();
             master.Dispose();
             return(regData);
         } catch {
             return(null);
         }
     }
     return(null);
 }
Exemple #3
0
        private async Task Broadcast()
        {
            ushort[] regData;
            bool[]   coilData;
            using (FacilityContext context = new FacilityContext()) {
                ModbusDevice device = context.ModbusDevices.FirstOrDefault(x => x.Identifier == "NH3_Box");


                if (device != null)
                {
                    using (TcpClient client = new TcpClient(device.IpAddress, device.Port)) {
                        ModbusIpMaster master = ModbusIpMaster.CreateIp(client);
                        regData = await master.ReadHoldingRegistersAsync(0, 70);

                        coilData = await master.ReadCoilsAsync(0, 10);

                        client.Close();
                    }
                    AmmoniaBoxReading reading = new AmmoniaBoxReading(DateTime.Now, "Tank 2 Loaded", device);
                    reading.Tank1Weight = BitConverter.ToInt32(BitConverter.GetBytes(regData[1]).Concat(BitConverter.GetBytes(regData[0])).ToArray(), 0);
                    reading.Tank2Weight = BitConverter.ToInt32(BitConverter.GetBytes(regData[3]).Concat(BitConverter.GetBytes(regData[2])).ToArray(), 0);
                    reading.Tank3Weight = BitConverter.ToInt32(BitConverter.GetBytes(regData[5]).Concat(BitConverter.GetBytes(regData[4])).ToArray(), 0);
                    reading.Tank4Weight = BitConverter.ToInt32(BitConverter.GetBytes(regData[7]).Concat(BitConverter.GetBytes(regData[6])).ToArray(), 0);

                    reading.Tank1Tare = (int)regData[56];
                    reading.Tank2Tare = (int)regData[57];
                    reading.Tank3Tare = (int)regData[58];
                    reading.Tank4Tare = (int)regData[59];

                    reading.Tank1Temperature = (int)regData[60];
                    reading.Tank2Temperature = (int)regData[61];
                    reading.Tank3Temperature = (int)regData[62];
                    reading.Tank4Temperature = (int)regData[63];

                    reading.Heater1DutyCycle = (int)regData[66];
                    reading.Heater2DutyCycle = (int)regData[67];
                    reading.Heater3DutyCycle = (int)regData[68];
                    reading.Heater4DutyCycle = (int)regData[69];

                    reading.Tank1Warning = coilData[2];
                    reading.Tank2Warning = coilData[3];
                    reading.Tank3Warning = coilData[4];
                    reading.Tank4Warning = coilData[5];
                    reading.Tank1Alarm   = coilData[6];
                    reading.Tank2Alarm   = coilData[7];
                    reading.Tank3Alarm   = coilData[8];
                    reading.Tank4Alarm   = coilData[9];


                    this._dataViews[0].Weight      = Convert.ToDecimal(reading.Tank1Weight);
                    this._dataViews[0].Temperature = Convert.ToDecimal(reading.Tank1Temperature);
                    this._dataViews[0].Status      = "Online";
                    this._dataViews[0].Alarm       = (reading.Tank1Alarm) ? "Ammonia Is Below Limit" : "Okay";
                    this._dataViews[0].Warning     = (reading.Tank1Alarm) ? "Ammonia Is Getting Low" : "Okay";

                    this._dataViews[1].Weight      = Convert.ToDecimal(reading.Tank2Weight);
                    this._dataViews[1].Temperature = Convert.ToDecimal(reading.Tank2Temperature);
                    this._dataViews[1].Status      = "Online";
                    this._dataViews[1].Alarm       = (reading.Tank2Alarm) ? "Ammonia Is Below Limit" : "Okay";
                    this._dataViews[1].Warning     = (reading.Tank2Alarm) ? "Ammonia Is Getting Low" : "Okay";

                    this._dataViews[2].Weight      = Convert.ToDecimal(reading.Tank3Weight);
                    this._dataViews[2].Temperature = Convert.ToDecimal(reading.Tank3Temperature);
                    this._dataViews[2].Status      = "Online";
                    this._dataViews[2].Alarm       = (reading.Tank3Alarm) ? "Ammonia Is Below Limit" : "Okay";
                    this._dataViews[2].Warning     = (reading.Tank3Alarm) ? "Ammonia Is Getting Low" : "Okay";

                    this._dataViews[3].Weight      = Convert.ToDecimal(reading.Tank4Weight);
                    this._dataViews[3].Temperature = Convert.ToDecimal(reading.Tank4Temperature);
                    this._dataViews[3].Status      = "Online";
                    this._dataViews[3].Alarm       = (reading.Tank4Alarm) ? "Ammonia Is Below Limit" : "Okay";
                    this._dataViews[3].Warning     = (reading.Tank4Alarm) ? "Ammonia Is Getting Low" : "Okay";



                    await this._hubContext.Clients.All.BroadcastAllTanks(this._dataViews);
                }
            }
        }
Exemple #4
0
        private bool RequestData(IO_DEVICE device, RealData realData, out string error, ModbusFragmentStore fragmentstore)
        {
            error = "";
            try
            {
                if (udpClient != null)
                {
                    //分段读取数据,如果是读取整个寄存器的话,一次只能最多读取123个,
                    //如果是读取线圈的话最大只能读取1999个,因此要分段进行数据的读取
                    List <byte> allbytes = new List <byte>();
                    try
                    {
                        for (int i = 0; i < fragmentstore.Fragments.Count; i++)
                        {
                            ModbusFragment fragment = fragmentstore.Fragments[i];
                            switch (fragment.Code)
                            {
                            case "01":    // 01和05是一个码 可写可读
                            {
                                //返回的线圈状态,由于线圈是按位操作,转换也是按位转换
                                Task <bool[]> result = master.ReadCoilsAsync(byte.Parse(device.IO_DEVICE_ADDRESS), fragment.StartRegister, fragment.RegisterNum);

                                byte[] bytes = ModbusConvert.BoolToByte(result.Result);
                                fragment.StartIndex = allbytes.Count;
                                fragment.Length     = bytes.Length;
                                allbytes.AddRange(bytes);
                            }
                            break;

                            case "02":    //只读属性
                            {
                                //返回的线圈状态
                                Task <bool[]> result = master.ReadInputsAsync(byte.Parse(device.IO_DEVICE_ADDRESS), fragment.StartRegister, fragment.RegisterNum);
                                byte[]        bytes  = ModbusConvert.BoolToByte(result.Result);
                                fragment.StartIndex = allbytes.Count;
                                fragment.Length     = bytes.Length;
                                allbytes.AddRange(bytes);
                            }
                            break;

                            case "03":    //HR保持寄存器,可写可读
                            {
                                //返回的数据全部是ushort 需要将ushort 转换为byte在进行传递
                                Task <ushort[]> result = master.ReadHoldingRegistersAsync(byte.Parse(device.IO_DEVICE_ADDRESS), fragment.StartRegister, fragment.RegisterNum);
                                byte[]          bytes  = ModbusConvert.Ushorts2Bytes(result.Result);
                                fragment.StartIndex = allbytes.Count;
                                fragment.Length     = bytes.Length;
                                allbytes.AddRange(bytes);
                            }
                            break;

                            case "04":    //只读属性
                            {
                                //返回的数据全部是ushort 需要将ushort 转换为byte在进行传递
                                Task <ushort[]> result = master.ReadInputRegistersAsync(byte.Parse(device.IO_DEVICE_ADDRESS), fragment.StartRegister, fragment.RegisterNum);
                                byte[]          bytes  = ModbusConvert.Ushorts2Bytes(result.Result);
                                fragment.StartIndex = allbytes.Count;
                                fragment.Length     = bytes.Length;
                                allbytes.AddRange(bytes);
                            }
                            break;
                            }
                        }
                    }
                    catch
                    {
                        //读取异常处理
                        this.DeviceStatus(this.IOServer, this.IOCommunication, device, null, "0");//tag为1表示上线,如果为0表示下线
                    }
                    //将数据返回到采集客户端
                    if (allbytes.Count > 0)
                    {
                        device.IO_DEVICE_STATUS = 1;
                        ReceiveData(this.IOServer, this.IOCommunication, device, allbytes.ToArray(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), fragmentstore);
                        //设置设备状态
                        this.DeviceStatus(this.IOServer, this.IOCommunication, device, null, "1");//tag为1表示上线,如果为0表示下线
                    }
                    else
                    {
                        device.IO_DEVICE_STATUS = 0;
                        //设置设备状态
                        this.DeviceStatus(this.IOServer, this.IOCommunication, device, null, "0");//tag为1表示上线,如果为0表示下线
                    }
                }
                return(true);
            }
            catch
            {
                return(false);
            }
        }